简体   繁体   中英

Need a shell script that deletes all files except *.pdf

任何人都可以编写一个shell脚本来删除文件夹中除pdf扩展名以外的所有文件吗?

This will include all subdirectories:

find . -type f ! -iname '*.pdf' -delete

This will act only in the current directory:

find . -maxdepth 1 -type f ! -iname '*.pdf' -delete
$ ls -1 | grep -v '.pdf$' | xargs -I {} rm -i {}

Or, if you are confident:

$ ls -1 | grep -v '.pdf$' | xargs -I {} rm {}

Or, the bulletproof version:

$ find . -maxdepth 1 -type f ! -iname '*.pdf' -delete

This should do the trick:

shopt -s extglob
rm !(*.pdf)
ls | grep -v '.pdf$' | xargs rm

这将过滤所有不以PDF结尾的文件,并对其执行RM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM