linux
Delete files where
size less than X
Need to delete files on your linux box that are a certain size or less? Use this quick command:
find -size -3b | xargs rm -f;
In this example, I had made an image migration scripting error that put a bunch of empty (371 bytes) images in my images folder.
Using the
-size
switch for the
find
command, one can list out all the files that are that size.
/home/web/imgs $ find -size 5k | xargs ls -lah
-rw-rw-r-- 1 jay_johnston magic_chickens 4.6K May 25 00:33 ./32.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 4.8K Jun 18 08:55 ./323t.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 4.4K May 25 00:34 ./49.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 4.8K May 25 00:34 ./50.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 4.5K May 25 00:33 ./6.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 4.3K May 25 00:34 ./72.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 5.0K May 25 00:34 ./75.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 5.0K May 25 00:34 ./76.jpg
Okay, that's pretty helpful, but what if I need to know which files are LESS than 5k:
/home/web/imgs $ find -size -5k | xargs ls -lah
-rw-rw-r-- 1 jay_johnston magic_chickens 2.2K Jun 18 08:01 ./285.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 2.2K Jun 18 08:01 ./285t.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 2.6K May 25 00:33 ./31.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 1.5K May 25 00:33 ./33.jpg
-rw-rw-r-- 1 jay_johnston magic_chickens 2.1K May 25 00:34 ./54.jpg
Okay, so if the trick is obscured by all the mess...note that the minus sign in front of the size, like this
-5k
caused it to invert the
-size
switch, thereby printing the files that are less than the desired size.And as one can see, piping the output to
xargs
allows us to run a command with the file as the standard input.Voila!
Last Updated: 2010-06-18 10:32:14