LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-04-2011, 09:12 AM   #1
guriinii
LQ Newbie
 
Registered: Aug 2010
Distribution: Arch Linux
Posts: 16

Rep: Reputation: 0
Recursive delete specific files from sub-directories.


Hi.

I would like to be able to recursively delete specific various files from a directory and sub-directories.

For example:

Dir/
|_sub1/
|
|_ _rm *file1 *file2 *file3
|
|_sub2/
|
|_ _rm *file1 *file2 *file3
|
|_sub3/
|
|_ _rm *file1 *file2 *file3


What is the best way to do this?

Thanks in advance.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-04-2011, 09:25 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux
Posts: 2,762
Blog Entries: 1

Rep: Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871
You can delete every instance of a file with:

Code:
find . -name "somefilename"-exec rm -rf {} \;
 
Old 03-04-2011, 09:30 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,524
Blog Entries: 1

Rep: Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997
Quote:
Originally Posted by guriinii View Post
Hi.

I would like to be able to recursively delete specific various files from a directory and sub-directories.

For example:

Dir/
|_sub1/
|
|_ _rm *file1 *file2 *file3
|
|_sub2/
|
|_ _rm *file1 *file2 *file3
|
|_sub3/
|
|_ _rm *file1 *file2 *file3


What is the best way to do this?

Thanks in advance.
It's not really clear whether there's any common pattern in file names. Do those files have similar names, extensions, patterns or they are different in each subdirectory?
Are we talking theoretically or perhaps you could post the output of the 'tree' command (as long as there are not too many files inside)?
 
Old 03-04-2011, 09:47 AM   #4
guriinii
LQ Newbie
 
Registered: Aug 2010
Distribution: Arch Linux
Posts: 16

Original Poster
Rep: Reputation: 0
My apologies here is the tree:

Code:
RR-Dir/
├── CD1
│** ├── rr-1.avi
│** ├── rr-1.r00
│** ├── rr-1.r01
│** ├── rr-1.r02
│** ├── rr-1.r03
│** ├── rr-1.r04
│** ├── rr-1.r05
│** ├── rr-1.r06
│** ├── rr-1.r07
│** ├── rr-1.r08
│** ├── rr-1.r09
│** ├── rr-1.r10
│** ├── rr-1.r11
│** ├── rr-1.r12
│** ├── rr-1.r13
│** ├── rr-1.r14
│** ├── rr-1.r15
│** ├── rr-1.r16
│** ├── rr-1.r17
│** ├── rr-1.r18
│** ├── rr-1.r19
│** ├── rr-1.r20
│** ├── rr-1.rar
│** └── rr-1.sfv
├── CD2
│** ├── rr-2.avi
│** ├── rr-2.r00
│** ├── rr-2.r01
│** ├── rr-2.r02
│** ├── rr-2.r03
│** ├── rr-2.r04
│** ├── rr-2.r05
│** ├── rr-2.r06
│** ├── rr-2.r07
│** ├── rr-2.r08
│** ├── rr-2.r09
│** ├── rr-2.r10
│** ├── rr-2.r11
│** ├── rr-2.r12
│** ├── rr-2.r13
│** ├── rr-2.r14
│** ├── rr-2.r15
│** ├── rr-2.r16
│** ├── rr-2.r17
│** ├── rr-2.r18
│** ├── rr-2.r19
│** ├── rr-2.r20
│** ├── rr-2.rar
│** └── rr-2.sfv
└── rr.nfo
I'd like to remove everything apart from the .avi
 
Old 03-04-2011, 09:48 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
If by *file1, you want every file ending in the characters "file1", you can use brace expansion:

rm Dir/{sub1,sub2,sub3}/{*file1,*file2,*file3}
 
2 members found this post helpful.
Old 03-04-2011, 09:51 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could also use find, to list all files, excluding "*.avi" files, and then delete them:
find RR-Dir -type f -not -iname "*.avi" -delete
 
2 members found this post helpful.
Old 03-04-2011, 09:52 AM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux
Posts: 2,762
Blog Entries: 1

Rep: Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871
Code:
find . -name \*.r* 
find . -name \*.sfv
if those return what you want, add this onto the end.

Code:
-exec rm -rf {} \;


---------- Post added 03-04-11 at 10:53 AM ----------

Quote:
Originally Posted by jschiwal View Post
You could also use find, to list all files, excluding "*.avi" files, and then delete them:
find RR-Dir -type f -not -iname "*.avi" -delete
Or try this: but remove -delete to test!
 
Old 03-04-2011, 09:56 AM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,524
Blog Entries: 1

Rep: Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997Reputation: 997
If you go to the RR-Dir and issue:

Code:
find . -name "*.[^avi]*" -exec rm -rf {} \;
BEFORE YOU DO IT, BACKUP YOUR DATA AND TEST IT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
2 members found this post helpful.
Old 03-04-2011, 10:13 AM   #9
guriinii
LQ Newbie
 
Registered: Aug 2010
Distribution: Arch Linux
Posts: 16

Original Poster
Rep: Reputation: 0
Thank you all for your help.

Code:
find RR-Dir -type f -not -iname "*.avi" -delete
Worked perfectly, and yes, I copied it first.

Could this be put into a script so that I type a simple command?

For example: delete RR-Dir

---------- Post added 03-04-11 at 04:13 PM ----------

Thank you all for your help.

Code:
find RR-Dir -type f -not -iname "*.avi" -delete
Worked perfectly, and yes, I copied it first.

Could this be put into a script so that I type a simple command?

For example: delete RR-Dir
 
Old 03-04-2011, 10:18 AM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux
Posts: 2,762
Blog Entries: 1

Rep: Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871
Code:
function delete_everything_but () { 

find . -type f -not -iname $* }
Put that in .bashrc

Again, test this, then add -delete when it works the way you want.

Im giving out REP for everyones help on this, because everyone here was very helpful.

Last edited by szboardstretcher; 03-04-2011 at 10:24 AM.
 
1 members found this post helpful.
Old 03-04-2011, 10:33 AM   #11
guriinii
LQ Newbie
 
Registered: Aug 2010
Distribution: Arch Linux
Posts: 16

Original Poster
Rep: Reputation: 0
Thank you all again. I'm currently out of directories to test this on. I'll get back to you when I've done it.
 
Old 03-07-2011, 10:41 AM   #12
guriinii
LQ Newbie
 
Registered: Aug 2010
Distribution: Arch Linux
Posts: 16

Original Poster
Rep: Reputation: 0
I've tested it and it works. Thanks.
 
  


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] FTP: recursive chmod (separate for directories and files) mgmax Linux - Software 11 01-29-2011 08:06 AM
How to do recursive file delete using specifier (*.tmp) from nested directories? Arodef Linux - General 3 11-11-2009 07:49 AM
FIND: Only directories which contain specific files brian.m Linux - General 2 05-07-2009 09:25 PM
Recursive chmod with different values for files and directories? Z038 Linux - Newbie 2 06-07-2006 08:38 AM
How to do recursive file copy of directory for specific files? Arodef Linux - Newbie 4 06-29-2004 05:35 PM


All times are GMT -5. The time now is 01:54 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration