LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   remove all but command (https://www.linuxquestions.org/questions/linux-newbie-8/remove-all-but-command-4175445563/)

atjurhs 01-14-2013 11:48 AM

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

TobiSGD 01-14-2013 12:03 PM

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

suicidaleggroll 01-14-2013 12:14 PM

Quote:

Originally Posted by atjurhs (Post 4869798)
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

colucix 01-14-2013 12:20 PM

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.

TobiSGD 01-14-2013 12:30 PM

Is the shopt command Bash only? I get a "command not found" when trying that in zsh.

atjurhs 01-14-2013 12:33 PM

thanks guys!

colucix 01-14-2013 12:34 PM

Quote:

Originally Posted by TobiSGD (Post 4869832)
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! :jawa:

TobiSGD 01-14-2013 12:49 PM

Quote:

Originally Posted by colucix (Post 4869838)
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! :jawa:

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 ^.


All times are GMT -5. The time now is 01:04 PM.