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

白昼怎懂夜的黑 提交于 2019-12-04 13:32:46

问题


Can anyone write a shell script that deletes all the files in the folder except those with pdf extension?


回答1:


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



回答2:


$ 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



回答3:


This should do the trick:

shopt -s extglob
rm !(*.pdf)



回答4:


ls | grep -v '.pdf$' | xargs rm

This will filter all files that don't end in PDF, and execute RM on them



来源:https://stackoverflow.com/questions/4702577/need-a-shell-script-that-deletes-all-files-except-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!