Linux / UNIX: Deleting Files In Many Subdirectories

Linux / UNIX: Deleting Files In Many Subdirectories

To delete files in many subdirectories on a Linux or Unix system, you can use the find command in combination with the rm command. The find command allows you to search for files based on various criteria, such as the file name, the file type, or the location, and the rm command allows you to delete the files that are found.

For example, to delete all the .txt files in the current directory and all of its subdirectories, you can use the following command:

$ find . -name "*.txt" -exec rm {} \;
So‮cru‬e:www.lautturi.com

This command will search for all files with a .txt extension in the current directory and its subdirectories, and it will delete the files that are found. The -exec flag allows you to specify a command (in this case, rm) to be executed on the files that are found, and the {} placeholder is replaced with the name of each file. The \; at the end of the command indicates the end of the command.

You can also use the -type flag to specify the type of files you want to delete. For example, to delete all directories in the current directory and its subdirectories, you can use the following command:

$ find . -type d -exec rm -r {} \;

This command will search for all directories in the current directory and its subdirectories, and it will delete the directories and all of their contents using the rm -r command.

Note that the rm command permanently deletes the specified files and there is no way to recover them. Use caution when using the rm command.

For more advanced usage of the find and rm commands, you can refer to the find and rm man pages by running man find and man rm on the command line.

Created Time:2017-10-29 22:08:56  Author:lautturi