LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 01-14-2013, 11:48 AM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Rep: Reputation: Disabled
remove all but command


is there an option with rm that i can't find that will remove all files in a directory but one file type? say i have files in a directory

Code:
file1.dat
file2.csv
file3.csv
file4.csv
file5.txt
file6.txt
file7.tar
etc.
and i want to get rid of everything except the "txt" files

so i could do

Code:
rm *.dat
rm *.csv
rm *.tar
but in psuedo code i'd like to do something like

Code:
rm *!.txt
i know this doesn't work. in ls there is an "I" option that might help, so maybe something like

Code:
ls -I .txt | rm
but i'm missing something?

Tabitha
 
Old 01-14-2013, 12:03 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
I would use the find command for that, like this:
Code:
find . -maxdepth 1 -type f ! -name "*.txt"  -exec rm '{}' +
This will remove everything but files ending with .txt.
Short explanation: Find will look in the current directory (indicated py the parameter "."), withouth looking into subdirectories (-maxdepth 1) for files of the type file (no directories, option -type f) that have not a name that matches to "*.txt" (! -name "*.txt") and after assembling those filenames it will invoke rm to delete them (-exec rm '{}' +).
Looks complicated at first, but once you have grasped the concepts of find you don't want to miss that command.
For more info on find look at
Code:
man find

Last edited by TobiSGD; 01-14-2013 at 12:06 PM.
 
Old 01-14-2013, 12:14 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by atjurhs View Post
i know this doesn't work. in ls there is an "I" option that might help, so maybe something like

Code:
ls -I .txt | rm
but i'm missing something?

Tabitha
close...

Code:
ls -I "*.txt" | xargs rm
Or you can do it using find with -exec like Tobi suggested
 
Old 01-14-2013, 12:20 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Tabitha, actually there is something very similar to your pseudo-code, but you have to enable the extglob shell option:
Code:
shopt -s extglob
Then you can use extended pattern matching to match anything except a given pattern:
Code:
rm !(*.txt)
This is documented here: http://www.gnu.org/software/bash/man...ttern-Matching toward the end of the paragraph. Hope this helps.
 
2 members found this post helpful.
Old 01-14-2013, 12:30 PM   #5
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Is the shopt command Bash only? I get a "command not found" when trying that in zsh.
 
Old 01-14-2013, 12:33 PM   #6
atjurhs
Member
 
Registered: Aug 2012
Posts: 311

Original Poster
Rep: Reputation: Disabled
thanks guys!
 
Old 01-14-2013, 12:34 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by TobiSGD View Post
Is the shopt command Bash only? I get a "command not found" when trying that in zsh.
Yes, it's a bash built-in. If I remember well in zsh you have setopt. Anyway, you can do
Code:
rm ^*.txt
by default in zsh!
 
1 members found this post helpful.
Old 01-14-2013, 12:49 PM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by colucix View Post
Yes, it's a bash built-in. If I remember well in zsh you have setopt. Anyway, you can do
Code:
rm ^*.txt
by default in zsh!
Doesn't work here:
Code:
tobi ~/test ☺ $ ls 
test  x.txt
tobi ~/test ☺ $ rm ^*.txt
rm: cannot remove ‘^*.txt’: No such file or directory
EDIT: Nevermind, you are right, setopt is the command in zsh and I have to use it to set EXTENDED_GLOB for the rm command to work with ^.

Last edited by TobiSGD; 01-14-2013 at 12:58 PM.
 
  


Reply



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
remove command not working gazman1 Programming 7 06-12-2006 11:20 AM
how do I remove this startup Command? kushalkoolwal Debian 5 05-17-2006 06:48 PM
Terminal command for GUI command "Remove to trash" GunSkit Linux - General 3 06-29-2004 01:50 PM
Command to remove a directory... xconspirisist Linux - Software 2 11-28-2003 01:06 PM
what is the command to remove the directory Harish_f Linux - General 6 06-19-2002 03:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:00 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
Open Source Consulting | Domain Registration