rm files that do not match a given pattern
Hello,
I'm trying to delete all the files in the current directory that doesn't match the pattern *.tex. Is there a switch in rm that inverts the match (something like grep's "-v" option)? Thanks! |
I don't think that there exists a switch for rm that inverts the match. One way is to output all files that don't have a .tex extension to a file and then read this file to remove the unwanted ones.
Code:
ls -l | grep -v .tex | awk '{print $9}' > /tmp/toremove.txt Code:
while read linevar |
Have a look at bash's extended globbing feature.
http://www.linuxjournal.com/content/...ended-globbing |
|
Code:
sasha@reactor: find . -maxdepth 1 -type f ! -iname "*.tex" -delete |
To expand on valen tino's post:
rm -f `ls -l | grep -v .tex | awk '{print $9}'` |
So many different methods to skin a cat. I like the following method as it allows you a clean way to review the commands before execution. Also it is good to use "drwx" to de-list and directories. Also some versions of ls will print a total bytes that you want to strip out for clean execution.
ls -la |grep -v drwx |grep -v total | grep -v .tex | awk '{print "rm -f " $8}' Once you review the command and verify it is what you want then simply pipe to bash. ls -la |grep -v drwx |grep -v total | grep -v .tex | awk '{print "rm -f " $8}' | bash |
To see what you're going to delete, without really doing any deleting, take what's in post #5 and remove the -delete from the end.
There's no need to have 3 greps and an awk to do this. :) |
That's great. Thanks everyone for the help!
|
Here's what I would consider:
Code:
rm -rf $(ls -l | grep -v .tex | awk '{print $9}') Edit: You could also set a variable to include the command to use the rm -rf on: Code:
export TOREMOVE="ls -l | grep -v *.tex | awk '{print $9}'" |
Way To Complicated
Why don't you just man the rm command...
rm [!a]* Kind Regards, John Ortega Madrid Linux Users Group http://madridlinuxgroup.blogspot.com Join Us Below! http://www.linux.com/community/group...p+Madrid+Spain http://www.facebook.com/group.php?gid=123666504360711 http://www.facebook.com/pages/Madrid...26426490752419 |
Do NOT issue that command
Quote:
|
Dude, just apply the logic.
Uh, did you not read what he wants?
Ok, I just posted up the general solution but the idea is the same... here's the original mail... read it this time ---------------------------------- I'm trying to delete all the files in the current directory that doesn't match the pattern *.tex. Is there a switch in rm that inverts the match (something like grep's "-v" option) ----------------------------------- He is trying to delete all of the files except those that match *.tex So, if you use my original solution it's simple. rm ./*[!.tex] You guys flip me sometimes... you want a straight answer instead of just applying the logic. So there you have you answer. Kind Regards, John Ortega Madrid Linux Users Group http://madridlinuxgroup.blogspot.com Join Us Below! http://www.linux.com/community/group...p+Madrid+Spain http://www.facebook.com/group.php?gid=123666504360711 http://www.facebook.com/pages/Madrid...26426490752419 |
Still wrong!
I guess what you actually mean is bash's extended globbing mechanism. But this is nothing specific to 'rm'.
Code:
rm ./!(*.tex) Look up Code:
man bash Not sure if this option is available on all shells. So the above given solutions should be kept in mind. |
I'm curios.
Did you actually try my solution? Kind Regards, John Ortega Madrid Linux Users Group http://madridlinuxgroup.blogspot.com Join Us Below! http://www.linux.com/community/group...p+Madrid+Spain http://www.facebook.com/group.php?gid=123666504360711 http://www.facebook.com/pages/Madrid...26426490752419 |
Quote:
Try it with the following filenames: Code:
a1.tex P.S.: However, I noticed a typo ('.text' instead of '.tex') in my previous post. Corrected it. |
Quote:
This is much better. |
All times are GMT -5. The time now is 09:26 PM. |