[LINUX] [High-speed version] Search files or directories and delete them all at once

Others may have already posted it, but it's a frequently used command, so it's a memorandum.

Conclusion

It becomes explosive speed with find + xargs.

find <path> -name <name> -type <type> | xargs rm -rf

Commentary

If you google, you can also hit the method using the -exec option in addition to the xargs.

find <path> -name <name> -type <type> -exec rm -rf {} \;

In the case of -exec {};, pass each line to the command and execute rm.

rm -rf foo.txt
rm -rf bar.txt
rm -rf baz.txt
rm -rf qux.txt

In the case of xargs, pass multiple lines to the command as much as possible and execute rm.

rm -rf foo.txt bar.txt baz.txt qux.txt

The difference between running each time and running all at once. So xargs is faster.

By the way, if you use -exec {} +, you can pass multiple lines to the command at once, but if there are tens of thousands of execution targets, xargs seems to be more efficient.

Recommended Posts

[High-speed version] Search files or directories and delete them all at once
Recursively search for files and directories in Python and output
Linux: files and directories
Linux (about files and directories)
Rsync multiple files at once