Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Unix&Linux>>Stupid Unix questions. Remove contents of directory.

Stupid Unix questions. Remove contents of directory.

AHunter3
04-27-2005, 03:14 PM
Two questions, actually. I often need to ssh into the Sun box we use as a webserver and remove directories populated with subdirectories and files and whatnot. I know of "rm -r*" and/or "rm -r" (doesn't work on the Sun), and of "rm -ri" (which does work on the Sun but i have to confirm every step). The Sun is running SunOS 5.9

a) What command can I issue to nuke a directory without having to confirm each file and subdirectory? (A single confirm, "Do you really want to nuke that folder?" would be OK, but not necessary).

b) I also need the protocol for nuking the contents of a known directory but leaving the now-empty directory itself intact. I haven't a clue on this one.

voldenuit
04-27-2005, 03:25 PM
There seems to be some extremely stupid wordfilter screwing up my contribution.

I've tried to figure it out, but now I'm fed up.

Read the attachment for some nifty tips.

If you have any idea what is wrong with my text, don't hesitate to tell me.

dakini
04-27-2005, 03:26 PM
a) What command can I issue to nuke a directory without having to confirm each file and subdirectory? (A single confirm, "Do you really want to nuke that folder?" would be OK, but not necessary).

rm -rf would probably do that. The -f option means it won't prompt you, it'll just rm everything.

b) I also need the protocol for nuking the contents of a known directory but leaving the now-empty directory itself intact. I haven't a clue on this one.

hmm.... how about cd into the directory and do rm -r * ? That should empty out the directory.

hayne
04-27-2005, 03:28 PM
Might I also suggest that reading 'man rm' on the Sun box could be helpful?

mark hunte
04-27-2005, 04:00 PM
does the rm -ri *
confirm every Directory and file or just the Directories.

I ask this because, yesterday at least 20,000 images on our system was killed by someone using rm -r *

but also adding an extra * on the end by mistake.

yellow
04-27-2005, 04:58 PM
On SunOS 5.9, I use /usr/bin/rm -rf

AHunter3
04-27-2005, 05:19 PM
OK, rm -rf works fine, thanks!

hayne
04-27-2005, 06:55 PM
does the rm -ri *
confirm every Directory and file or just the Directories.
Watch this:

% mkdir Test
% cd Test
% touch foo bar
% mkdir Subdir
% cd Subdir
% touch rez mis
% cd ../..
% rm -ri Test
remove Test? y
remove Test/bar? y
remove Test/foo? y
remove Test/Subdir? y
remove Test/Subdir/mis? y
remove Test/Subdir/rez? y


I ask this because, yesterday at least 20,000 images on our system was killed by someone using rm -r *

but also adding an extra * on the end by mistake.
I'm curious what the person who did that was intending to do.
I.e. was the problem that they didn't understand what the "-r" did or they didn't understand what the "*" did, or what?
Having an extra "*" at the end shouldn't matter.


% mkdir Test
% cd Test
% touch foo bar
% mkdir Subdir
% cd Subdir
% touch rez mis
% cd ..
% ls
Subdir bar foo
% rm -ri **
remove Subdir? y
remove Subdir/mis? y
remove Subdir/rez? y
remove bar? y
remove foo? y

acme.mail.order
04-28-2005, 06:19 AM
b) I also need the protocol for nuking the contents of a known directory but leaving the now-empty directory itself intact. I haven't a clue on this one.

Save this script somewhere convenient (/usr/bin ? ) and call it `neutron`


#!/bin/bash
case $1 in

"" ) echo "Usage: neutron <path>";;
"-h" ) echo "Erases all files under the specified directory, ";;
* ) find $1 -type f -exec rm {} \; ;;
esac


Write back if you want it dissected, and keep the following two quotes in mind:

---------------

Unix was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things.
-- Doug Gwyn

Unix gives you just enough rope to hang yourself -- and then a
couple more feet, just to be sure.
-- Eric Allman

voldenuit
04-28-2005, 07:24 AM
Unix gives you just enough rope to hang yourself -- and then a
couple more feet, just to be sure.
-- Eric Allman [/i] Allman also lived up to this quote by writing the ugliest, most security-bug-riddled MTA ever to trample the earth.

AHunter3
04-28-2005, 10:09 AM
dakini: b) I also need the protocol for nuking the contents of a known directory but leaving the now-empty directory itself intact. I haven't a clue on this one.

hmm.... how about cd into the directory and do rm -r * ? That should empty out the directory.

Yeah, I actually built one version that approached it that way.

Problem is, there's not one single folder for which I want to nuke the contents and leave the folder itself intact; there's approximately 90 of them, (a quantity slowing growing over time), and they'll have this done to them on a roughly monthly basis, so it's to be a scripted operation. So picture this: the 11th or 29th command to cd into one of these folders fails to switch the focus because for some inexplicable reason that folder doesn't exist any more. Next command issued is "rm -r*", and did someone mention something about a rope?

I looked at that script, thought about how elegant and clean it was, imagined the havoc from one little misfire on one little "cd" line, and instead wrote a version that removes the directory itself (rm -rf /path/to/directory) and then recreates it, chowns it, chmods it, and does other things to it to cause the new, empty folder to be in every way like the old one aside from being empty.

Messy, clunky, many more command steps, far from elegant.... but I think I'll have fewer nightmares this way.

hayne
04-28-2005, 11:36 AM
So picture this: the 11th or 29th command to cd into one of these folders fails to switch the focus because for some inexplicable reason that folder doesn't exist any more. Next command issued is "rm -r*", and did someone mention something about a rope?

You are allowed to use paths in an 'rm' command, you know.
Instead of cd-ing into the folder and using 'rm -r *', you should use:
rm -r /full/path/to/the/folder/*

mark hunte
04-28-2005, 12:19 PM
I'm curious what the person who did that was intending to do.
I.e. was the problem that they didn't understand what the "-r" did or they didn't understand what the "*" did, or what?
Having an extra "*" at the end shouldn't matter.

I'm sure they knew what they where doing with the "-r"

What they where meant to do was kill some of the directories and files organised by date. ( I do not actually know the structure as I am an end user ) All I know is what they told me the reason all the dir/files got killed, which was
rm -r * */j2005/m04/t08
instead of
rm -r * /j2005/m04/t08

for some reason the extra "*" killed all directories in the /j2005/m04/ directory

I have never used rm much so I do not know to much of its use, but I did notice the lack of a confirm option -i , so wondered why they did not use it.

having to hit "y" thousands of time would be the answer.

hayne
04-28-2005, 12:58 PM
What they where meant to do was kill some of the directories and files organised by date. ( I do not actually know the structure as I am an end user ) All I know is what they told me the reason all the dir/files got killed, which was
rm -r * */j2005/m04/t08
instead of
rm -r * /j2005/m04/t08


Neither of those commands makes much sense.
The first one is saying to delete all files and folders under the current folder (via the *) and also to delete all files/folders under the current folder whose names end in "j2005/m04/t08" (via the */j2005/m04/t08).
This last part (the "also") is obviously redundant since all means all.

The second command is saying to delete all files and folders under the current folder (via the *) and also to delete the folder named "/j2005/m04/t08" and all files/folders under it (the folder "/j2005" is something at the top level of the hard drive).

I suspect that perhaps what was wanted was something like this:

rm -r j2005/m04/t08/*
(Note the lack of a slash (/) in front of the j2005 and the fact that the * is at the end)

mark hunte
04-28-2005, 01:06 PM
Thanks, I will go back and check with them.

The lines are pasted from the email they sent me. so they are exactly what has been described to me.??

maybe I am getting it third party in the email.


 

TOP

Windows Server Outsell
Unix Signals And C++ E

For more info

Unix Signals And C++ E
Windows Server Outsell
ssh setup for password
Bash script does not w
esc code 
ARD send unix command 
question about binarie
Scanning mail 
Issuing multiple comma
How do I install Linux

News Archive

/etc/hosts? 
Manually Start a Start
mounting missed hard d
Using Netinfo in Singl
darwin/bsd login probl
system_profiler and fi
Send mail from script 
mounting a drive 
OS X disk first aid ha
system.log shows steal

Related stories:

Application packages
Puzzlement: -bash: /sw/bin/init.sh: No such file or directory
Check that a process hasn't crashed/quit
JAVA help, please!
fat32+disk image
Change scroll speed of 'cat' command?
CUPS n00bie question

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics