First question here, I'm usually on StackOverflow.
I need to delete all the contents of a directory, but not the directory itself as I don't have permission to delete that actual directory. This seems simple, but I'm unable to find the command for this. Thanks in advance.
or
if you want to see what is happening.
Chaos -- You are incorrect in your concern that rm will ever delete ..
I did a quick search and found the man pages to rm from the 7th edition unix manual at http://plan9.bell-labs.com/7thEdMan/vol1/man1.bun where it says:
DIAGNOSTICS
Given that 7th edition unix is the parent of all modern unixes, and was released in 1979, I would say that it is an emphatically safe thing to do. It doesn't do anything, but it causes no harm whatsoever.
Now, there are other programs like chown that will happily "descend" into .. and cause all sorts of chaos if you do wacky things like "chown -Rh user .*" but rm is not chown.
Easy version if current directory is fine to work with:
Harder version if current directory is no good:
find /path/dir/ -type f -print|xargs rm
This is one of those dark areas of unix that can get sticky fast.
Each of the above examples tickles a long-standing bug in unix that these days people just regard as a cute little personality quirk.
find . | xargs rm
won't work if there are wacky filenames in the directory like newlines or white space. You may even start deleting other files not in the directory if there is a filename with a ; in it. Who knows what happens if there is a filename with a ` in it. Just ask little bobby drop tables. Things can get exciting quickly.
Bill Weiss's comment correctly points out that modern versions of find and xargs that properly use nulls as the field separators for each thing that find finds if you use the -print0 in find and -0 in xargs. Not being a trusting sort, and having cut my teeth on older, randomly broken versions of unix, I tend to be wary of these newfangled gnuisms, even though they work quite well and in this case are the correct answer to this specific problem.
rm -r /path/to/directory/* won't work if you've got 10,000 files in that directory.
Now -- mostly I just don't bother to do this right, so I'll use rm -rf and look at the error if there is an error. If I'm 100% sure there aren't wacky files, I might use find and xargs, though I don't really trust those.
If I'm doing it in a script that runs automatically, and I have no idea how long this is going to be used or who is going to use it, I try to do it the right way.
I can't really think of a quick, tidy, and reliable way to do this but I think I could do it with a bourne shell script like:
Now -- this is safe because the for loop protects the command line of "rm" from having a billion inputs and the double quotes around the variable protect it from wacky things like escape characters or semicolons or other meta garbage. It is also far slower than the find + xargs.
So I guess the right answer is "There is no program to do that. You have to write a program to do that reliably." I guess that's what stallman et all did with find and xargs...
Why not just:
You'll get an error message since you don't have permission to remove the directory. But it will also remove everything in the directory, including those troublesome hidden files.
I believe one could use: rm -rf /test/*
You can try