Process to Find And Remove Files on Linux

Are you trying to find and delete all files (for example, ‘*.bak’) in Linux using a shell prompt?

This guide is for you.

In some cases, you may need 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.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux related tasks on their Servers.
In this context, you will learn how to find and remove files with a single command.

How to Find And Remove Files With a Command on Linux?

To do this, you can execute the following command;

find dir-name criteria action

i. dir-name : – Defines the working directory such as look into /tmp/
ii. criteria : Use to select files such as "*.sh"
iii. 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, execute:

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


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:

1. -name "FILE-TO-FIND" : File pattern.
2. -exec rm -rf {} \; : Delete all files matched by file pattern.
3. -type f : Only match files and do not include directory names.
4. -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. Similarly, 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 example, -mindepth 1 means process all files except the starting-points. So here is a simple command to implement these;

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

Instances 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.

For example, to find all files having .bak (*.bak) extension in the current directory and remove them, simply execute;

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


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

To find all core files in the / (root) directory and remove them (be careful with this command), simply run:

# find / -name core -exec rm -f {} \;

### OR ###

# find / -name core -delete

To find all *.bak files in the current directory and removes them with confirmation from user, simply execute:

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

The -delete always works better because it does not 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 as well:

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

The find command option can be used as follows:
-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.

For the xargs command options, the following applies:
i. -I {} : Replace occurrences of {} in the initial-arguments with names read from standard input. We pass {} as arg to the rm command.
ii. -0 : Input items are terminated by a null character instead of by white-space, 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.
iii. rm -v "{}" : Run rm command on matched files.

[Need to Perform Ubuntu queries or Install Packages on your Linux Server? We are available to help you today.]

This article will guide you on using find command to find and remove files on your Server.

Related Posts