Remove all files with a prefix except the one of the largest size

command linefileswildcards

What is the best way to remove all the files in the current directory with some prefix, except the file with this prefix but having the largest size among all the files with this prefix?

(In the case when the largest one is not unique, just randomly keep one of them and remove the others.)

For example, the current directory contains the following files:

-rw-rw----  1 user user 3468 Jan 01 00:00 filea
-rw-rw----  1 user user 3434 Jan 01 00:00 pre_0
-rw-rw-r--  1 user user 9769 Jan 01 00:00 fileb
-rw-rw-r--  1 user user 1950 Jan 01 00:00 filec
-rw-rw-r--  1 user user 8654 Jan 01 00:00 pre_1
-rw-rw----  1 user user 6508 Jan 01 00:00 pre_2

After running the command or the script to remove all the files with prefix "pre_" except the largest one, the current directory should be listed as:

-rw-rw----  1 user user 3468 Jan 01 00:00 filea
-rw-rw-r--  1 user user 9769 Jan 01 00:00 fileb
-rw-rw-r--  1 user user 1950 Jan 01 00:00 filec
-rw-rw-r--  1 user user 8654 Jan 01 00:00 pre_1

Best Answer

You can use a combination of few utilities:

stat -c '%s %n' pre_* | sort -k1,1rn | tail -n +2 | cut -d' ' -f2 | xargs rm

Assuming GNU system and no unusual filenames.

  • stat gets the filesize and name separated by space for all pre_* files

  • sort sorts the file according to the file size, with highest sized one goes to top

  • tail -n +2 gets the rest of the files apart from the max sized one

  • cut -d' ' -f2 gets the file name only, and rm (xargs rm) does the removal

Related Question