Search code examples
bashfile-copying

How to copy files in Bash that have more than 1 line


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

Solution

  • 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.