giskard22
09-20-2005, 02:08 PM
I noticed that 'rm -Rf' doesn't delete items that begin with a period. The 'rm' command doesn't seem to have any options that change this behavior. How do you truly delete all the items in a folder without deleting and recreating the folder?
yellow
09-20-2005, 02:50 PM
Use a follow up of "rm -f .*"?
nkuvu
09-20-2005, 02:54 PM
Well technically rm -Rf won't remove anything. You haven't specified a filename.
But pedantry aside, consider using two filenames on the command line, * and .* (example follows):
/tmp nkuvu$ touch foo.bar foo.baz .foo .waffle
/tmp nkuvu$ ls -al
total 0
drwxr-xr-x 6 nkuvu admin 204 Sep 20 12:50 .
drwxr-xr-x 21 nkuvu admin 714 Sep 20 12:49 ..
-rw-r--r-- 1 nkuvu admin 0 Sep 20 12:50 .foo
-rw-r--r-- 1 nkuvu admin 0 Sep 20 12:50 .waffle
-rw-r--r-- 1 nkuvu admin 0 Sep 20 12:50 foo.bar
-rw-r--r-- 1 nkuvu admin 0 Sep 20 12:50 foo.baz
/tmp nkuvu$ rm * .*
rm: "." and ".." may not be removed
/tmp nkuvu$ ls -al
total 0
drwxr-xr-x 2 nkuvu admin 68 Sep 20 12:50 .
drwxr-xr-x 21 nkuvu admin 714 Sep 20 12:49 ..
Ignoring the warning that "." and ".." may not be removed, works fine.
hayne
09-20-2005, 02:57 PM
I noticed that 'rm -Rf' doesn't delete items that begin with a period. The 'rm' command doesn't seem to have any options that change this behavior. How do you truly delete all the items in a folder without deleting and recreating the folder?
yellow has already given you the answer.
I'd just like to point out that it this isn't really related to the 'rm' command at all. The 'rm' command merely takes a list (space-separated) of files as part of the command. When you run the command:
rm *
the shell expands the * into a list of all files (not starting with a dot) in the current folder. Thus the 'rm' command does not deal with the * at all - it gets a list of files. You can see the list of files that it would receive by running the command:
echo *
The above is true for almost all Unix commands/utilities - they don't usually deal with wildcards at all. Almost all wildcarding is handled by the shell, not by the command. This means that in the relatively unusual case where a command does handle wildcards, you need to take special steps to prevent the shell from expanding the wildcards before they get to the command.
For example, if you are using the 'find' command to find all files/folders under the current folder whose names start with "foo", you would do it with:
find . -name 'foo*'
It wouldn't work if you omitted the single-quotes around foo* since then the shell would do the wildcard expansion.
giskard22
09-20-2005, 04:45 PM
Thanks, all. I kind of figured that .* would do it, but I didn't want to test it. I know the rm man page says . and .. are ignored, but 'rm' + wildcards + ignorant user = data loss. :)
hayne
09-20-2005, 05:18 PM
I kind of figured that .* would do it, but I didn't want to test it. I know the rm man page says . and .. are ignored, but 'rm' + wildcards + ignorant user = data loss.
If you ever are concerned that a wildcard might not be doing what you want (e.g. in an 'rm' command), you should use the 'echo' command ahead of time to verify what the wildcard will expand to.
In zsh, you can hit ^D after a glob (like *) to see what it expands to, if you have completion on. (Hitting tab instead of ^D will cycle through the options rather than simply displaying the expansion.)
bash seems to do this when hitting tab.
jashmenn
10-18-2005, 11:59 PM
You could also try:
ls -a | xargs rm
the ls -a shows all of the files and xargs takes the output of whatever the first argument is and does it each once for each line. This is also helpful if you have more items in a folder than the default rm will allow (10,000+ ... like from a spider download or something).
I think xargs is installed by default, but if not you can get it from darwinports or something.
-nate murray (http://www.natemurray.com)
Jonne
10-19-2005, 08:50 AM
While on the subject,
why can't I remove a garbled file from trash? I don't know where it's from and the name shows up different in OSX and terminal. The size is 0.
/jondah/.Trash/Rescued Items root# ls
?????????????????
/jondah/.Trash/Rescued Items root# rm *
rm: ƌƅĠĠĞĞŲ㖒: No such file or directory
hayne
10-19-2005, 09:19 AM
why can't I remove a garbled file from trash? I don't know where it's from and the name shows up different in OSX and terminal. The size is 0.
/jondah/.Trash/Rescued Items root# ls
?????????????????
/jondah/.Trash/Rescued Items root# rm *
rm: ????????: No such file or directory
The low-level system sometimes has some problems when the filenames have strange characters in them. There have been a few other threads relating to this:
http://forums.macosxhints.com/showthread.php?t=15814
http://forums.macosxhints.com/showthread.php?t=10758
So what happens when you empty the Trash using Finder? Does that work?
If not - i.e. if the file seems to be undeletable, then I'd be interested in finding out more about this so we could find a solution.
It would be helpful in this case if you could email me a copy of the file, or at least tell me the full name of the file as shown by:
ls | vis -o
or as shown by my 'listdir' script given in the first of the above links.
Jonne
10-19-2005, 09:51 AM
It wont be deleted through Finder, can't move it either but I can move the surrounding folder.
here's the result:
/jondah/.Trash/Recovered files root# ls | vis -o
639020
/jondah/.Trash/Recovered files root# find . -inum 639020 -exec rm -f {} \;
find: .: No such file or directory
I'll mail a copy of it.
Cheers.
Las_Vegas
10-20-2005, 05:07 PM
Try typing 'sudo rm -f ' (Note the space at the end. Don't type the quotes.) and then drag the file into the terminal window. Any illegal characters will be proceeded with a backslash for you.