How to recursively delete all files with extensions *.a *.b *.c *.d etc
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The default -and has higher priority than -or ==> there should be brackets.
The brackets have special meaning in the shell ==> need to be escaped.
Further, -exec rm {} + is faster than -exec rm {} \; and -delete is even faster.
A safer idea I use is: put (> or >>) the names in a file.
Review the file, to ensure nothing 'improper' will be removed.
Then, use one of several techniques to rm the list of files.
Safety first I'd hate to see an errant `find` destroy things!!!
That would delete any file or directory in the current directory named *.ext. The -r flag just means if you give it a directory, it will delete all of its contents as well. That's different than what the OP had asked for because it won't search out any file named *.ext located inside subdirectories, it will only match files/dirs named *.ext in the current directory. For example, if you had a directory named "blah", and inside a file named "file.ext", "rm -r *.ext" would NOT delete it, while "find . -name '*.ext' -delete" would.
Of course you should run it without the "-exec rm {} \;" at first to make sure the files it finds are what you actually want to delete.
This is inefficient. The -exec command at the end will call the rm command for every file it finds separately, which produces a lot of overhead. A better way to do it is to either use the inbult -delete command, or to use
Code:
-exec rm {} +
instead of
Code:
-exec rm {} \;
. The + sign signals the find command that you expect to find more than one file and that it should accumulate filenames until you reach the maximal allowed commandline length and then to call the rm command with all the accumulated files.
This may not be relevant if you only have a few files to delete, but if you traverse over large directories with thousands of files to delete you will see significant performance improvements.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.