Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 295
xiamx Avatar asked Nov 28 '22 10:11

xiamx


2 Answers

$ 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
like image 35
miku Avatar answered Dec 06 '22 09:12

miku


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
like image 115
Juliano Avatar answered Dec 06 '22 09:12

Juliano