Linux find command is a powerful tool that can be used to locate and manage files and directories based on a wide range of search criteria.
This post will cover how to delete files with find exec rm in Linux. However, some solutions in this article can be dangerous if we don’t correctly use them. We need to test this before we run it in the production environment.
What is find exec rm?
The find, exec, and rm commands are frequently used together in Linux to search for files or directories that match certain criteria and then delete them.
This command combines three commands together to remove any found directories or files from your system. Find exec rm is very helpful for quickly deleting files or directories based on their permissions, type, date, ownership, size, and more.
Find command Syntax in Linux
The find command in Linux is used to search for files and directories in a specified location. Its syntax is as follows:
find [path] [expression]
Here is a brief explanation of each element of the command:
- [path]: This is the starting point for the search. You can specify a directory to start the search from. If you don’t specify a path, the search starts from the current directory.
- [expression]: This is a logical expression that determines which files to include in the search results. The expression can include various operators such as -and, -or, and -not, as well as various tests such as -name, -type, and -size. You can also use parentheses to group expressions and make complex searches.
For example, to search for all files in the current directory and its subdirectories that have the extension .txt, you can use the following command:
find . -name "*.txt"
This command starts the search from the current directory (.), searches for files with the extension .txt (-name “*.txt”), and returns a list of all matching files and their paths.
For example, the result of this command might look something like:
./file1.txt
./dir1/file2.txt
./dir2/subdir/file3.txt
This means that there are three files that match the search criteria, named “file1.txt”, “file2.txt”, and “file3.txt”, and they are located in the current directory and its subdirectories. The . at the beginning of the command specifies that the search should start from the current directory.
Some common expression include -name to search for files by name, -type to search for files by type, -mtime to search for files by modification time, and -size to search for files by size. You can combine multiple options to refine your search criteria.
Find exec rm command Syntax
The syntax of the command is as follows:
find [path] [expression] -exec rm {} \;
Here is a brief explanation of each element of the command:
- -exec: This option allows you to execute a command on each matching file. In this case, the command is rm to delete the file.
- {} \;: This is a placeholder for the name of the matching file, and the \; indicates the end of the -exec option.
For example, to search for all files in the current directory and its subdirectories that have the extension .txt and delete them, you can use the following command:
find . -name "*.txt" -exec rm {} \;
This command starts the search from the current directory (.), searches for files with the extension .txt (-name “*.txt”), and deletes each matching file (-exec rm {} \;).
Be careful when using the rm command, as it permanently deletes files and there is no undo option.
Therefore, it’s always a good idea to double-check the search results before executing the rm command.
Delete Files with a specific name
Every time the find command identifies a file with the .c suffix, the rm command deletes that file.
find /directory -name “*.c” -exec rm {} \;
Delete files in multiple directories
If we want to search and delete all files with a given name in multiple directories,
we can either start the search at root folder, or if we know the directories, we can specify them.
Example:
find /path1 /path2 -name failed*.* -type f -exec rm {} \;
Delete Files Older Than x Days on Linux
find /path/to/files* -mtime +5 -exec rm {} \;
Delete Files with modifying time
To remove the files that are modified within 60 minutes, type:
find /path -mmin -60 -exec rm {} \;
Delete Large files
The first step is to find files larger than 200 MB.
The next target is to remove these files. This can be done by:
find /path -type f -size +200M -exec rm {} \;
Advanced find exec rm examples
To remove all files named a.out or *.o that are not accessed for a week and that are not mounted by using nfs, type:
find / \( -name a.out -o -name ‘*.o’ \) -atime +7 ! -fstype nfs -exec rm {} \;
Note: The number that is used within the -atime expression is +7. It is the correct entry if we want the command to act on files that are not accessed for more than a week (seven 24-hour periods).