1835962 Members
2184 Online
110088 Solutions
New Discussion

remove file

 
SOLVED
Go to solution
Cheung_2
Frequent Advisor

remove file

I world like to remove some files but omit one or part of these files, how to do it?

eg.
$ ls
a1 a2 a3 a4 a5
a6 ab a8 a9 a10
a1b a2b

if I run ??? rm a* ??? , then all files will be removed , how can I omit to remove a2 & a6 and the file name ended with ???b???? Thx.
Andy
9 REPLIES 9
Robert Thorneycroft
Valued Contributor

Re: remove file

The easiest way to do this is just tu use rm in interactive mode ie
rm -i a*
Then just answer y to the files you wish to remove.
This way will be 100 times quicker than trying to find some kind of pattern match.

Kind regards,

Robert Thorneycroft
Michael Tully
Honored Contributor

Re: remove file

One of the easiest ways is to use:

# rm -i
Anyone for a Mutiny ?
Ralph Grothe
Honored Contributor
Solution

Re: remove file

Just a general hint before you do any deletions,
use the shell built-in echo command to see what the shell expands your wildcards (i.e. meta characters) to.

Maybe this is not quite what you want, but remember that you can exclude certain character sets with the '!'

e.g.

echo a[!26]*

However in your case a more subtle parsing is required, where I would use commands that can handle regular expressions (e.g. grep, sed, awk, perl)
Madness, thy name is system administration
Leif Halvarsson_2
Honored Contributor

Re: remove file

Hi,
One method you can use when you want to do something with a large number of files (not necessary removing them) is to list to a file and use a text editor to process the file. Then you can use this file as input to a script. Example:

ls a* >tmpfile

vi tmpfile

while read file
do
#do something
done
Niraj Kumar Verma
Trusted Contributor

Re: remove file


#/bin/sh

for index in `ls a* |grep -v "a[26]"`
do
echo "== removing $index =="
rm -rf $index
done


-Niraj
Niraj.Verma@philips.com
justin berkman
Frequent Advisor

Re: remove file

hi guys



command :
rm a1 a[3-5] a[8-10]

omit to remove a2 - a6 - a1b and a2b
Jean-Louis Phelix
Honored Contributor

Re: remove file

hi,

egrep (or grep -E) with multiple patterns could do it.

rm -i $(ls a* | egrep -v 'a2|a6|b$')

regards
It works for me (© Bill McNAMARA ...)
Robert Thorneycroft
Valued Contributor

Re: remove file

Jimmy your substitution [8-10] will not work as the value in brackets will only substitute a single character, which 10 is not.
justin berkman
Frequent Advisor

Re: remove file

hi guys

the correct command is :

rm a1 a[3-5] a[8-9] a10



excuse me for the error