Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
08-13-2010, 11:17 AM
|
#1
|
|
Member
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101
Rep:
|
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!
|
|
|
|
08-13-2010, 02:35 PM
|
#2
|
|
Member
Registered: Jan 2008
Posts: 105
Rep:
|
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
do
rm -i $linevar
done < /tmp/toremove.txt
|
|
|
|
08-13-2010, 02:47 PM
|
#3
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,558
|
|
|
|
|
08-13-2010, 03:23 PM
|
#5
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Code:
sasha@reactor: find . -maxdepth 1 -type f ! -iname "*.tex" -delete
|
|
|
1 members found this post helpful.
|
08-13-2010, 03:57 PM
|
#6
|
|
Member
Registered: Aug 2010
Location: Atlanta
Distribution: ubuntu, redhat
Posts: 138
Rep:
|
To expand on valen tino's post:
rm -f `ls -l | grep -v .tex | awk '{print $9}'`
|
|
|
1 members found this post helpful.
|
08-13-2010, 04:35 PM
|
#7
|
|
Member
Registered: Sep 2009
Location: Houston Tx
Posts: 89
Rep:
|
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
|
|
|
1 members found this post helpful.
|
08-13-2010, 04:46 PM
|
#8
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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. 
|
|
|
1 members found this post helpful.
|
08-13-2010, 06:43 PM
|
#9
|
|
Member
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101
Original Poster
Rep:
|
That's great. Thanks everyone for the help!
|
|
|
|
08-13-2010, 06:54 PM
|
#10
|
|
Senior Member
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Rep:
|
Here's what I would consider:
Code:
rm -rf $(ls -l | grep -v .tex | awk '{print $9}')
This will remove all files listed by the command in the '$()'.
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}'"
rm -rf $TOREMOVE
Last edited by Kenny_Strawn; 08-13-2010 at 06:59 PM.
|
|
|
|
11-27-2010, 04:02 PM
|
#11
|
|
LQ Newbie
Registered: Nov 2010
Location: Madrid
Posts: 5
Rep:
|
Way To Complicated
|
|
|
0 members found this post helpful.
|
11-27-2010, 06:23 PM
|
#12
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Do NOT issue that command
Quote:
Originally Posted by madridlinuxgroup
Why don't you just man the rm command...
rm [!a]*
...
|
Not only does this NOT help with the initial problem, but it will also delete ALL files in the current folder that do not start with an 'a'.
|
|
|
|
11-28-2010, 01:45 PM
|
#13
|
|
LQ Newbie
Registered: Nov 2010
Location: Madrid
Posts: 5
Rep:
|
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
|
|
|
0 members found this post helpful.
|
11-29-2010, 10:40 AM
|
#14
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
Still wrong!
I guess what you actually mean is bash's extended globbing mechanism. But this is nothing specific to 'rm'.
Your command will exclude all files that have one of the characters '.,t,e,x' as part of their filename.
Look up
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.
Last edited by crts; 12-10-2010 at 01:59 PM.
|
|
|
|
12-10-2010, 01:48 PM
|
#15
|
|
LQ Newbie
Registered: Nov 2010
Location: Madrid
Posts: 5
Rep:
|
|
|
|
0 members found this post helpful.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:00 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|