I’m new to the Unix and Linux command line. How do I find and delete all files (say all *.bak) on Linux / UNIX-like operating systems from the command line?
Sometimes you have to find all the files and delete them all at once. However, the rm command does not support search criteria. For example, find all *.bak files and delete them. For such needs, you should use the search command to find files in the folder and delete them immediately. Click here to read Windows Server Management. You can combine the search and the rm command. This page explains how to find and delete files with one command.
Searching for and deleting files using Hot Command
The basic syntax of the search is as follows:
Find the criterion name-direction action
True,
- dirty name: – Sets a workbook such as /tmp/.
- criteria: Use this option to select files such as *.sh.
- Special offer: Find action (what to do with the file), e.g. delete the file.
If you want to delete multiple files such as ‘*.jpg’ or ‘*.sh’ with a single query, try:
find . -name FILE-TO-FIND -exec rm -rf {} ;
OF
find /dir/to/search/ -type f -name FILE-TO-FIND-Regex -exec rm -f {} ;
The only difference between the above two syntaxes is that the first command also deletes directories, while the second command only deletes files. True, the options are as follows:
- – Name FILE TO FIND : Presentation of the files.
- -exec rm -rf {} : Delete all files that match the template.
- -type f : Only identify files and do not specify folder names.
- -type d : Only identify folders and do not specify filenames.
The modern version of the query also includes the -delete option. Instead of using -exec rm -rf {}, use -delete to delete all matching files. We can also explicitly pass the -depth search option to process the contents of each folder for the folder itself. You can also use the -max depth option to check the descent at most folder levels below the starting points. For example -max depth 0 means that tests and actions are only applied to the starting points themselves. Similaria, we can overcome discovery. This means that no test or action may be applied below the levels (non-negative integer). For control purposes, -depth 1 means the processing of all files with the exception of the basic principles. Now, here’s the simple syntax:
Find /directory/to/find/ – type f – name FILES-TO-FIND – delete
Find /directory/to/find/ – type f – name FILES-TO-FIND – depth – delete
Find /directory/to/find/ – maximum depth 2 – type f – name FILES-TO-FIND – delete – depth – delete
Examples of order search
WARNING! These examples may cause your computer to shut down unexpectedly. Before deleting a file, you need to back up all important files. Do not use the rm command as a root, it can cause critical system damage.
Find all files with the .bak extension (*.bak) in the current directory and delete them:
find . -type f -name *.bak -exec rm -f {} ; search
OF
. -type f -name *.bak -delete
Find all main files in the / (root) directory and delete them (be careful with this command):
# find / -name core -exec rm -f {} ;
### OR ###
# find / -name core -delete
Find all *.bak files in the current directory and delete them with the user’s confirmation:
$ find . -type f -name *.bak -exec rm -i {} ;
Example output :
Rm: Delete an ordinary empty file `./data0002.bak’ ? y
rm : Delete an ordinary empty file `./d234234fsdf.bak’ ? y
rm : Delete an ordinary empty file `./backup-20-10-2005.bak’ ? n
The delete function works better and better because there is no need to create an external process as rm for each corresponding file. However, the -delete option may not be available for all versions found. So we can also use the xargs command as follows: Search
. -type f -name *.err -print0 | xargs -I {} -0 rm -v {}
command, where the option of the search command is as follows:
- -print0 – Forces a query to print the full filename on the standard output followed by a zero character (instead of the character of a new string that -print uses). This allows you to correctly interpret the names of files that contain new lines or other types of spaces by programs that process the output of the search results. This option corresponds to -0 for xargs.
And the options of the xargs command are as follows:
- -I {} : Replace the {} entries in the source documents with the names contained in the standard entry. We turn to the rm command as an argument.
- -0 : Input elements end with a zero character instead of a space, and quotation marks and backslashes are nothing special (each character is taken at its nominal value). Disables the end of a file line, which is treated as any other argument. Useful when input elements may contain spaces, quotes, or backslashes. The GNU find -print0 option produces items suitable for this mode.
- Execute the rm command to get the corresponding files.
Conclusion
- For more information about the search, see the section Finding/removing files using the search query in Part 1, Part 2.
- Management pages: rm(1)
RECOMMENDATIONS
Published: Vivek gears
The author is the creator of nixCraft and an experienced system administrator, DevOps engineer and Linux/Unix shell script trainer. Receive the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly by email.delete from find command,find and delete directories in linux,how to delete a word from a file in linux,terminal find delete directory,find delete files and directories,linux delete files with specific name,delete files containing string linux,command delete file