LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   rm files that do not match a given pattern (https://www.linuxquestions.org/questions/linux-newbie-8/rm-files-that-do-not-match-a-given-pattern-826029/)

elsheikhmh 08-13-2010 11:17 AM

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!

valen_tino 08-13-2010 02:35 PM

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


David the H. 08-13-2010 02:47 PM

Have a look at bash's extended globbing feature.

http://www.linuxjournal.com/content/...ended-globbing

PradeepKr 08-13-2010 03:12 PM

You can use xargs.


Something similar is seen here to find files having some pattern.

GrapefruiTgirl 08-13-2010 03:23 PM

Code:

sasha@reactor: find .  -maxdepth 1 -type f ! -iname "*.tex" -delete

suprstar 08-13-2010 03:57 PM

To expand on valen tino's post:

rm -f `ls -l | grep -v .tex | awk '{print $9}'`

joec@home 08-13-2010 04:35 PM

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

GrapefruiTgirl 08-13-2010 04:46 PM

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. :)

elsheikhmh 08-13-2010 06:43 PM

That's great. Thanks everyone for the help!

Kenny_Strawn 08-13-2010 06:54 PM

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


madridlinuxgroup 11-27-2010 04:02 PM

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

crts 11-27-2010 06:23 PM

Do NOT issue that command
 
Quote:

Originally Posted by madridlinuxgroup (Post 4172981)
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'.

madridlinuxgroup 11-28-2010 01:45 PM

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

crts 11-29-2010 10:40 AM

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.

madridlinuxgroup 12-10-2010 01:48 PM

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

crts 12-10-2010 02:05 PM

Quote:

Originally Posted by madridlinuxgroup (Post 4187310)
Did you actually try my solution?

Who? Me? Yes, I tried it with some dummy files. Results as expected. Did you try your solution?
Try it with the following filenames:
Code:

a1.tex
a2.tex
a3.tex
aa
vv
xt
a.t
a.b

What the OP wants is only for the three files a1.tex,a2.tex,a3.tex to NOT be deleted. Anything else can go. Your solution will not delete xt,a.t and a.b.

P.S.: However, I noticed a typo ('.text' instead of '.tex') in my previous post. Corrected it.

StevenXL 02-08-2014 09:52 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 4065529)
Code:

sasha@reactor: find .  -maxdepth 1 -type f ! -iname "*.tex" -delete

This is awesome - and I didn't know you could use -delete; I always used -exec rm -f {} \;

This is much better.


All times are GMT -5. The time now is 09:26 PM.