Recursively deleting directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Recursively deleting directories
# 1  
Old 11-26-2004
Recursively deleting directories

Say I have a directory call test, and several directories nested in it, and several directories nested in them. And I want to remove all directories within "test" and its subdirectories that have the name "cvs", how can I do this?

I tried rm -r cvs, but that only removed the top level direcotry name cvs.

Thanks for any help
# 2  
Old 11-26-2004
I'd say something like

find /path/to/test -name "cvs" -type d | xargs rm -r

As with all rm -r usage - beware! I always create a test directory structure first whilst evaluating the command and its side-effects!

Cheers
ZB
# 3  
Old 11-26-2004
Hi,
Thanks very much, could you explain what "-type d | xargs" means.
I assume -type d means directory
# 4  
Old 11-26-2004
No problem.

You are correct - "-type d" does indeed mean directories. Specifically it tells find to only match files of type "directory". This can also be f for regular files, l for symlinks, etc - "man find" will detail the plethora of options available for find - an extrememly powerful command.

The | xargs bit is used to pipe the results from find (i.e. the list of directories) to the rm -r command, so that the command can then be executed. We could also have formed the find command thusly:
find /path/to/test -name "cvs" -type d -exec rm -r {} \;
and it would have done pretty much the same thing.
xargs is useful when the number of results returned by find exceeds the maximum number of arguments allowed by the command we're piping to. Using find with "exec" would have sufficed here, but xargs is habit (and I believe, good practice) for me.

man find, man xargs, man <my_shell_here> will teach you more about the find and xargs commands - and pipes and redirection.

Cheers
ZB

Last edited by zazzybob; 11-26-2004 at 11:00 AM..
# 5  
Old 11-26-2004
Thank you, I appreciate your help
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generic script to recursively cd into directories and git pull

Hi all, I'm trying to write a script to recursively cd into my Git projects and pull them, and will later expand it to build my projects as well. I'm having a bit of trouble with my current script, as I want to supply a command line argument to tell it which branch to check out. I can hard... (2 Replies)
Discussion started by: Cows
2 Replies

2. Shell Programming and Scripting

Recursively Searcing file in the directories

i have directory dgf in the dgf( some other Sub-dir are there) 00 01 02 03 04 in all the Sub directory there is a SG.csv .. i want the scripts should run one by one Sub-dir and print the result for that particular Sub-dir ..then go to next Sub-Dir and print the result....... please... (6 Replies)
Discussion started by: Aditya.Gurgaon
6 Replies

3. Shell Programming and Scripting

Deleting all files recursively from directories while ignoring one file type

Hi, Seems like I need help again with a problem: I want to delete all files from my lets say "Music" Directory inkluding all of the subfolders except for .mp3 and .MP3 files. I tried it with globalignoring mp3 files, finding and deleting all other files, which resulted in all files... (3 Replies)
Discussion started by: pasc
3 Replies

4. Shell Programming and Scripting

Recursively rename directories

I have this directory tree under /apps/myapp/data: imageshack.us/photo/my-images/703/foldersc.png How to recursively rename ONLY directories with 5 digits (00000, 00100, 00200,..., 00007, 00107,...)? I want to add to their name two more zeros: Before: 00107 After: 0000107 Thanks in... (2 Replies)
Discussion started by: Susan_45
2 Replies

5. UNIX for Advanced & Expert Users

Delete empty directories recursively - HP-UX

Hi, I want to delete all empty directories in a long directore tree structure. I want to use that from a script that will run on HP-UX 11. My definition of empty directory is that there is no regular file under it and directly beneath it. To elaborate, I have below directories. /app/dev/java... (14 Replies)
Discussion started by: asutoshch
14 Replies

6. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

7. Windows & DOS: Issues & Discussions

PSFTP/PSCP: remotely deleting directory recursively

Hello, I'm looking for a way to use PSFTP or PSCP, executed from a batch file in Windows, to delete an entire folder and its contents. I can run a script using PSFTP which can use rm or rmdir, but the -rf command doesn't work. Any suggestions on another client to use in the same context, or a... (2 Replies)
Discussion started by: ocdcollector
2 Replies

8. UNIX for Advanced & Expert Users

Recursively delete only specified directories with given pattern

Hi All, We have a requirement to recursively delete the directories and its subdirectories older than 60 days based on timestamp (folder creation timestamp)under certain directory. However it has some specific requirements. The directories will continue to be there upto any depth. the... (0 Replies)
Discussion started by: rcvasu
0 Replies

9. UNIX for Dummies Questions & Answers

How to display directories recursively?

Cannot find how to list the directory structure of a volume recursively. Do not want the files reported. Say I have 100 directories and 10,000 files, I do not want 10,000 lines of output. (If this is relevant, I am using the terminal on my OSX Mac). I hope this is easy - there should be an easy... (5 Replies)
Discussion started by: jwriter
5 Replies
Login or Register to Ask a Question