Reputation: 141
I am trying to copy files from one directory (defined as $inDir below) to another (defined as $outDir below) if they 1) exist and 2) have more than 1 line in the file (this is to avoid copying files that are empty text files). I am able to do the first part using the below code but am struggling to know how to do the latter part. I'm gussing maybe using awk and NR somehow but I'm not very good with coding in Bash so any help would be appreciated. I'd like this to be incorporated into the below if possible, so that it can be done in one step.
for i in $inDir/NVDI_500m_mean_distance_*_40PCs; do
batch_name_dir=$i;
batch_name=$(basename $i);
if [ ! -f $outDir/${batch_name}.plink.gz ]; then
echo 'Copying' $batch_name;
find $batch_name_dir -name ${batch_name}.plink.gz -exec cp {} $outDir/${batch_name}.plink.gz \;
else
echo $batch_name 'already exists'
fi
done
Upvotes: 0
Views: 769
Reputation: 143
You can use wc -l
to check how many lines are in a file and awk
to strip only the number from the result.
lines=$(wc -l $YOUR_FILE_NAME | awk '{print $1}')
if [ $lines -gt 0 ]; then
//copy the file
fi
Edit: I have corrected LINES to lines according to the comments below.
Upvotes: 1
Reputation: 344
I propose this:
for f in "$(find $indir -type f -name 'NVDI_500m_mean_distance_*_40PC' -not -empty)";
do
cp "$f" /some/targetdir;
done
find
is faster than wc
to check for zero size.
I consider it more readable, than the other solution, subjectivly.
However, the for-loop is not necessary, since:
find "$indir" -type f -name 'NVDI_500m_mean_distance_*_40PC' -not -empty |\
xargs -I % cp % /some/targetdir/%
Always "quote" path strings, since most shell utils break when there are unescaped shell chars or white spaces in the string. There are rarely good reasons to use unquoted strings.
Upvotes: 0