rm

Remove all files except some from a directory

馋奶兔 提交于 2019-11-27 05:46:48
When using sudo rm -r , how can I delete all files, with the exception of the following: textfile.txt backup.tar.gz script.php database.sql info.txt find [path] -type f -not -name 'textfile.txt' -not -name 'backup.tar.gz' -delete If you don't specify -type f find will also list directories, which you may not want. Or a more general solution using the very useful combination find | xargs : find [path] -type f -not -name 'EXPR' -print0 | xargs -0 rm -- for example, delete all non txt-files in the current directory: find . -type f -not -name '*txt' -print0 | xargs -0 rm -- The print0 and -0

Remove all files except some from a directory

杀马特。学长 韩版系。学妹 提交于 2019-11-26 11:44:43
问题 When using sudo rm -r , how can I delete all files, with the exception of the following: textfile.txt backup.tar.gz script.php database.sql info.txt 回答1: find [path] -type f -not -name 'textfile.txt' -not -name 'backup.tar.gz' -delete If you don't specify -type f find will also list directories, which you may not want. Or a more general solution using the very useful combination find | xargs : find [path] -type f -not -name 'EXPR' -print0 | xargs -0 rm -- for example, delete all non txt-files