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:
-
find . \( -name "*.txt" -or -name "*.exe" \): This uses thefindcommand to search for all files with the specified extensions (.txtor.exe) in the current directory (.) and its subdirectories. The parentheses (\(and\)) are used to group the-nameoptions for theorcondition. -
-print0: This option adds a\0character to the end of each file name, making it easier to handle filenames that contain spaces or other special characters. -
pv -0 -s $(find . \( -name "*.txt" -or -name "*.exe" \) -print0 | wc -c): Thepv(pipe view) command is used to display the progress of data transfer in the pipeline. It takes the output from thefindcommand, calculates the size of the data usingwc -c, and passes it topvto display the progress of the data transfer. The-0option is used to specify that the\0characters should be used as separators. -
xargs -0 rm: Thexargscommand is used to execute a command with arguments taken from standard input. In this case, it takes the output frompvand passes it as arguments to thermcommand, which deletes the files. The-0option is used to specify that the\0characters 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.
Comments powered by Disqus.