LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-24-2017, 02:47 PM   #1
Grabby
Member
 
Registered: Feb 2016
Posts: 69

Rep: Reputation: Disabled
How to recursively delete all files with extensions *.a *.b *.c *.d etc


Hi
I need some help with the command line.

I need to recursively delete all files with given extensions, starting from the current directory and including all its sub-directories.

Say I want to delete all *.a *.b *.c *.d etc files recursively, what command should I type into the terminal emulator?

Thanks

Grabby
 
Old 10-24-2017, 02:50 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138
Code:
find . -iname '*.a' -or -iname '*.b' -or -iname '*.c' -exec rm {} \;
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.
 
Old 10-24-2017, 03:14 PM   #3
Grabby
Member
 
Registered: Feb 2016
Posts: 69

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Code:
find . -iname '*.a' -or -iname '*.b' -or -iname '*.c' -exec rm {} \;
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.
Does this work recursively for all the nested sub-directories?
 
Old 10-24-2017, 03:52 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,771

Rep: Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797
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.
Code:
find . -type f \( -name '*.a' -o -name '*.b' -o -name '*.c' \) -delete
 
1 members found this post helpful.
Old 10-24-2017, 05:17 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,555

Rep: Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088
It's a lot simpler with patterns:
Code:
find . -type f -name '*.[abcd]' -delete
# or
find . -type f -name '*.[a-d]' -delete
Use "-iname" in place of "-name" if you want a case-insensitive match.
 
Old 10-24-2017, 06:06 PM   #6
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 759

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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!!!

p.s. notice the "Similar Threads" at very bottom.

Last edited by !!!; 10-24-2017 at 06:08 PM.
 
Old 10-26-2017, 09:09 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 945

Rep: Reputation: 216Reputation: 216Reputation: 216
rm -r *.ext ..
?
 
Old 10-26-2017, 10:10 AM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138
Quote:
Originally Posted by AnanthaP View Post
rm -r *.ext ..
?
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.
 
Old 10-26-2017, 05:20 PM   #9
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,677

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Setting a shell option in bash allows recursion into subdirectories.
Code:
shopt -s globstar; rm ./**/*.[a-d]; shopt -u globstar
 
Old 10-27-2017, 06:48 AM   #10
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871
Quote:
Originally Posted by suicidaleggroll View Post
Code:
find . -iname '*.a' -or -iname '*.b' -or -iname '*.c' -exec rm {} \;
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.

Last edited by TobiSGD; 10-27-2017 at 06:50 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Recursively find and delete corrupted movie files seashell11 Linux - General 5 01-14-2015 07:44 PM
Shell script to delete folders and files dynamically and recursively rjbaca Linux - General 1 06-21-2010 11:26 AM
delete files which have no extensions sridhar.bodike Linux - Newbie 12 06-01-2010 04:58 AM
Delete files for a particular changed date recursively? qwertyme Linux - Newbie 5 01-23-2009 10:41 AM
how to recursively delete *.xtension files from a directory structure kpachopoulos Linux - General 6 08-24-2008 08:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration