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 03-15-2010, 07:42 AM   #1
CHARL0TTE
LQ Newbie
 
Registered: Jul 2009
Posts: 14

Rep: Reputation: 0
How to remove multiple directories in different locations


Hi,

I have hundreds of directories in various subdirs that I need to remove. I want to remove all of these dirs, but can only find solutions on how to do remove files (or how to remove subdirs from within the current dir). Hope someone can help translate this into something useful.

I think I need something like


find -iname 'testfile*' | xargs rm -i


where I want to remove every directory that contains the word 'testfile' within the directory name. I know xargs wont work for dirs, but hopefully you can see what I'm trying to achieve and suggest what will.

Thanks

Charlotte
 
Old 03-15-2010, 07:45 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Code:
find -type d -name 'testfile*' -exec rm -ri "{}" \;

Last edited by troop; 03-15-2010 at 07:47 AM.
 
Old 03-15-2010, 07:48 AM   #3
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,274

Rep: Reputation: 148Reputation: 148
find /path -name "testfile"-exec rm -rf {} \;
 
Old 03-15-2010, 07:51 AM   #4
CHARL0TTE
LQ Newbie
 
Registered: Jul 2009
Posts: 14

Original Poster
Rep: Reputation: 0
SOLVED: How to remove multiple directories in different locations

Thank you Troop and Kirukan!

Works great!
 
Old 03-15-2010, 07:57 AM   #5
CHARL0TTE
LQ Newbie
 
Registered: Jul 2009
Posts: 14

Original Poster
Rep: Reputation: 0
SOLVED: How to remove multiple directories in different locations

...
 
Old 03-15-2010, 08:44 AM   #6
reed9
Member
 
Registered: Jan 2009
Location: Boston, MA
Distribution: Arch Linux
Posts: 653

Rep: Reputation: 142Reputation: 142
Quote:
Originally Posted by kirukan View Post
find /path -name "testfile"-exec rm -rf {} \;
Eek, why add the -f in there?? That's asking for trouble.
 
Old 03-19-2010, 02:09 AM   #7
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,274

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by reed9 View Post
Eek, why add the -f in there?? That's asking for trouble.
-f option forcefully remove the file and directory (it wont prompt to ask one-by-one removal)

See this
PHP Code:
[root@Est local]# rm -r pixman
rmdescend into directory `pixman'? y
rm: descend into directory 
`pixman/include'? y
rm: remove directory `pixman/include/pixman-1'
y
rm
remove directory `pixman/include'? y
rm: descend into directory 
`pixman/lib'? y
rm: remove directory `pixman/lib/pkgconfig'
y
rm
remove directory `pixman/lib'? y
rm: remove directory 
`pixman'? y 

Last edited by kirukan; 03-19-2010 at 02:11 AM.
 
Old 03-19-2010, 03:10 AM   #8
reed9
Member
 
Registered: Jan 2009
Location: Boston, MA
Distribution: Arch Linux
Posts: 653

Rep: Reputation: 142Reputation: 142
I'm aware of what it does. I'm concerned when automatically finding and removing files, that it's easy to accidentally remove something vital.
 
Old 03-25-2010, 06:13 AM   #9
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,274

Rep: Reputation: 148Reputation: 148
Quote:
it's easy to accidentally remove something vital.
In some occasion there may be thousands of files. is it possible to confirm one-by-one removal? As i understand, find have options to specify the target removal(Ex:- name, size, crated or modified date, user, group, no-user & no-group etc).
 
Old 03-25-2010, 08:20 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
http://linux.die.net/man/1/rm - see the -i option.
However, if you've got 100s or 1000s of files

1. do a backup
2. write a more intelligent program eg in Perl, that checks carefully that each file or dir name matches a strict pattern & test(!).

FYI, there's no such thing as 'created' date in *nix; ctime is inode change date.
 
  


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
data sharing across multiple geographical locations ekalavya Linux - Networking 1 03-09-2010 06:32 AM
Copying single file to multiple locations Ashiro Linux - Server 4 12-05-2008 06:50 AM
Redirecting output to multiple locations? SirTristan Linux - Newbie 6 05-29-2008 04:52 PM
Remove Multiple Home Directories paparob Linux - Newbie 3 04-24-2008 03:57 AM
mount same device at multiple locations merlin23 Linux - Newbie 2 12-06-2004 08:16 AM


All times are GMT -5. The time now is 10:37 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration