Click to See Complete Forum and Search --> : rm nonempty directories with wildcard


topcoder1
10-29-2007, 05:00 PM
is it possible to use wildcard to remove nonempty directories? I looked through rm and rmdir, none of them do what I want completely. any useful script is welcome!
for example: I have 2 nonempty directories /a/bcd/ and /b/xcd/, i want to remove both of them by using wildcard *cd
thanks!

saikee
10-29-2007, 05:12 PM
Topcoder1

Welcome to Justlinux !

Try
rm -r /a
rm -r /bThink it worked for me the last time when I had to use one. The "Linux in a Nutshell" also describes it.

topcoder1
10-29-2007, 07:24 PM
saikee, thanks!
but my intention is to have a filter that would allow me to remove certain directories based on their names. For example, I want to remove all sub directories whose name ends with "cd". I don't want to go through each directory that I want to remove.
is it possible?

DrChuck
10-29-2007, 07:58 PM
I have 2 nonempty directories /a/bcd/ and /b/xcd/, i want to remove both of them by using wildcard *cd Like Saikee said, try the following: rm -r */*cd But be extremely careful with the wildcards! An accidental space after the first *, and you're screwed.

saikee
10-29-2007, 08:13 PM
I believe DrChuck's instruction is to have the command issued inside /a and the /b directories which is a lot safer when using the wildcards.

Issuing the command in "/ " could wipe the Linux clean but I have never done it before so there may be a protection mechanism that inhibits its implementation.

teeitup
10-30-2007, 03:52 AM
Using wildcards to remove entire directories can be very dangerous.

That said I would use the find command.

cd to the top level directory that the target directories would exist.
ie: /home or /data
Being the owner instead of root would also be safer.

$ find . -type d -name '*cd' -exec rm -rf {} \;

Be careful.

Good Luck,

topcoder1
10-30-2007, 04:45 AM
Issuing the command in "/ " could wipe the Linux clean but I have never done it before so there may be a protection mechanism that inhibits its implementation.

U mean issuing rm -r */*cd inside root would wipe the linux clean? or do you just mean rm -r / would cause the disaster? I wanna make sure before I do it...
thanks all for the help!!

mrrangerman43
10-30-2007, 05:30 AM
WARNING rm -r / WARNING

Yes that would wipe your root directory out, and everything under it if you are logged in as root.