Pages

08 November 2012

Delete nested empty folders.

This is how to delete any empty folders that are below a certain point on a linux filesystem. It will not remove any folder that still has files or folders in them. To show how it works I will create a few sub folders under my home folder, and then create some files in a couple of them (to show those folders won't be deleted). Then I will remove only those folders that are empty.

The command used is "find -depth -type d -empty -exec rmdir {} \;". I will briefly explain what this does before actually showing you below.
  •     find is a standard linux/unix command (man find) to learn more.
  •     The -depth tells find to work from the deepest folder upwards i.e if you have the following folder structure "/home/clive/mydir1/mydir2/mydir3/mydir4" it will start at mydir4, then go up to mydir3, then up to mydir2 etc...
  •     The -type d tells find that you are looking only for folders.
  •     The -empty tells find that you are looking for empty folders.
  •     The -exec rmdir {} \; tells find to delete any empty folders it finds.

N.B. If you do not use the -depth option it will work the other way down (mydir1 -> mydir2 -> mydir3 -> mydir4), and will therefore only delete mydir4, because it will see that mydir1, mydir2 and mydir3 are not empty because they contain other folders/files, it will then only delete mydir 4 - as this is the only empty folder.


Now on to the actual example:
clive@dogmatix > cd ~
clive@dogmatix > pwd
/home/clive

clive@dogmatix > mkdir rmdir_example
clive@dogmatix > cd rmdir_example/
clive@dogmatix > mkdir -p subdir{01,02,03}/subdira
clive@dogmatix > mkdir -p subdir01/subdirb
clive@dogmatix > mkdir -p subdir{01,02,03}/subdira/another/stillmore/isempty
clive@dogmatix > mkdir -p subdir01/subdirb/another/empty/directory   
clive@dogmatix > mkdir -p subdir02/subdira/folder/hasfiles
clive@dogmatix > mkdir -p subdir03/empty/folder
clive@dogmatix > echo "This is a file" > subdir01/subdira/another/stillmore/myfile01.txt
clive@dogmatix > echo "This is a file" > subdir02/subdira/folder/hasfiles/myfile02.txt
clive@dogmatix > tree
.
+-- subdir01
¦   +-- subdira
¦   ¦   +-- another
¦   ¦       +-- stillmore
¦   ¦           +-- isempty
¦   ¦           +-- myfile01.txt
¦   +-- subdirb
¦       +-- another
¦           +-- empty
¦               +-- directory
+-- subdir02
¦   +-- subdira
¦       +-- another
¦       ¦   +-- stillmore
¦       ¦       +-- isempty
¦       +-- folder
¦           +-- hasfiles
¦               +-- myfile02.txt
+-- subdir03
    +-- empty
    ¦   +-- folder
    +-- subdira
        +-- another
            +-- stillmore
                +-- isempty
23 directories, 2 files


clive@dogmatix > find -depth -type d -empty -exec rmdir {} \;
clive@dogmatix > tree
.
+-- subdir01
¦   +-- subdira
¦       +-- another
¦           +-- stillmore
¦               +-- myfile01.txt
+-- subdir02
    +-- subdira
        +-- folder
            +-- hasfiles
                +-- myfile02.txt
8 directories, 2 files



As you can see, because the stillmore and hasfiles folders have files in them they were not deleted, all other empty folders have been deleted.

No comments:

Post a Comment