Home > Article > Operation and Maintenance > How to find and delete files or directories recursively in Linux?
This article mainly introduces to you how to linux recursively search for files and linux recursively delete files or directories.
To achieve the purpose of recursively finding and deleting files/directories in Linux, we can use the following syntax to use the find
command and the rm
command together.
Here, the number at the end indicates that multiple directories are allowed to be read at the same time.
$ find /start/search/from/this/dir -name "dirname-to-delete" -type d -exec /bin/rm -rf {} +
NOTE: The rm command must be used with caution as it is one of the most dangerous commands used in Linux: you may accidentally delete critical system directory, causing system failure.
In the following example, we will search for a directory called files_2008 and delete it recursively:
$ $find ~/Downloads/software -name "files_2008" -type d -exec /bin/rm -rf {} +
You can also use find
and xargs
;
In the following syntax, the -print0
operation allows printing the full directory path on standard output, followed by a null character:
$ find /start/search/from/this/dir -name "dirname-to-delete" -type d -print0 | xargs -0 /bin/rm -rf "{}"
Use the same as above For example, we have:
$ find ~/Downloads/software -name "files_2008" -type d -print0 | xargs -0 /bin/rm -rf "{}"
In this article, we show you how to find and delete directories recursively on Linux. We hope it will be helpful to friends in need!
The above is the detailed content of How to find and delete files or directories recursively in Linux?. For more information, please follow other related articles on the PHP Chinese website!