1

I have tree of folders as follow:

parent folder
....folder A
....folder B
    ....folder 1
    ....folder 2
....folder C
    ....folder 1
    ....folder 2
....folder D
    ....folder 1
    ....folder 2
. 
.

Inside every folder I have a punch of files. These files have different extensions (zip, tar.gz, jpeg, bmp, csv, xlsx, doc, txt, ...etc)

  1. How can I delete all the files in the folders except the files *.jpeg.

  2. How can I delete all the files in the folders except more than one type of file ( e.g. except the files *.jpeg , *.csv)

In order to achieve this I was thinking of using the command:

find . type -f -exec  

But I don't know how to complete it!..

Any other ways to do it ?

  • 4
    find . -type f ! -name \*.jpeg to list them, add -delete if you're happy with the result – don_crissti Jul 19 '16 at 18:31
  • @don_crissti. Thanks! " -name *.jpeg" does it means list all the files except *.jpeg. Kindly what "!" stands for? if I want to exclude more than one type of files. How can I do it? Thank you very much for explaining it. – user88036 Jul 19 '16 at 18:34
  • 2
    ! negates the -name test... man find for more details; as to excluding more than one type... please edit your post and state your exact requirements, don't add them via comments – don_crissti Jul 19 '16 at 18:39
  • This has been asked before but I can't find the exact duplicate, anyway for more than one extension see derobert's find solution here, to remove the files just add -delete – don_crissti Jul 19 '16 at 18:49
0
find . type -f ! -name '*.jpeg' ! -name '*.csv' -delete

Read this as: traverse the current directory; when you find a file that is a regular file, and whose name does not match *.jpeg, and whose name is does not match *.csv, then delete it.

If your version of find doesn't have -delete, make find invoke the rm command: replacte -delete by -exec rm {} +.

In zsh (with setopt extended_glob turned on), *~(PATTERN) matches all files that don't match PATTERN. Use the glob qualifier D to include dot files and . to include only regular files. Change . to ^/ to delete all non-directories (except the ones matching the excluded patterns).

rm **/*~*.(jpeg|csv)(D.)

Your Answer

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