2

I want to remove all the photos from a folder and keep the remaining files. I searched in the web and I found a tons of solutions like:

find . -type f -name "*.jpg" -delete

All the photos are jpeg, yes, but the main problem is that the photo's names don't end with their extension ".jpg".

Could anyone help me?

  • Do you want to delete all of the files within a folder? or all JPEG files within the folder? or a some subset of the JPEG files? It would be helpful to give examples of what you want to remove and what (if anything) you want to keep. – steeldriver Jun 14 '15 at 0:04
  • You could probably use file command and check that returned value is JPEG image data. – Armen Jun 14 '15 at 0:30
  • @steeldriver " photo I want to remove all the photos from a folder and keep the remaining files" ( op, first line ) – Sergiy Kolodyazhnyy Jun 14 '15 at 0:32
  • @Serg I modified the op thanks to the suggestion made by steeldriver, but I forgotten to notified it. – tito Jun 14 '15 at 0:35
  • @Armen : Could you explain me how to do that? – tito Jun 14 '15 at 0:37
3

this should do

for i in *; do 
    TESTRESULT=$(file --mime-type -b "$i"); 
    if [ "$TESTRESULT" == "image/jpeg" ]; then 
        echo "$i"; 
    fi;
done

If that prints the desired files, replace echo "$i" with rm "$i"

  • could you explain me how your code work? – tito Jun 14 '15 at 1:12
  • for each file in directory check if its mimetype is jpeg if so echo its name (or if you replace echo with rm remove) – czapa.ols Jun 14 '15 at 1:15
1

Here is a script to remove all images:

userX@ubuntu:~$ cat removeImages.sh 
#!/bin/bash
path=/home/userX/Downloads/*;
for file in $path
do
  fileInfo=`file "$file" |awk -F: '{print $2}'`
  if [[ $fileInfo =~ .*image.* ]]; then echo "Removing: " $file; rm $file; fi
done

You use second part of file file "$file" output and look for image keyword in it, and then remove the current file in the loop.

If you also want to remove bitmaps:

userX@ubuntu:~$ cat removeImages.sh 
#!/bin/bash
path=/home/userX/Downloads/*;
for file in $path
do
  fileInfo=`file "$file" |awk -F: '{print $2}'`
  if [[ $fileInfo =~ .*image.*]] || [[$fileInfo =~ .*bitmap.* ]]; 
  then echo "Removing: " $file; rm $file; fi
done
1

Okay I have found a solution:

Try this command

find . -name "*.jpg" -delete

I tried this on Xubuntu 14.04 and it worked fine. I hope this helps!

I know this is similar to your example but the example you gave didn't work for me. But this is a simpler one liner.

  • Did you read the question? The OP mentions this solution and explains that their files don't have the right extensions. – terdon Jun 14 '15 at 9:59
  • Yes, Don't worry. I double checked the question after submitting my answer and realized that. But I thought I would leave it on here in-case someone stumbled upon this page thinking it was another question. – Eyovin Jun 14 '15 at 21:59

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.