Can I skip files when running rm command

Platform: Oracle Enterprise Linux 6.2

I have several files like below. I want to remove all files except one file

For example , I want to remove all the files below except

dasd_91197.trc
$ ls -alrt *.trc
-rw-r----- 1 ecmdev wms  8438784 May 7 21:30 dasd_91177.trc
-rw-r----- 1 ecmdev wms  8439296 May 7 21:30 dasd_91178.trc
-rw-r----- 1 ecmdev wms  8439296 May 7 21:30 dasd_91179.trc
-rw-r----- 1 ecmdev wms  8436736 May 7 21:30 dasd_91180.trc
-rw-r----- 1 ecmdev wms  8438784 May 7 21:30 dasd_91181.trc
-rw-r----- 1 ecmdev wms  8439296 May 7 21:30 dasd_91182.trc
-rw-r----- 1 ecmdev wms  8438784 May 7 21:30 dasd_91183.trc
-rw-r----- 1 ecmdev wms  8438272 May 7 21:30 dasd_91184.trc
-rw-r----- 1 ecmdev wms  8439296 May 7 21:30 dasd_91186.trc
-rw-r----- 1 ecmdev wms  8437760 May 7 21:30 dasd_91185.trc
-rw-r----- 1 ecmdev wms  8437248 May 7 21:31 dasd_91187.trc
-rw-r----- 1 ecmdev wms  8439808 May 7 21:31 dasd_91188.trc
-rw-r----- 1 ecmdev wms  8439296 May 7 21:31 dasd_91189.trc
-rw-r----- 1 ecmdev wms  8437760 May 7 21:31 dasd_91190.trc
-rw-r----- 1 ecmdev wms  8440832 May 7 21:31 dasd_91191.trc
-rw-r----- 1 ecmdev wms  8436224 May 7 21:31 dasd_91192.trc
-rw-r----- 1 ecmdev wms  8441856 May 7 21:31 dasd_91193.trc
-rw-r----- 1 ecmdev wms  8437248 May 7 21:31 dasd_91194.trc
-rw-r----- 1 ecmdev wms  8437248 May 7 21:31 dasd_91195.trc
-rw-r----- 1 ecmdev wms  8437248 May 7 21:31 dasd_91200.trc
-rw-r----- 1 ecmdev wms  8437248 May 7 21:31 dasd_91199.trc
-rw-r----- 1 ecmdev wms  8436736 May 7 21:31 dasd_91198.trc
-rw-r----- 1 ecmdev wms  8436736 May 7 21:31 dasd_91197.trc ---------------> Don't remove this file
-rw-r----- 1 ecmdev wms  8436736 May 7 21:31 dasd_91196.trc
-rw-r----- 1 ecmdev wms  8439808 May 7 21:31 dasd_91202.trc
-rw-r----- 1 ecmdev wms  8436736 May 7 21:31 dasd_91201.trc
-rw-r----- 1 ecmdev wms  8441344 May 7 21:31 dasd_91203.trc
-rw-r----- 1 ecmdev wms  8441344 May 7 21:31 dasd_91204.trc
-rw-r----- 1 ecmdev wms  8438272 May 7 21:31 dasd_91205.trc
-rw-r----- 1 ecmdev wms  8441856 May 7 21:31 dasd_91206.trc
-rw-r----- 1 ecmdev wms  8437248 May 7 21:31 dasd_91207.trc

Sometimes I have deal with thousands of files and I might have keep one or two. In this case, I have to run individual rm commands for all the files I have to remove; hence looking for this solution

If there are 1000+ files you might end up with too many arguments error with below..

 
ls *.trc|grep -v <<your filename>>|xargs rm
1 Like

Here is one possible way:

ls | grep -v dasd_91197.trc | xargs rm

If more than one file to retain:

ls | grep -v -e dasd_91197.trc -e someother.trc | xargs rm

---------- Post updated at 04:49 AM ---------- Previous update was at 04:36 AM ----------

For just the *.trc extension files:

ls | grep "\.trc$" | grep -v dasd_91197.trc | xargs rm

Or probably better to use find to generate the list of files:

find . -type f -name "*.trc" | grep -v dasd_91197.trc | xargs rm

---------- Post updated at 09:34 PM ---------- Previous update was at 04:49 AM ----------

One other thing came to me, and I think important. find command of course also finds files in subdirectories (might cause a big problem, maybe remove files you had not intended to remove). So you might need to use find -maxdepth option, as follows:

$ find . -name "*.sh" -print
./xxx/test.sh
./test.sh
./test-2.sh
$ find . -maxdepth 1 -name "*.sh" -print
./test.sh
./test-2.sh

So: find . -maxdepth 1 -type f -name "*.trc" | grep -v dasd_91197.trc | xargs rm

1 Like

Unless you also want to delete files in subdirectories of your current directory, using find to get the list of files with names ending in .trc is grossly inefficient compared to ls *.trc . Unless you're on a system with a small value for ARG_MAX, you can skip using xargs as well using:

rm $(ls *.trc|grep -F -v -e dasd_91197.trc [-e dasd_other.trc]...)

The standards allow ARG_MAX to be as small as 4096, but on OS X it is 262144. You can find the value on your system using:

getconf ARG_MAX

If the value returned is likely to be smaller than the number of bytes needed to hold the names of the files you'll be removing plus any environment variables you're exporting, you'll need to keep the xargs element of the pipeline rather than using the above form of command substitution.