Finding empty directories

73,449

Solution 1

Check whether find <dir> -type f outputs anything. Here's an example:

for dir in A B C; do
    [ -z "`find $dir -type f`" ] && echo "$dir is empty"
done

Solution 2

It depends a little on what you want to do with the empty directories. I use the command below when I wish to delete all empty directories within a tree, say test directory.

find test -depth -empty -delete

One thing to notice about the command above is that it will also remove empty files, so use the -type d option to avoid that.

find test -depth -type d -empty -delete

Drop -delete to see the files and directories matched.

If your definition of an empty directory tree is that it contains no files then you be able to stick something together based on whether find test -type f returns anything.

find is a great utility, and RTFM early and often to really understand how much it can do :-)

Solution 3

You can use the following command:

find . -type d -empty

Solution 4

find directory -mindepth 1 -type d -empty -delete

This is the version that I found most interesting. If executed from inside directory, it will delete all empty directories below (a directory is considered empty if it only contains empty directories).

The mindepth option prevents the directory itself from being deleted if it happens to be empty.

Solution 5

find . -type d -empty

finds and lists empty directories and sub-directories in the current tree. E.g. resulting list of empty dirs and subdirs:

./2047
./2032
./2049
./2063
./NRCP26LUCcct1/2039
./NRCP26LUCcct1/2054
./NRCP26LUCcct1/2075
./NRCP26LUCcct1/2070

No operation is made on the directories. They are simply listed. This works for me.

Share:
73,449

Related videos on Youtube

soField
Author by

soField

System Integrator , Plsql, Java and Python Django developer

Updated on July 08, 2022

Comments

  • soField
    soField almost 2 years

    I need to find empty directories for a given list of directories. Some directories have directories inside it.

    If inside directories are also empty I can say main directory is empty otherwise it's not empty.

    How can I test this?

    For example:

    A>A1(file1),A2 this is not empty beacuse of file1
    B>B1(no file) this is empty
    C>C1,C2 this is empty
    
  • Alex S
    Alex S about 14 years
    -empty only works if the current directory is completely empty, not if the subtree contains no files.
  • Alex S
    Alex S about 14 years
    No, it will not output subdirectories. That's what the -type f is for.
  • mosg
    mosg about 14 years
    @soField Tested find . -type d -empty with FreeBSD & CentOS. Works fine. What your OS?
  • Admin
    Admin about 14 years
    the op asks for test/test2 to be returned also
  • YasirA
    YasirA about 14 years
    Howerver if there are empty files, your solution won't yield the message.
  • soField
    soField about 14 years
    hp-ux so there is no empty parameter
  • mosg
    mosg about 14 years
    @soField So, you see, that it's better (for all of us) to post this data at the top of your question...
  • Ukko
    Ukko about 14 years
    Yasir: I would think that a directory containing an empty file wound not be empty itself. Would that not be the correct result?
  • devdavid
    devdavid about 14 years
    Indeed. But I figured he could read the find man page to go further. A script that adjusts depths based on results would work just fine.
  • xslash
    xslash about 13 years
    find doesn't have -empty on solaris either, but it does have -depth to enforce depth-first traversal so that empty subdirectories can be deleted before the parent directory is checked for emptiness.
  • SO Stinks
    SO Stinks almost 13 years
    -delete implies -depth (at least for GNU findutils 4.4.2)
  • P. Dmitry
    P. Dmitry over 12 years
    You can follow this up with rmdir --ignore-fail-on-non-empty -p directory to get rid of the parents as well.
  • Alex S
    Alex S over 10 years
    @akostadinov: If I create the directories and files as indicated by the OP — mkdir A B C A/A1 A/A2 B/B1 C/C1 C/C2; touch A/A1/file1 — and run the code in my answer, it reports that B and C are empty, as required by the OP. So you will need to be a little more specific than, "it wont work".
  • akostadinov
    akostadinov over 10 years
    @MarceloCantos, I said everything needed to reproduce. Having your script work with some example data, does not prove it correct in all corner cases... or course I'm not telling this could be a problem for the OP's use case.
  • Alex S
    Alex S over 10 years
    In the example, B1, C1, and C2 are empty directories for $dir values B and C, and it clearly works for them. I'm not saying you're wrong — there possibly is a bug in my code — but it most certainly isn't clear from your comments what the problem is. Perhaps you could supply an example that fails.
  • Alex S
    Alex S about 10 years
    @akostadinov: I just noticed that I didn't address the above response to you, so here's a ping.
  • akostadinov
    akostadinov about 10 years
    @MarceloCantos, looks like I misread the question. It's very ugly lacking punctuation... sorry for confusion. I think your answer is nice.
  • Andreas
    Andreas almost 10 years
    This was the most useful version for mye use, thanks
  • SO Stinks
    SO Stinks almost 9 years
    @Pete Peterson My command seems to remove directories that only contain other empty directories (which is what OP also seems to want). I note that your command will usually leave the user in a directory with a missing i-node even if they accomplish what they want, which may be confusing to them. Maybe I'm missing the point.
  • Raffi
    Raffi over 8 years
    did not know about -empty, so convenient!
  • anthony
    anthony over 8 years
    However on machine that are not the latest and greatest linux. (EG Solaris), then you have no fancy find features like -empty or -delete.
  • Martin
    Martin over 8 years
    @antofthy been a while since I touched Solaris, but you used to have a choice between Sun's own version and the GNU version of various utilities. GNU tools was prefixed with g so perhaps gfind might work
  • anthony
    anthony over 8 years
    Something I have done at times, for general clean up. Also done things like rmdir */*/* */* * Though that does not handle 'dot-files' but then in manual situations (cleaning data files) you generally don't expect 'dot-files'. The recursive methods, however are for more automated general clean up, especially in very large directory structures.
  • anthony
    anthony over 8 years
    In one case was more complicated as I needed to remove empty directories where 'empty' included directories that could contain a single 'readme' file. Anything else in the directory and it wasn't 'empty'. In that example the directory structure involved tens of thousands of sub-directories.
  • Olie
    Olie about 8 years
    On MacOS-X, for almost empty directories, run find test -name ".DS_Store" -delete first, then the -empty delete command. Also note, like to pushd to the parent directory, so that find test becomes find ., but the rest of the commands are the same.
  • Michal aka Miki
    Michal aka Miki about 8 years
    @Martin Can you please add the definition of -depth here i.e. Cause find to perform a depth-first traversal, i.e., directories are visited in post-order and all entries in a directory will acted on before the directory itself. I think it is relevant here.
  • OmarOthman
    OmarOthman about 7 years
    If you are a Bash noob like me, check what -z means here.
  • Znik
    Znik about 6 years
    hp-ux curreltny is completly exotic system, usually based on intel itanium cpu. try find GNU tools with its find command.
  • hpavc
    hpavc almost 6 years
    You can mix actions as well, such as adding and -ls or -print "find . -depth -type d -empty -ls -delete"
  • kittygirl
    kittygirl over 5 years
    @LéoLéopoldHertz준영,what's the difference between add -depth or not in this question?
  • Ethan Hohensee
    Ethan Hohensee about 5 years
    rmdir -vp helped me immensely since I needed to not just delete all empty dirs under a root, but check for empty dirs and parent dirs after deleting a file. In my situation empty dirs are fine as long as they are not in the tree i'm currently removing a file from. Thanks!
  • myol
    myol over 3 years
    Be careful in what order you use -delete and -type d with -o unless you want to loose a few hours googling ext4magic and loose a few gigs of data still to be backed up.