Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am attempting to use regular expressions to remove set of files however the bash shell returns the message

rm: cannot remove `[0-99]+ -': No such file or directory
rm: cannot remove `[a-zA-Z': No such file or directory
rm: cannot remove `]+.[a-z]+': No such file or directory

The command is [0-99]+\ - [a-zA-Z ]+\.[a-z]+

Questions

  1. Can I use regular expressions?
  2. If yes, how do I use them with commands such as rm, mkdir, etc
share|improve this question

2 Answers 2

up vote 2 down vote accepted

Bash (and the other Unix shells) use wildcards, not full regular expressions, and ranges cannot include a space.

share|improve this answer
    
What can I use with full support for regular expressions? –  PeanutsMonkey Sep 9 '12 at 21:00
    
You'll have to use find if you want full regex support. But be aware that find supports several variations on regex. The default is emacs-style. If you'd like one of the others, e.g., posix-awk or posix-egrep, you'll need to specify the -regextype. You can paste the output of find back onto the command line as arguments to rm using `...` command substitution (but not if the filenames contain spaces). –  Nicole Hamilton Sep 10 '12 at 1:01

Use regural expression into a find command like

find . -type f -regex regEXP

and use it in tandem with rm in the following way

find . -type f -regex regEXP -exec rm -rf {} \;

share|improve this answer
3  
Careful: -regexp matches the whole path (different from -name), including parent directories. Not an issue with these patterns, but something to keep in mind. –  Daniel Beck Sep 9 '12 at 8:00
    
Yes you're right but this command works –  DonCallisto Sep 9 '12 at 8:02
    
@DonCallisto: You missed a hyphen on the rf. If GNU find is available it's quicker to delete with the -delete command. –  Thor Sep 9 '12 at 10:55
    
@Daniel Beck - What do you mean by whole path? –  PeanutsMonkey Sep 9 '12 at 21:00
1  
By "whole path", he means everything including the "./" that will get pasted onto the beginning of each result. –  Nicole Hamilton Sep 10 '12 at 0:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.