LinuxQuestions.org
Review your favorite Linux distribution.
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, 12:48 PM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 168

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, 01:03 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
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 01:06 PM.
 
Old 01-14-2013, 01:14 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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, 01:20 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 01:30 PM   #5
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

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

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

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 01:49 PM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
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 01:58 PM.
 
  


Reply


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


All times are GMT -5. The time now is 03:48 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration