Which command lets you delete subdirectories in addition to files in the current directory?

This Linux tutorial explains how to remove files recursively using different available techniques.

After reading this article, you will be able to find and remove single or multiple files from the command line. This tutorial is optimized for both new and experienced Linux users.

The first section of this tutorial explains how to remove files recursively (directories with all their content and subdirectories’ content). Below I also added instructions to remove recursively certain types of files depending on their size, extension, creation or modification time, and permissions.

All practical examples in this document contain screenshots to make it easy for every Linux user to understand and apply them to their needs.

Deleting all files recursively in Linux

The first section shows how to use the rm (Remove) command to delete a directory with all its content, including all subdirectories with their files and additional subdirectories.

The rm command used with -r flag will remove all directories’ content independently of their type.

But first, let’s see the directories in my home using the ls command.

 

Which command lets you delete subdirectories in addition to files in the current directory?

As you can see, I have 5 directories: Desktop, dir2, Documents, Downloads, and removerecurdir.

Let’s see the content of the directory named removerecurdir using the command tree as shown in the screenshot below.

 

Which command lets you delete subdirectories in addition to files in the current directory?

According to the tree output the removerecurdir directory contains two directories that contain subdirectories and a file inside removecurdir: The directory dir1, with otherdir and otherdir2 subdirectories, and the directory dir2 contains a file named file3.

Let’s say we want to remove the removecurdir and all its content including all files and subdirectories. The proper command is the rm command followed by the -r flag as shown in the syntax below.

 
Thus, if I want to remove the removerecurdir with all the content, I run:

 

Which command lets you delete subdirectories in addition to files in the current directory?

The subsequent ls output shows the directory, and all its content were successfully removed.

This section shows how to recursively delete files smaller than 10 megabytes using the command find.

The syntax is the following:

find <ParentDirectory> -type f -size -<size and units> -exec rm {} +

 
Note that in the example below, I use sudo to get privileges to remove protected files.

sudo find /var/log -type f -size -10M -exec rm {} +

 

Which command lets you delete subdirectories in addition to files in the current directory?

The syntax to remove files greater than a specific size is very similar. The minus (-) symbol must be replaced by a plus symbol (+). The exact syntax is shown below.

find <ParentDirectory> -type f -size +<size and units> -exec rm {} +

 
In the example below I will use the previous syntax to remove files greater than 1 GB.

find /var/log -type f -size +1G -exec rm {} +

 

Which command lets you delete subdirectories in addition to files in the current directory?

How to remove files recursively by extension (File type)

The current chapter explains how to delete files recursively by extension or file type.

On my home I have a directory named testhint. Let’s see its content using the tree command.

 

Which command lets you delete subdirectories in addition to files in the current directory?

As you can see, the parent directory testhint contains a file (file1.txt) and two subdirectories: testhint2 containing file3.txt and the testhint3 subdirectory containing file3 and something.txt.

Let’s assume you want to recursively remove all txt files only. The syntax is the following:

find <ParentDirectory> -type f -name '*.<Extension>' -print -delete

 
Thus, to remove all txt files recursively within the parent directory testhint, I run the command shown in the figure below.

find ~/testhint -type f -name '*.txt' -print -delete

 

Which command lets you delete subdirectories in addition to files in the current directory?

As you can see all txt files were removed, and only file3 without an extension remains.

 

Which command lets you delete subdirectories in addition to files in the current directory?

You can also delete files by extension using find together with exec commands, as I will explain below.

Let’s see a new scenario with the same directory structure but different files.

Which command lets you delete subdirectories in addition to files in the current directory?

The above image shows 4 log files and 3 files without extension.

The syntax to remove files by extensions using -exec is the following:

find <ParentDirectory> -type f -name '*.<Extension>' -exec rm -f {} \;

 
Thus, to remove the .log files from the previous screenshot, I ran the command below.

find ~/testhint -type f -name '*.log' -exec rm -f {} \;

 

Which command lets you delete subdirectories in addition to files in the current directory?

The image above shows all .log files were deleted while other files remained.

The xargs command offers the same solution. The difference between xargs and exec is that exec runs the rm function every time a file matches the condition. The command xargs executes the rm command once for all found files matching the condition.

The syntax to remove all files by extension with find and xargs is the following:

find <ParentDirectory> -name "*.<FileExtension>" -print0 | xargs -0 rm

 
The new scenario depicted in the screenshot below shows five .c files in different subdirectories and five files without the .c extension.

Which command lets you delete subdirectories in addition to files in the current directory?

To remove all .c files using xargs I run the command as shown below.

find . -name "*.c" -print0 | xargs -0 rm

 

Which command lets you delete subdirectories in addition to files in the current directory?

Again, you can see the selected extension files were successfully deleted.

Deleting all files recursively based on permissions

Let’s check the new content of the testhint directory.

There are four files with full permissions (file2, file3.c, file6.c and file7).

Which command lets you delete subdirectories in addition to files in the current directory?

Now let’s assume you want to find and remove all files with full permissions for everyone.

The syntax is the following:

find <ParentDirectory> -perm <Permissions> -print0 | xargs -0 rm

 
Thus, to remove all files with full access to all users, I execute the command below.

find ~/testhint -perm 777 -print0 | xargs -0 rm

 

Which command lets you delete subdirectories in addition to files in the current directory?

How to delete files recursively based on modification or creation time

The last section of this tutorial explains how to delete files recursively by creation or modification time.

The syntax is the following:

find ~/testhint -perm 777 -print0 | xargs -0 rm

 
If you want to delete files created or modified in the last day (last 24 hours), run the following command, where 1 is the number of days, and the minus (-) symbol specifies files created or modified before the defined number of days.

find <Directory> -type f -mtime -1 -delete

 
To remove files created or modified before a day, before 24 hours, just replace the minus symbol for a plus symbol.

find <Directory> -type f -mtime +1 -delete

Conclusion

Since Linux is a very versatile and flexible operating system, users have different techniques to get the same result. All the alternatives explained above are valid for almost every Linux distribution. Some of the commands are even useful for some Unix systems. As you can see, implementing them is easy and any Linux user can do it independently of their knowledge level. To delete files recursively according to other conditions, check the main page of each command described in this article.

About the author

Which command lets you delete subdirectories in addition to files in the current directory?

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

Which command removes subdirectories and files in the current directory?

If the directory still contains files or subdirectories, the rmdir command does not remove the directory. To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r .

Which command would you use to copy all files and subdirectories in a directory?

To copy a directory with all subdirectories and files, use the cp command.

Which option used with the Copy command makes sure that all copied files?

The switch that is used for making sure that the copy command copied files correctly or not is /V. The command copies files from one directory to other.

Which command would you use to do a directory listing?

Use the ls command to display the contents of a directory.