On Sun, Dec 08, 2002 at 03:36:11PM +1300, Paul wrote:
> I am trying to delete files by piping the output from ls to egrep to rm :
> ls | egrep '(cd|CD)' | rm
As has already been mentioned, xargs is the utility command you need to
connect an 'argument only' command to a pipeline of data.
However, I was wondering about your use of egrep - it's a little
expensive in cpu terms, and the shell provides great regexp controls
that you can use ...
It looks like you're trying to get rid of any file with 'cd' or 'CD' in
its name - this would include things like 'abcdef' as well as 'file.cd'?
Always check a regexp before committing it to a rm command ...
$ ls *cd* *CD*
$ rm *cd* *CD*
How about those with Cd or cD as well?
$ ls *[cC][dD]*
$ rm *[cC][dD]*
On the other hand, if you wanted to delete only files ending in cd|CD,
$ ls *cd *CD
Basically, your shell can already do a lot of work with regexps, and it
does all the expanding of * before the ls or rm commands get invoked.
Other things to look at are :-
? matches a single character
* matched zero or more characters
[ ] specifies a range of characters, there are lots of variations here.
[AbcDeF] - any one of those characters listed, and nothing else
[^AbcDef] - anything except those characters listed
[A-Z] - any capital letter
[0-9] - any digit
[^A-Z] - anything except a capital letter
Ranges become much more powerful within full regexp environments, such
as perl and other programming languages, and enable you to specify
groups like 'white space' or 'lowercase letters' without having to make
any explicit assumptions about the character set. All the range examples
above are only valid for ASCII, but most shells are pretty simple.
-jim