LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Recursive delete specific files from sub-directories. (http://www.linuxquestions.org/questions/linux-newbie-8/recursive-delete-specific-files-from-sub-directories-866423/)

guriinii 03-04-2011 09:12 AM

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.

szboardstretcher 03-04-2011 09:25 AM

You can delete every instance of a file with:

Code:

find . -name "somefilename"-exec rm -rf {} \;

sycamorex 03-04-2011 09:30 AM

Quote:

Originally Posted by guriinii (Post 4278879)
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)?

guriinii 03-04-2011 09:47 AM

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

jschiwal 03-04-2011 09:48 AM

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}

jschiwal 03-04-2011 09:51 AM

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

szboardstretcher 03-04-2011 09:52 AM

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 (Post 4278929)
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!

sycamorex 03-04-2011 09:56 AM

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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

guriinii 03-04-2011 10:13 AM

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

szboardstretcher 03-04-2011 10:18 AM

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. :)

guriinii 03-04-2011 10:33 AM

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.

guriinii 03-07-2011 10:41 AM

I've tested it and it works. Thanks.


All times are GMT -5. The time now is 12:20 PM.