Removing of Multiple Files with Exception Using "rm" command in Linux

Now I get into this point. I remember I did some simple command, back then, by using "rm" command in Linux. Try searching in the net but none of it provides me the answer.

The goal was that, I have a directory named, "reports" with lots of files/directories/sub-directories inside it, and try to delete all of them but with exclusion of the files that starts with the word "report-" (ex. report-2009-10-19.html).

I thought the command issued in my shell

$> rm -rf $(ls reports/|grep -e '[^report-*]')

would enable me to delete the files but it seems it doesn't work. My expectation was the files that does not start with "reports" word will be deleted. Then I read the manual of rm ("man rm" in bash), I found that the -v would select those non-matching lines.

Now, issuing the commands in my shell

$> cd reports
$> rm -rf $(ls .|grep -v '[report-*]')

enables me to delete the files that starts with the word "report-".

Hope this helps!

Comments

Anonymous said…
This won't work for a long list of files, better use find

Popular posts from this blog

LVM: How to remove a volume using pvremove

Use Shell Editor for Eclipse for editing bash, ksh, csh in Unix/Linux system

pdftotext not found in CentOS