I have the following files in a directory:
-rw-r--r-- 1 smsc sys 46 Apr 22 12:09 bills.50.1.3G.MO.X.20120422120453.Z
-rw-r--r-- 1 smsc sys 28 Apr 22 12:15 bills.50.1.3G.MO.X.20120422120953.Z
-rw-r--r-- 1 smsc sys 46 Apr 22 12:20 bills.50.1.3G.MO.X.20120422121453.Z
-rw-r--r-- 1 smsc sys 46 Apr 22 12:25 bills.50.1.3G.MO.X.20120422121953.Z
Where the fifth column is the file's size. I wish to delete all files which size is 46. In order to filter out these files I used the following command:
ls -ltr | awk '$5 ~ /46/ {print $0}'
Which works fine. But now I want to delete all files which were filtered out, so I add the following to the above command:
ls -ltr | awk '$5 ~ /46/ {print $0}' | xargs rm
However it gives me the following error:
rm: invalid option -- w
It seems that I have to use find over ls so I will get the output in the below format:
./bills.50.1.3G.MO.X.20120421050453.Z
./bills.50.1.3G.MO.X.20120421154953.Z
./bills.50.1.3G.MO.X.20120419133452.Z
But then I have no way to filter the files by its parameters. How this task could be done?
findcould also do this, i.e. something likefind . -maxdepth 1 -size 46c -delete(at least GNU find can) – sr_ Apr 22 '12 at 9:51lsoutput. @sr_ You should post that as an answer. – jw013 Apr 22 '12 at 9:55lshere. – Eugene S Apr 22 '12 at 9:58find. Why don't you just use the suggested command? It is never necessary to uselsfor manipulating files (at least I have yet to run into such a situation) sincefindand shell globbing cover nearly all cases. – jw013 Apr 22 '12 at 10:05