Home Find and delete multiple file types in Linux using the command line
Post
Cancel

Find and delete multiple file types in Linux using the command line

The Script

If you need to delete multiple file types in Linux, you can use the following command in the terminal:

Please note that this command will permanently delete the files, so use it with caution and only in directories where you have permission to delete files.

1
find . \( -name "*.txt" -or -name "*.exe" \) | pv -s $(find . \( -name "*.txt" -or -name "*.exe" \) | wc -c) | xargs -I {} rm {}

This command will delete both .txt and .exe files in the current directory and its subdirectories. Let’s take a closer look at the different components of the command:

  1. find . \( -name "*.txt" -or -name "*.exe" \): This uses the find command to search for all files with the specified extensions (.txt or .exe) in the current directory (.) and its subdirectories. The parentheses (\( and \)) are used to group the -name options for the or condition.

  2. -print0: This option adds a \0 character to the end of each file name, making it easier to handle filenames that contain spaces or other special characters.

  3. pv -0 -s $(find . \( -name "*.txt" -or -name "*.exe" \) -print0 | wc -c): The pv (pipe view) command is used to display the progress of data transfer in the pipeline. It takes the output from the find command, calculates the size of the data using wc -c, and passes it to pv to display the progress of the data transfer. The -0 option is used to specify that the \0 characters should be used as separators.

  4. xargs -0 rm: The xargs command is used to execute a command with arguments taken from standard input. In this case, it takes the output from pv and passes it as arguments to the rm command, which deletes the files. The -0 option is used to specify that the \0 characters should be used as separators.

In conclusion, this command can be a useful tool for deleting multiple file types in Linux. By using pv and xargs, you can easily display the progress and delete multiple files at once.

This post is licensed under CC BY 4.0 by the author.

Script to Delete User Profiles with PowerShell GUI

-

Comments powered by Disqus.