Linux / UNIX: Deleting Files In Many Subdirectories

If you need to delete files in many subdirectories, you can use the find command in Linux and UNIX systems. The find command allows you to search for files and directories based on various criteria, including the name, type, size, and date of the files.

Here’s an example of using the find command to delete all files with the .txt extension in the current directory and its subdirectories:

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

This command will find all files in the current directory and its subdirectories that have a .txt extension and are of type f (regular file), and then delete them.

It’s important to be careful when using the find command, especially when combined with the -delete option, as it can cause unintended consequences if used improperly. It’s always a good idea to first test the command using the -print option to see which files will be deleted before actually executing the delete operation. For example:

find . -name "*.txt" -type f -print

This will display the names of the files that would be deleted, but not actually delete them.

Leave a Comment