Reputation: 12122
i have a lot of different type of files in one folder. i need to delete the files but except the pdf file. I tried to display the pdf file only. but i need to delete the other than pdf files
ls -1 | xargs file | grep 'PDF document,' | sed 's/:.*//'
Upvotes: 1
Views: 901
Reputation: 5602
It's rare in my experience to encounter PDF files which don't have a .pdf extension. You don't state why "file" is necessary in the example, but I'd write this as:
# find . -not -name '*.pdf' -delete
Note that this will recurse into subdirectories; use "-maxdepth 1" to limit to the current directory only.
Upvotes: 0
Reputation: 12994
$ ls aa.txt a.pdf bb.cpp b.pdf
$ ls | grep -v .pdf | xargs rm -rf
$ ls a.pdf b.pdf
:) !
Upvotes: 2
Reputation: 468191
You could do the following - I've used echo rm
instead of rm
for safety:
for i in *
do
[ x"$(file --mime-type -b "$i")" != xapplication/pdf ] && echo rm "$i"
done
The --mime-type -b
options to file make the output of file easier to deal with in a script.
Upvotes: 2
Reputation: 67319
ls |xargs file|awk -F":" '!($2~/PDF document/){print $1}'|xargs rm -rf
Upvotes: 1
Reputation: 37929
Try inverting the grep match:
ls -1 | xargs file | grep -v 'PDF document,' | sed 's/:.*//'
Upvotes: 0