2

I have exercise, in which I have to delete all files, which are not jpeg.

I tried find -type f |xargs file| grep -iv 'jpeg', but it doesn't work.

6

Deleting based on file mimetype

To delete all non-jpeg regular files in the current directory or its subdirectories, use:

find . -type f -exec bash -c 'file -bi "$1" | grep -q image/jpeg || rm "$1"' none {} \;

This approach is safe for all file names. It will work even if the file names have newlines or other difficult characters in them.

How it works

  • find . -type f

    This starts a find command, restricting the files found to regular files, -type f.

  • -exec bash -c 'file -bi "$1" | grep -q image/jpeg || rm "$1"' none {} \;

    For all the files found, this runs a bash command to test the file's type. In particular, file -bi "$1" | grep -q image/jpeg will return true if file reports that the file has mimetype image/jpeg. The operator || assures that the rm command which follows is executed only for files which failed the jpeg test. Thus, all non-jpeg files are deleted.

Deleting based on file name

To delete all files whose names do not end in .jpeg:

find . -type f ! -name '*.jpeg' -delete

This approach is also safe for all file names. It will work even if the file names have newlines or other difficult characters in them.

How it works

  • find .

    Find all files in the current directory and its subdirectories

  • -type f

    Restrict ourselves only to regular files

  • ! -name '*.jpeg'

    -name '*.jpeg' would find all files whose names end in .jpeg. The exclamation mark, !, however, means negation. So, ! -name '*.jpeg' restricts our search to files whose names do not end in .jpeg.

  • -delete

    This tells find to delete the files that match the above criteria.

Testing

To test the command, leave off the -delete:

find . -type f ! -name '*.jpeg'

This will show you what files would be deleted when the -delete action is used.

  • No, no. There are no files "*.jpeg", they don't have format in name. I can know its name using 'file' command. – diego9403 Aug 15 '15 at 6:45
  • @adrian OK, OK. See updated answer. – John1024 Aug 15 '15 at 6:51
  • I didn't find answer. I have to add, that there are a lot of folders in folders, so "ls -l" is not enough.I thought about '-exec', but its accept only one commant and my favorite "|" doesn't work. – diego9403 Aug 15 '15 at 7:22
  • @adrian Look again: I did not use ls -l. I used find which will search all subfolders. Second, look at how I used -exec. The -exec command runs a bash shell and the bash shell fuilly supports features like | and ||. – John1024 Aug 15 '15 at 7:26
  • Ok, but I have to know what is the type of files ('jpeg'), that why i should use 'file', but that command make it difficult. After use 'file', i can't use rm to delete files. – diego9403 Aug 15 '15 at 7:53

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.