[SOLVED] rm files that do not match a given pattern
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)?
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.
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.
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.
I guess what you actually mean is bash's extended globbing mechanism. But this is nothing specific to 'rm'.
Code:
rm ./!(*.tex)
Your command will exclude all files that have one of the characters '.,t,e,x' as part of their filename.
Look up
Code:
man bash
Search for 'extglob'. Will hopefully clear things up a bit.
Not sure if this option is available on all shells. So the above given solutions should be kept in mind.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.