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 so i could do Code:
rm *.dat Code:
rm *!.txt Code:
ls -I .txt | rm Tabitha |
I would use the find command for that, like this:
Code:
find . -maxdepth 1 -type f ! -name "*.txt" -exec rm '{}' + 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 |
Quote:
Code:
ls -I "*.txt" | xargs rm |
Tabitha, actually there is something very similar to your pseudo-code, but you have to enable the extglob shell option:
Code:
shopt -s extglob Code:
rm !(*.txt) |
Is the shopt command Bash only? I get a "command not found" when trying that in zsh.
|
thanks guys!
|
Quote:
Code:
rm ^*.txt |
Quote:
Code:
tobi ~/test ☺ $ ls |
All times are GMT -5. The time now is 01:04 PM. |