Advertisement

Linux Basics = Remove Directories

Started by February 21, 2005 07:47 PM
6 comments, last by jflanglois 19 years, 9 months ago
I'm just a little confused. What's the best way to remove all the files and subfolders within a directory and then delete the directory from the command-line? Do I need to write a script for this? It's easy to do this in konqueror with the mouse: select, shift+delete. More importantly I want a way to delete all folders named ".svn" within a certain file-hierarchy. Should I write a script for this and would this be better suited to shell scripting or python scripting? Thanks [help]
IIRC

rm -R <directory name>

And if you don't want to be prompted at each file, do:
rm -R -f <directory name>

To see all command line params do "man rm".

[edit] Oh, and for your second question, you can use the find tool, e.g.:
find / -name '.svn' -exec rm {} \;


HTH.

Regards,
jflanglois

[Edited by - jflanglois on February 21, 2005 8:51:56 PM]
Advertisement
rmdir -r
[size="1"]
Quote: Original post by mrbastard
rmdir -r

There's no -r for rmdir.

You need to use rm -rf <dirname>/*

Use the wildcard if you want to preserve the top directory and delete everything within it. Remove the /* if you want the top directory deleted/removed as well.
Quote: Original post by Scribbler
There's no -r for rmdir.


Perhaps not in the gnu version, but a quick google will confirm it's existence in many other unix variants.
[size="1"]
A quick look at `man rmdir` from 'GNU fileutils 4.0' will include:

CONFORMING TO
POSIX 1003.2

Which means that using a rmdir -r is unportable while rm -r /is/ POSIX-conforming and thus more likely to work in other places.
Advertisement
Quote:
find / -name '.svn' -exec rm {} \;


This way of doing it is fairly slow, because it forks once per thing getting rm-ed. It also won't quite work, because it'll try to "rm" without arguments the directory named '.svn'; it'll need the "-rf" option. Last, it starts at the root, not at the current directory, which was what the OP wanted (I think).

Here's an alternate, that I like better:

find . -name .svn | xargs rm -rf


In general, you'll want to use xargs with find, instead of the "{}" kludge.
enum Bool { True, False, FileNotFound };
Quote: Original post by hplus0603
Quote:
find / -name '.svn' -exec rm {} \;


This way of doing it is fairly slow, because it forks once per thing getting rm-ed. It also won't quite work, because it'll try to "rm" without arguments the directory named '.svn'; it'll need the "-rf" option. Last, it starts at the root, not at the current directory, which was what the OP wanted (I think).

Here's an alternate, that I like better:

find . -name .svn | xargs rm -rf


In general, you'll want to use xargs with find, instead of the "{}" kludge.


Yeah, I forgot to add the rm arguments. And I know it starts at root. I was giving an example, not an implementation.
Didn't know about xargs though. Thanks.

Regards,
jflanglois

This topic is closed to new replies.

Advertisement