1

I want to delete subfolders, only if all its files are of some specific extension, say json. I have seen a similar question Find and copy directories containing file type. Thanks in advance.

├── 20101018_DSC05369.JPG
├── f
│   └── some.jpg
├── f1
│   ├── f
│   │   └── some.json
│   ├── f4
│   │   ├── a.jpg
│   │   └── some.json
│   └── some.json
└── some.jpg


exp. result:
.
├── 20101018_DSC05369.JPG
├── f
│   └── some.jpg
├── f1
│   ├── f4
│   │   ├── a.jpg
│   │   └── some.json
│   └── some.json
└── some.jpg
  • What should happen if a given directory is elegible for deletion but has one or more subdirectories that not meet the requirement for deletion? – Paulo Tomé Dec 19 '19 at 23:18
  • it should not be deleted Paulo – Ferroao Dec 19 '19 at 23:50
1
#! /bin/bash
# recursively
shopt -s globstar || exit
# for every subfolder
for dir in **;do
      [[ ! -d $dir ]] && continue # if not directory then skip
      # SUBFOLDER name inspected
      echo "$dir"
      # count number of files in subfolder 
      NUMFILES=$(find "$dir" -type f | wc -l)
      # if non-empty folder
      if (( $(bc <<<"$NUMFILES > 0") )); then
      # if all files in folder have extension json, remove subfolder
              NUMJSON=$(find "$PWD/$dir" -iname '*.json' | wc -l)

              if (( $(bc <<<"$NUMFILES == $NUMJSON") )); then
                echo "rm -rf "$dir"" 
                rm -rf "$dir"
              fi
      fi
done
| improve this answer | |
0

The following one liner will solve your problem:

sudo find . -links 2 -exec sh -c '[ "$(ls -l \{} | grep -c '^-')" \
-eq "$(ls \{} | grep -c '\.json$')" ]' \; -print0 | xargs -0  echo rm -rf {} + 2> /dev/null \;

Explanation of the code:

sudo find . -links 2 

In the current directory will only return leaf directories.

 ls -l \{} | grep -c '^-'

returns the number of regular files in the directory.

 ls \{} | grep -c '\.json$'

returns the number of files with json extension in the directory.

sh -c '[ "$(ls -l \{} | grep -c '^-')" -eq "$(ls \{} | grep -c '\.json$')" ]'

spwans a sub-shell that returns true if all files in the directry have json extension, thus allowing the instruction to proceed.

rm -rf {} + 2> /dev/null

will delete the non-empty directory redirecting standard error to /dev/null.

Caveat: This solution will delete found folders that do not contain any regular file.

Please backup your data before executing tests.

Execute the code in the top directory.

Remove echo when your tests succeed.

| improve this answer | |
  • @Ferroao Please add the output from the comentary into the question to make it more readable. – Paulo Tomé Dec 20 '19 at 18:38
  • @Ferroao Check corrections performed. – Paulo Tomé Dec 23 '19 at 1:06
  • didnt work for me. seems this is not working sudo find . -links 2 – Ferroao Dec 23 '19 at 15:44
  • What does sudo find . -links 2 returns? – Paulo Tomé Dec 23 '19 at 15:45
  • nothing .. just nothing – Ferroao Dec 23 '19 at 17:29

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.