# Help with the rm command



## jollewhoever (Apr 26, 2002)

I want to delete a folder, but when i put it in the trash, it says that i don't have the permission  to throw it away... Okey, then i try  su root in the terminal and then rm -d folderpath... But it give me the reply: Directory not empty... Is there any command to delete folders that containing files... Please help me...


----------



## nkuvu (Apr 26, 2002)

There are two ways to do this.

One, do your su root then issue an rm -r _foldername_.  The -r means recursive, so it deletes everything named _foldername_ and below.  *Be careful when using the -r!  You can accidentally delete a lot of things, especially as root!*

Two, cd into the folder, rm everything there, then go back up and rm the directory.  I've never used the rm -d, I usually use the rmdir on empty directories.  I think that they're the same, though.


----------



## jollewhoever (Apr 26, 2002)

Thank you.... Very strange that you can't delete folder containing files...


----------



## nkuvu (Apr 26, 2002)

It's been this way for a long time, even on other operating systems.

I usually use the -r option if I know that the folder is not empty, but I don't use the -r option blindly.  As I mentioned, be careful when using rm, the -r, and/or wildcards.  You can royally hose your system by issuing seemingly simple commands like rm -rf /*.  The -f is force, by the way.  New Unix users have been confused with this command, thinking that it will delete everything in the current directory and all subdirectories.  What it actually does is try to wipe everything on the hard drive.  If you're logged in as root, it will go a long way before failing.  I don't do this on my system, so I don't remember where it fails -- but I think it fails when it tries to delete a folder representing mounted volumes.

  Too much rambling!  Sorry.


----------



## jollewhoever (Apr 26, 2002)

It couldn't be too hard to program a little app that listed a folder and then delete all files that the folder containing. If anyone got the knowledge, please make one and post it here at this forum...


----------



## gdanko (Apr 26, 2002)

I use rm -rf to override read only files.


----------



## nkuvu (Apr 26, 2002)

I meant to mention that, testuser.  Thanks for catching it.


----------



## lethe (Apr 26, 2002)

jolliewhatever:  i have written such a program.  it is a shell script.  i don t think it is worth anything, since it is almost as easy to just type ls, and then rm, and that is much more flexible, but here you go:


```
#!/bin/sh

ls $1
echo "Removing files in $1"
for i in $1/*;
	do
	if [ -f $i ]
		then rm $i
	fi
done
```

it does not do much.  it lists the directory, and then removes the files (and not the directories).


----------



## nkuvu (Apr 26, 2002)

There's another way, lethe.

In your .cshrc in your home directory, add the following line:
alias rm 'rm -i'

From the man pages:

```
-i    Request confirmation before attempting to remove each file, regard-
           less of the file's permissions, or whether or not the standard
           input device is a terminal.  The -i option overrides any previous
           -f options.
```
Of course, this makes you say yes for every file, so this might not be what you want to do.  It really depends on how comfortable you are with rm.


----------



## lethe (Apr 27, 2002)

rm -i is real nice for newbies, but it does not "listed a folder and then delete all files that the folder containing", which jolle asked for.

rm -i tells you the file it is going to delete, and asks you if you really want to delete.   

i suppose i am being too literal.  to have an app that does what she asks for is not too useful.  she probably just needs to learn the various uses of rm and ls.  nevertheless, i provide the app exactly as it was asked for.


----------



## nkuvu (Apr 27, 2002)

> _I said:_
> Of course, this makes you say yes for every file, so this might not be what you want to do. It really depends on how comfortable you are with rm.


Right, lethe.  I said that.    I agree that your script is exactly what was asked for.  My solution is another way to see the files listed before you delete them.

The other thing I wanted to point out is that if you use the -i alias, you can still use -d or -r arguments to rm.  So using built in aliases and flags is slightly more flexible.


----------



## macfreak88 (Apr 28, 2002)

Hi!
i Used -rm and deleted a folder with files i wanted to trow away then i tried to delete a file in the "Applications" folder and it din´t work...Next time i should start Teminal this comes up  :

_
Welcome to Darwin!
tcsh: No such file or directory
tcsh: Trying to start from "/Users/mrtenced"
[www:~] mrtenced% _


why doesnt this appear as usualy??:
_
Welcome to Darwin!
[www:~] mrtenced%_

note... "mrtenced" is my username..please i hope i havn´t done anythinng bad..!!!


----------



## macfreak88 (Apr 28, 2002)

I want to delete an folder named "Norton Solutions Support" because it alleays comes upp an dialog window when i log in that norton antivirus is not propely installed...so i want to delete it...but i cant i wrote "rm -r /Norton Solutions Support" but it didn´t work, why?. I tried to make a new map in the hard drive directory (same as the norton map) an named it "N" and i could delete that!...So i have an idé could it be because it have a space betwen the words?...it coulsd be ...but what should i write instead for those spaces?


----------



## nkuvu (Apr 29, 2002)

I only got a little of what you said -- but I'm reading quickly, since I have to leave soon.

To delete a folder (or file) that has spaces in the name, you can put a backslash (\) in front of each space.  An easier route is to type the "rm " (with trailing space), then find the folder in the Finder, and drag the folder onto the Terminal window.

So for a folder named "Norton Solutions Support" your command might be: "rm -r /Norton\ Solutions\ Support".


----------



## macfreak88 (Apr 29, 2002)

i can´t delete the norton folder it stands that the permission is denied  what should i do then??


----------



## nkuvu (Apr 29, 2002)

Put a sudo in front of the rm command.  
	
	



```
sudo rm -r /Norton\ Solutions\ Support
```
This will ask you for a password; enter your login password.  The sudo command runs a command as the root user, so you shouldn't have any permissions problems then.


----------



## Valrus (Apr 29, 2002)

If you can, I find it best just to use the autocomplete function to deal with those pesky spaces (and any other filename over, say, 5 letters ).

For those who don't know, type some letters in a filename and hit tab - if there's only one file matching what you've typed, the shell will finish it for you. The autocompletion is pretty smart too. Try it.

-the valrus


----------

