How to delete empty files and directories recursively

We can use  find command to delete all empty files and directories recursively

To delete empty files

To delete only empty directories

If you omit filter -type, it will delete both directories and files.

You can control the how deep the find  command can go and delete the empty directories and files.

Let’s say I have this directory tree

Directory Tree
Directory Tree

As you can see, we have 3 levels of directories (and files). All are empty, this is to give you better illustration.

To control how deep we search to delete empty files and directories and from which level we should delete. We would use the following two options

-maxdepth levels
Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0
means only apply the tests and actions to the command line arguments.

-mindepth levels
Do not apply any tests or actions at levels less than levels (a non-negative integer). -mindepth 1 means process all files
except the command line arguments.

These options (-maxdepth and -mindepth) should be provided immediately after the last path.

The simple find command below will print all files till level 3

Let’s say if you want to delete only the deepest or very last directory and files which are empty. You can do so,

You can delete them by adding -delete action to above command

Apparently only a/b/c/filec.txt and a/b/fileb.txt will be delete because the director c is not empty.

The command find is great utility that every linux geek should know.

How to delete files older than specified number of days using find command

The Linux command utility find will allow us to perform arbitrary commands on files which are filtered by the command.  Using this opportunity we can delete the files which are older than specified days by passing either command or action to find command.

Find command syntax would look like,

How to delete files older than specified days

To delete files which are older than specified number of days. We have to filter those files using criteria with action which will delete those files.

We can filter the files which are older than specified number of days using test -mtime.  Filtered files can b deleted using either of the following actions  -exec or -delete.

The command we need would look like,

find <path-to-files> -mtime +n -exec rm {} \;

or

find <path-to-files> -mtime +n -delete

Where,
n specifies number of days old that file should be to get not filtered(to get included in output)

Let’s consider an example to  delete files which are older than 7 days.

Example: Delete files older than 7 days

or, as mentioned we can also use -delete action.

As you can in the above command. We also mention other filters(tests) like -name along with -mtime to control what files should be deleted.

Break Down Of Command

First argument:  this could be either absolute path or relative path or wildcard specifies that where to search for files

Second argument: this is the criteria to filter files based on name, path, pattern and how many number of days older etc. More tests can be added to reduce final outcome of files.  Here we used -mtime +7 to filter all files which are older than 7 days.

Third argument: this argument will be the action. Which specifies what action should be preformed on found files. By default it is -print, means it will just print the result. As per our requirement we are using action -delete or -exec to delete files

Action -exec this is generic action, which can be used to perform any shell command on each file which is being located. Here use are using rm {} \;  Where {} represents the current file, it will expand to the name/path of found file.
Note: There is space between {} and \. If you omit this space it will throw and error.

🙂