开发者

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

Can anyone write a shell script that deletes all the file开发者_Go百科s in the folder except those with pdf extension?


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜