0

we create the following command in order to remove only the empty folders that older then 100min

find /tmp -type  d -empty -mmin +100 -printf '%p was deleted!!\n' -delete

because we intend to run it from cron job on production machines

we want to understand if empty flag will also ignore folders with links or empty files

or in other words how empty flag check the folder ?

  • Empty means empty. A directory which contains anything (symbolic links, other directories, regular files empty or not, device files, sockets, whatever) is not empty. – AlexP Dec 17 '18 at 23:32
  • @AlexP I would say that's an answer instead of a comment. If you post that and ping me, I'll come back and upvote... – Fabby Dec 17 '18 at 23:43
  • Test, test, test – Jeff Schaller Dec 17 '18 at 23:51
  • Test ? what you mean about test? ( it is difficult to cover all cases ) – yael Dec 17 '18 at 23:54
  • @yael I see you asking about safety and running code in production. Have a Test system and create test cases and & etc. No one here can test your environment like you can. – Jeff Schaller Dec 18 '18 at 0:30
4

Empty means empty. If there is any sort of file in a directory, then the directory isn't empty. To illustrate:

$ mkdir dir{1..8}; \
  ln -s /etc/ dir1/workingLink; \
  ln -s noSuchFile dir2/brokenLink;  \
  mkfifo dir3/fifo; 
  touch dir4/emptyFile; \
  echo foo > dir5/nonEmptyFile; \
  touch dir6/.hiddenFile; \
  mkdir dir7/subdir
$ tree -a
.
├── dir1
│   └── workingLink -> /etc/
├── dir2
│   └── brokenLink -> noSuchFile
├── dir3
│   └── fifo
├── dir4
│   └── emptyFile
├── dir5
│   └── nonEmptyFile
├── dir6
│   └── .hiddenFile
├── dir7
│   └── subdir
└── dir8

So, we have a directory with a working symlink, one with a broken link (pointing to a non-existant file), one with a FiFo (a named pipe), one with an empty file, one with a file that isn't empty, one with a hidden file, one with a subdirectory and only one that's empty. Which one will be deleted?

$ find . -type d -empty -printf '%p WAS DELETED!\n' -delete \
                        -or -printf '%p: not empty!\n'
./dir8 WAS DELETED!
./dir3/fifo: not empty!
./dir3: not empty!
./dir5/nonEmptyFile: not empty!
./dir5: not empty!
./dir6/.hiddenFile: not empty!
./dir6: not empty!
./dir2/brokenLink: not empty!
./dir2: not empty!
./dir4/emptyFile: not empty!
./dir4: not empty!
./dir7/subdir WAS DELETED!
./dir7 WAS DELETED!
./dir1/workingLink: not empty!
./dir1: not empty!
.: not empty!

So, three things were deleted:

  1. The empty directory dir8.
  2. The empty (sub) directory dir7/subdir
  3. The (now) empty directory dir7. This was deleted because find first deleted dir7/subdir and then dir7 was empty so that was deleted as well.

So, any type of content in a directory will stop it from being deleted by this command, but you need to be careful in case the only thing in a directory is other, empty, directories. In that case, the find command will also delete the parent directory since it will be empty by the time it finishes.

If you don't want to remove subdirectories which might cause a parent to be removed, with GNU find (the default on Linux) you can use the -maxdepth flag to limit find to only the current directory:

$ find . -maxdepth 1 -type d -empty -printf '%p WAS DELETED!\n' \
                                    -delete -or -printf '%p: not empty!\n'
./dir8 WAS DELETED!
./dir3: not empty!
./dir5: not empty!
./dir6: not empty!
./dir2: not empty!
./dir4: not empty!
./dir7: not empty!
./dir1: not empty!
.: not empty!
| improve this answer | |

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.