1. Introduction

Linux systems can accumulate junk files over time, taking up lots of space. Deleting such files can free up vital storage for new files and applications that we want to install in the future. Subsequently, this organizes and declutters our system, and hence enhances overall efficiency and productivity.

In this tutorial, we’ll learn different ways of deleting non-empty directories using the find command. While find primarily locates files and both empty and non-empty directories, it can also delete them when used in conjunction with the -delete option. As empty directories have less impact on improving system performance, our focus will be on non-empty directories only.

First, we’ll discuss the method to delete a single non-empty directory. After that, we’ll go over the method to delete multiple non-empty directories. Lastly, we’ll explore a method to find the path of a directory that needs to be deleted.

2. Deleting a Single Non-empty Directory

To begin with, we can use the find -delete command to delete a single directory no matter how many files or subdirectories it contains by following its general syntax:

$ find <Directory_Path> -delete

Here, we provide the path of the directory to be deleted using the find command along with the -delete flag.

Let’s suppose we want to delete a non-empty directory with the name dir1 inside the home directory. We’ll first navigate to the home directory using the cd command. Next, we’ll use the ls command to verify the directory isn’t empty:

$ ls dir1
file1.txt  file2.txt  file3.txt  subdir1  subdir2

The output of ls shows three files and two sub-directories.

Since we’re already in the home directory, we can directly use find to delete this non-empty directory:

$ find dir1 -delete

Actually, we can repeat the find command again to verify whether we successfully deleted the desired directory:

$ find dir1
find: ‘dir1’: No such file or directory

The output indicates that the find command isn’t able to locate the dir1 directory. We can also provide the complete path of the directory to delete it irrespective of our current location:

$ find /home/dir1/subdir1 -delete

The command deletes the subdir1 directory as we’re providing its complete path.

3. Deleting Multiple Non-empty Directories

Let’s suppose we have three different directories with the names dir1, dir2, and dir3.  To delete them, we can again use the find command:

$ find home/dir* -delete

Here the asterisk (*) acts like a wildcard within the Bash globbing mechanism. Using *, we find and delete all the directories and files containing “dir” present inside the home directory. This can sometimes produce unwanted results by deleting useful directories or files. Therefore, it’s generally a good idea to avoid this method unless we’re absolutely sure about the output.

So, to see the output without deleting each filesystem object, we can use the find command without -delete option:

$ find /home/dir* 
dir1
dir1/file1.txt
dir1/subdir1
dir2
dir2/file2.txt
dir2/subdir2
dir3
dir3/file3.txt
dir3/subdir3
directions
directions/north.txt

Now, we can provide a list of only those non-empty directories that require deletion:

$ find dir1 dir2 dir3 -delete 

Similarly, relative paths work here as well, especially if the directories are in different locations.

4. Deleting Non-empty Directories by Searching

If the path of the directory is unknown, then we can apply the original function of find recursively to the root of the filesystem:

$ find / -type d -name "dir1" 
/home/dir1
/var/Docs/dir1

Let’s break down this command:

  • /: the root (/) directory is the starting point as it contains all mounted filesystems
  • -type d: search only for directories
  • -name “dir1”: name of the directory to find

The output shows that there are two directories which can be either empty or non-empty with the name dir1 at two different locations. In fact, the command displays a list of complete paths of all the dir1 directories below the root. At this point, we can use the ls command again to see their content and then we can either delete both or one of these as we saw earlier.

5. Precautionary Measure While Using the -delete Option

We can also delete non-empty directories by using another variation of the find command:

$ find dir1 -delete -type d -name 'subdir1'
$ ls dir1
ls: cannot access dir1: No such file or directory

Let’s break down this command:

  • dir1: parent directory from where the find command will start locating
  • -type d: only search for directories
  • -name ‘subdir’: directory name to find

Here, the main pitfall is the placement of the -delete flag. As we’ve placed it in the third position, find immediately deletes dir1. Thus, if we only wanted to delete subdir1, the parent directory dir1 will also be deleted which probably isn’t what we want.

First, let’s display the content of the dir1 directory:

$ ls dir1
file1.txt  file2.txt  file3.txt  subdir1  subdir2

Now, let’s see what happens if we place the -delete flag at the last position:

$ find dir1 -type d -name 'subdir1' -delete
$ ls dir1
file1.txt  file2.txt  file3.txt  subdir2

This time, only the subdir1 has been deleted which is exactly what we wanted. Here, find first locates the subdir1 within the dir1 directory, and then the -delete option is applied.

6. Conclusion

In this article, we’ve discussed multiple methods to delete non-empty directories using the find -delete command. Basically, the command searches for desired directories and files, while its -delete option deletes them. The method works for single as well as multiple directories.

However, we should take care when deleting multiple directories using a (*) wildcard, since it risks deleting important data accidentally. Furthermore, we have also discussed the pitfall of the -delete option where its placement while executing the command matters a lot. It’s highly recommended to either make a backup or check the content of any data before deletion.

Comments are closed on this article!