Answer by Epscillon Rules for Remove all files except some from a directory
The answer I was looking for was to run script, but I wanted to avoid deleting the sript itself. So incase someone is looking for a similar answer, do the following.Create a .sh file and write the...
View ArticleAnswer by Gowtam Thakur for Remove all files except some from a directory
I belive you can userm -v !(filename)Except for the filename all the other files will e deleted in the directory and make sure you are using it in
View ArticleAnswer by mik-mak for Remove all files except some from a directory
Remove everything exclude file.name:ls -d /path/to/your/files/* |grep -v file.name|xargs rm -rf
View ArticleAnswer by 18446744073709551615 for Remove all files except some from a directory
Since no one yet mentioned this, in one particular case:OLD_FILES=`echo *`... create new files ...rm -r $OLD_FILES(or just rm $OLD_FILES)orOLD_FILES=`ls *`... create new files ...rm -r $OLD_FILESYou...
View ArticleAnswer by L'alligator des abers for Remove all files except some from a...
Make the files immutable. Not even root will be allowed to delete them.chattr +i textfile.txt backup.tar.gz script.php database.sql info.txtrm *All other files have been deleted.Eventually you can...
View ArticleAnswer by Nick Tsai for Remove all files except some from a directory
I prefer to use sub query list:rm -r `ls | grep -v "textfile.txt\|backup.tar.gz\|script.php\|database.sql\|info.txt"`-v, --invert-match select non-matching lines\| SeparatorTo avoid preserving files...
View ArticleAnswer by wyx for Remove all files except some from a directory
Just:rm $(ls -I "*.txt" ) #Deletes file type except *.txtOr:rm $(ls -I "*.txt" -I "*.pdf" ) #Deletes file types except *.txt & *.pdf
View ArticleAnswer by Leonardo Hermoso for Remove all files except some from a directory
You can do this with two command sequences.First define an array with the name of the files you do not want to exclude:files=( backup.tar.gz script.php database.sql info.txt )After that, loop through...
View ArticleAnswer by sudo bangbang for Remove all files except some from a directory
If you're using zsh which I highly recommend.rm -rf ^file/folder pattern to avoidWith extended_globsetopt extended_globrm -- ^*.txtrm -- ^*.(sql|txt)
View ArticleAnswer by juanfal for Remove all files except some from a directory
Trying it worked with:First and foremost, activate extglob (on bash, on zsh the syntax is quite similar)shopt -s extglobthenrm -r !(Applications|"Virtualbox VMs"|Downloads|Documents|Desktop|Public)but...
View ArticleAnswer by Daniel Chen for Remove all files except some from a directory
This is similar to the comment from @siwei-shen but you need the -o flag to do multiple patterns. The -o flag stands for 'or'find . -type f -not -name '*ignore1' -o -not -name '*ignore2' | xargs rm
View ArticleAnswer by lhuber for Remove all files except some from a directory
A little late for the OP, but hopefully useful for anyone who gets here much later by google...I found the answer by @awi and comment on -delete by @Jamie Bullock really useful. A simple utility so you...
View ArticleAnswer by pl1nk for Remove all files except some from a directory
rm !(textfile.txt|backup.tar.gz|script.php|database.sql|info.txt)The extglob (Extended Pattern Matching) needs to be enabled in BASH (if it's not enabled):shopt -s extglob
View ArticleAnswer by Ajeesh for Remove all files except some from a directory
Rather than going for a direct command, please move required files to temp dir outside current dir. Then delete all files using rm * or rm -r *.Then move required files to current dir.
View ArticleAnswer by gniourf_gniourf for Remove all files except some from a directory
Since nobody mentioned it:copy the files you don't want to delete in a safe placedelete all the filesmove the copied files back in place
View ArticleAnswer by theharshest for Remove all files except some from a directory
You can use GLOBIGNORE environment variable in Bash.Suppose you want to delete all files except php and sql, then you can do the following -export GLOBIGNORE=*.php:*.sqlrm *export GLOBIGNORE=Setting...
View ArticleAnswer by Dennis Williamson for Remove all files except some from a directory
Assuming that files with those names exist in multiple places in the directory tree and you want to preserve all of them:find . -type f ! -regex...
View ArticleAnswer by mishunika for Remove all files except some from a directory
You can write a for loop for this... %)for x in *do if [ "$x" != "exclude_criteria" ] then rm -f $x; fidone;
View ArticleAnswer by awi for Remove all files except some from a directory
find [path] -type f -not -name 'textfile.txt' -not -name 'backup.tar.gz' -deleteIf you don't specify -type f find will also list directories, which you may not want.Or a more general solution using the...
View ArticleAnswer by darioo for Remove all files except some from a directory
find . | grep -v "excluded files criteria" | xargs rmThis will list all files in current directory, then list all those that don't match your criteria (beware of it matching directory names) and then...
View Article