LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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



Reply
 
Search this Thread
Old 08-13-2010, 12:17 PM   #1
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
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!
 
Old 08-13-2010, 03:35 PM   #2
valen_tino
Member
 
Registered: Jan 2008
Posts: 105

Rep: Reputation: 28
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
 
Old 08-13-2010, 03:47 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Have a look at bash's extended globbing feature.

http://www.linuxjournal.com/content/...ended-globbing
 
Old 08-13-2010, 04:12 PM   #4
PradeepKr
LQ Newbie
 
Registered: Aug 2010
Posts: 6

Rep: Reputation: 0
You can use xargs.


Something similar is seen here to find files having some pattern.
 
Old 08-13-2010, 04:23 PM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Code:
sasha@reactor: find .  -maxdepth 1 -type f ! -iname "*.tex" -delete
 
1 members found this post helpful.
Old 08-13-2010, 04:57 PM   #6
suprstar
Member
 
Registered: Aug 2010
Location: Atlanta
Distribution: ubuntu, debian
Posts: 142
Blog Entries: 2

Rep: Reputation: 21
To expand on valen tino's post:

rm -f `ls -l | grep -v .tex | awk '{print $9}'`
 
1 members found this post helpful.
Old 08-13-2010, 05:35 PM   #7
joec@home
Member
 
Registered: Sep 2009
Location: Houston Tx
Posts: 89

Rep: Reputation: 29
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.
Old 08-13-2010, 05:46 PM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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.
Old 08-13-2010, 07:43 PM   #9
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Original Poster
Rep: Reputation: 15
That's great. Thanks everyone for the help!
 
Old 08-13-2010, 07:54 PM   #10
Kenny_Strawn
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
Blog Entries: 62

Rep: Reputation: 55
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 07:59 PM.
 
Old 11-27-2010, 05:02 PM   #11
madridlinuxgroup
LQ Newbie
 
Registered: Nov 2010
Location: Madrid
Posts: 5

Rep: Reputation: 0
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
 
0 members found this post helpful.
Old 11-27-2010, 07:23 PM   #12
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Do NOT issue that command

Quote:
Originally Posted by madridlinuxgroup View Post
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'.
 
Old 11-28-2010, 02:45 PM   #13
madridlinuxgroup
LQ Newbie
 
Registered: Nov 2010
Location: Madrid
Posts: 5

Rep: Reputation: 0
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.
Old 11-29-2010, 11:40 AM   #14
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Still wrong!

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.

Last edited by crts; 12-10-2010 at 02:59 PM.
 
Old 12-10-2010, 02:48 PM   #15
madridlinuxgroup
LQ Newbie
 
Registered: Nov 2010
Location: Madrid
Posts: 5

Rep: Reputation: 0
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
 
0 members found this post helpful.
  


Reply

Tags
shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Use of uninitialized value in pattern match (m//) koshihaku Linux - Newbie 2 08-10-2010 02:07 PM
[SOLVED] Splitting files by pattern match bartonski Linux - Software 8 11-20-2009 11:05 AM
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 01:36 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 07:17 AM
Select the files of a directory that match a specific pattern jianelisj Linux - Newbie 2 03-17-2008 01:25 PM


All times are GMT -5. The time now is 03:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration