Home  >  Article  >  Operation and Maintenance  >  How to find and delete files or directories recursively in Linux?

How to find and delete files or directories recursively in Linux?

藏色散人
藏色散人Original
2019-04-16 14:53:086548browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn