The “find” is the built-in command for finding files/directories based on their name, username, group, and creation/modification date. It offers a wide range of supported arguments that perform special tasks such as the “-exec rm {}\;” and “-delete” options. The “-exec rm {}\;” allows the execution of the “rm” command for the deletion of the found result. While the “-delete” removes the found files/ directories from the system.
This post highlights the difference between find’s “-exec rm {} \;” and find’s “-delete” options.
- Working of “-exec rm{}\;” Argument with the “find” Command
- Working of “-delete” Argument with the “find” Command
- Difference Between find’s -exec rm {} \; and -delete Arguments
Working of “-exec rm{}\;” with the “find” Command
The “-exec rm{}\;” removes the specified files/directories that are found with the help of the “find” command. The components of the “exec rm{}\;” is described here:
- -exec: Allows the execution of another command on the result of the find command.
- rm: Represents the “remove” command for removing files and directories found using the “find” command.
- {}: Shows the placeholder that takes the file/directory name given by the find command to execute the “rm” or another command.
- \; Acts as a delimiter that identifies the end of the command.
Example:
Execute the “-exec rm{}\;” command with the “find” tool to find the “File1.txt” from the current working directory and delete it:
$ find . -name "File1.txt" -exec rm {} \;
In the above command, the “.” represents the current working directory and the “-name” argument specifies the filename, i.e., “File1.txt”:
The above command has been successfully executed.
For verification, execute the “ls” command by specifying the name of the file “File1.txt” in this way:
$ ls File1.txt
It is verified that there is no “File1.txt” file in the current working directory.
Working of “-delete” with the “find” Command
The “-delete” is another flag or argument that can be used with the “find” command to delete the searched files/directories. Let’s see its practical implementation:
Example:
Specify the “-delete” flag after the particular file/directory that needs to be found via the below command:
$ find . -name "File2.txt" -delete
The “-delete” flag has deleted the “File2.txt” file after finding it from the current working directory.
Difference Between find’s -exec rm {} \; and -delete Arguments
This section highlights the difference between the find’s -exec rm {} \; and -delete arguments. Let’s dig into one of one:
- Syntax: The syntax of the “-exec rm {} \; ” depends on the “rm(remove)” command that executes after the successful execution of the “find” command. On the other hand, the “-delete” does not require any additional command.
- Extensibility: The “exec rm {}\;” is more extensible as compared to “-delete”. This is because it allows the supported arguments of the “rm” command that performs the desired operation on found files/directories.
- Efficiency: The “-exec rm {} \;” argument is slower as compared to the “-delete” flag because it requires the additional step of invoking the “rm” command. Whereas the “-delete” flag removes the find command result instantly.
- Compatibility: The “-delete” argument is limited for the latest version of the “find” command, while the “-exec rm {} \;” is supported by all Linux operating systems.
Conclusion
The find’s “-exec rm {} \;” and the find’s “-delete” are different from each other based on their “syntax,” “extensibility,” “efficiency,” and the “compatibility.” The usage of both these arguments depends on the user’s choice. However, the “-exec rm {}\;” argument is preferable due to its availability on all Linux systems.
This post has provided a detailed view on find’s “-exec rm {} \;” vs. “-delete” arguments