edub
edub

Reputation: 61

Count and remove extraneous files (bash)

I am getting stuck on finding a succint solution to the following.

In a given directory, I have the following files:

10_MIDAP.nii.gz
12_MIDAP.nii.gz
14_MIDAP.nii.gz
16_restAP.nii.gz
18_restAP.nii.gz

I am only supposed to have two "MIDAP" files and one "restAP" file. The additional files may not contain the full data, so I need to remove them. These are likely going to be smaller in size and/or the earlier sequence number (e.g., 10).

I know how to count / echo the number of files:

MIDAP=`find $DATADIR -name "*MIDAP.nii.gz" | wc -l`
RestAP=`find $DATADIR -name "*restAP.nii.gz" | wc -l`

echo "MIDAP files = $MIDAP"
echo "RestAP files = $RestAP"

Any suggestions on how to succinctly remove the unneeded files, such that I end up with two "MIDAP" files and one "restAP" (in cases where there are extraneous files)? As of now, imagining it would be something like this...

if (( $MIDAP > 2 )); then
  ...magic happens
fi

Thanks for any advice!

Upvotes: 1

Views: 51

Answers (1)

karakfa
karakfa

Reputation: 67467

here is an approach

create test files

$ for i in {1..10}; do touch ${i}_restAP; touch ${i}_MIDAP; done  

sort based on numbers, and remove the top N-1 (or N-2) files.

$ find . -name '*restAP*' | sort -V | head -n -1 | xargs rm
$ find . -name '*MIDAP*'  | sort -V | head -n -2 | xargs rm

$ ls -1

10_MIDAP
10_restAP
9_MIDAP

you may want to change the sort if based on file size.

Upvotes: 2

Related Questions