Linux / Unix: Find And Remove Files With One Command On Fly

Tutorial details Difficulty Easy (rss) Root privileges No Requirements find Time 5 minute

ADVERTISEMENTS



Find And Remove Files With One Command On Fly

I am new to Unix and Linux command line. How do I find and delete all files (say all ‘*.bak’) under Linux / UNIX-like operating systems using a shell prompt?Sometimes it is necessary to find out all files and remove them in a single go. However, the rm command does not support search criteria. For example, find all “*.bak” files and delete them. For such necessities, you need to use the find command to search for files in a directory and remove them on the fly. You can combine find and rm command together. This page explains how to find and remove files with one command on fly.

The basic find command syntax is as follows:

find dir-name criteria action

Where,

dir-name : – Defines the working directory such as look into /tmp/ criteria : Use to select files such as “*.sh” action : The find action (what-to-do on file) such as delete the file.

You want to remove multiple files such as ‘*.jpg’ or ‘*.sh’ with one command find, try:

find . -name "FILE-TO-FIND" -exec rm -rf {} \;

OR

find /dir/to/search/ -type f -name "FILE-TO-FIND-Regex" -exec rm -f {} \;

The only difference between above two syntax is that the first command remove directories as well where second command only removes files. Where, options are as follows:

-name "FILE-TO-FIND" : File pattern. -exec rm -rf {} \; : Delete all files matched by file pattern. -type f : Only match files and do not include directory names. -type d : Only match dirs and do not include files names.

Modern version of find command has -delete option too. Instead of using the -exec rm -rf {} \; , use the -delete to delete all matched files. We can also explicitly pass the -depth option to the find to process each directory’s contents before the directory itself. It is also possible to use the -maxdepth option to control descend at most levels of directories below the starting-points. For example, -maxdepth 0 means only apply the tests and actions to the starting-points themselves. Similary, we can pass the -mindepth to the find. It means do not apply any tests or actions at levels less than levels (a non-negative integer). For exampe, -mindepth 1 means process all files except the starting-points. So here is a simplied syntax:

find /dir/to/search/ -type f -name "FILES-TO-FIND" -delete

find /dir/to/search/ -type f -name "FILES-TO-FIND" -depth -delete

find /dir/to/search/ -maxdepth 2 -type f -name "FILES-TO-FIND" -depth -delete

Examples of find command

WARNING! These examples may crash your computer if executed. Before removing file makes sure, you have backup of all-important files. Do not use rm command as root user it can do critical damage to the system. These examples may crash your computer if executed. Before removing file makes sure, you have backup of all-important files. Do not use rm command as root user it can do critical damage to the system.

Find all files having .bak (*.bak) extension in the current directory and remove them:

find . -type f -name "*.bak" -exec rm -f {} \;

OR

find . -type f -name "*.bak" -delete

Find all core files in the / (root) directory and remove 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 removes them with confirmation from user:

$ find . -type f -name "*.bak" -exec rm -i {} \;

Sample outputs:

rm: remove regular empty file `./data0002.bak'? y rm: remove regular empty file `./d234234234fsdf.bak'? y rm: remove regular empty file `./backup-20-10-2005.bak'? n

The -delete always works better because it doesn’t have to spawn an external process such as rm for every matched file. However, the -delete option may not available on all find versions. Hence, we can use the xargs command as follows too:

find . -type f -name "*.err" -print0 | xargs -I {} -0 rm -v "{}"



-print0 – Force find command to print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs.

Where the find command option is as follows:

And the xargs command options are as follows:

-I {} : Replace occurrences of {} in the initial-arguments with names read from standard input. We pass {} as arg to the rm command.

: Replace occurrences of {} in the initial-arguments with names read from standard input. We pass {} as arg to the rm command. -0 : Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.

: Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode. rm -v "{}" : Run rm command on matched files.

Conclusion

For detailed information on find command please see finding/locating files with find command part # 1, Part # 2.

Man pages : rm(1)