To remove a file using the find command in Linux

Published on Aug. 22, 2023, 12:14 p.m.

To remove a file using the find command in Linux, you can use the exec option to execute the rm command on each file that matches your search criteria. Here is an example command to remove all files named “file-to-delete.txt” from the current directory and its subdirectories:

find . -name "file-to-delete.txt" -type f -exec rm {} \;
In this command , . specifies the current directory as the starting point of the search. -name “file-to-delete.txt” specifies the filename pattern to match. -type f ensures that we only match regular files and not directories or other special files. -exec rm {} \; executes the rm command on each matched file , with {} being replaced by the filename. The \; signifies the end of the command to exec.

Note that this command will permanently delete the matching files, so use it with caution. Also, make sure to test the command on a few files first to ensure that it behaves as expected.

Tags:

related content