rm all files but one

rm all files but one

Post by s » Sun, 09 Sep 2001 10:20:11



i am trying to remove all files in a directory except one.
for example if i have files a , b, and c then i want to remove
b and c but keep a. something like if test filename != a ; then rm *; fi
i dont really need to do this but i just cant figure it out. any help
please?
 
 
 

rm all files but one

Post by Matthew Clin » Sat, 08 Sep 2001 07:36:31


s was touched by the minds of the terrible Old Ones, and imparted unto us
these blasphemous ravings:

Quote:> i am trying to remove all files in a directory except one.
> for example if i have files a , b, and c then i want to remove
> b and c but keep a. something like if test filename != a ; then rm *; fi
> i dont really need to do this but i just cant figure it out. any help
> please?

Well, if you're doing it interactively, you could do:

    rm b c d e f

To delete everything but "a", or

    rm -i *

and it will ask you to delete or no for each file.  Assuming that you want
to do it non-interactily, you could do:

    rm `ls -1 | grep -v '^$a'`

which would delete everything but file a; if you wanted to do it for all
files under a certain directory, you could do:

    find directory \! -name a -print | xargs rm

 
 
 

rm all files but one

Post by s » Sun, 09 Sep 2001 23:05:37


thats it! thanks!