![]() |
removing the contents of a directory
Is there a way in terminal to remove all the contents of a folder, but not the folder itself? If not, perhaps an easy way to do it in Applescript? Thanks.
-Bell |
Yes.
rm -r /path/to/folder/ don't forget that trailing slash. |
thanks baf, but your idea doesn't work, it still deletes the folder. However, it got me thinking and I believe I figured it out. Use this:
rm -r /path/to/folder/* That seems to do the trick. Thanks again. |
Quote:
Code:
cd the/directory/in/questionBecause it's scary, I'll break it down and explain the pieces, on the theory that an incantation you understand is easier to type correctly:
The net effect is to remove everything in the current directory except '.' (the directory itself) and '..' (its parent). |
Quote:
|
Can I ask a supplementary question to this topic please?
How would one delete all files in a folder that are older than say 3 months? |
find /path/to/folder \! \( -newerct '3 months ago' \) -delete
Of course test it without the destructive option :D |
Thanks acme but it doesn't seem to work. My unix is not so hot but do I really put '3 months ago' into the command?
|
Yes it should work with that syntax. Try copying that line except -delete and replace path to folder with something else.
|
kaptagat, if it doesn't work the next time, please copy/paste from your Terminal the exact command you entered and the exact results, so we can see where it went wrong.
Trevor |
Quote:
Quote:
Shell prints an error: Code:
$ rm -i * .*(we can't always be sure the local rm has that particular feature) -HI- |
cd /path/to/folder
/bin/rm -rf * .* |
Quote:
|
Quote:
that might also delete stuff in the *parent* folder. Just try it with ls to see what could happen (on other systems): $ cd ~/.Trash:eek: The shell's pattern matching facility will include stuff in the parent (..*) ls just lists what the shell "asked" it to. So, further design is required, to filter out all ..* matches that occur. rm wasn't always so thoughtful to do that, as it appears to be today. -- For example!!! :: DANGER :: Apple's own srm does not seem friendly in that regard!!! |
Taking a cue from ganbustein,
this also seems to do the trick: rm * .[^.]* |
Quote:
|
Excellent!
|
Interesting rm safety note: Keeping files called "-r" in directories can be a Bad Idea (not that I'm sure why you would have this file, or if there would be a situation where you'd think it'd be a good idea to use "rm *" in a directory to remove the top level files but keep everything lower down the directory tree).
Keeping a file called "-i" in a folder will definitely stop you rm-ing anything by mistake, but it's a pain in the backside if you do actually want to delete a bunch of stuff without confirmation. |
Quote:
especially when we're 'parked' in the directory at the time. Wrapping ganbustein's magical patterns with some brace expansion syntax, and applying a $PWD preamble, we could do... ls -l "$PWD"/{*,.[^.],.??*} Simply change ls -l to rm -r ...after a full backup! :D [just kidding -- the listing looks perfectamundo] -- Or hell... full path not needed to dust off "-i" files. A basic ./ should do it: rm -r ./{*,.[^.],.??*} |
Quote:
Both "..." and "..secret_stash" are visible in Finder! :rolleyes: |
Quote:
As for dealing with -r and -i files... as man rm mentions, rm uses getopt() to parse its arguments, so the usual -- option works. rm -rf -- * .[^.] .??*Hmm.. without that, a file named -- might prevent an unpredictable subset of your files from being deleted. Of course, prefixing with ./ works also on systems where rm does not look for --. Back in the olden days when I was learning Unix, I seem to recall that ? would not match a period. My first attempt was rm -rf * .? ..?* ...*but ? doesn't work like that anymore (if it ever actually did). I just thought I'd mention that. This thread seems to be accentuating that every program still has at least one bug, even if the program is a one-liner. Anyone but me remember IEFBR14? Of course you can google it, but do you remember it without looking it up? (I don't know if the fact that today is my 62nd birthday, and I've been programming since I was 19, makes that easier or harder to remember.) |
Quote:
-- I found we can also circumvent the "-i" effect by changing the order so that the bare asterisk is not first. For example... using ls with the -f option (to force it to list items in the order they were submitted): Code:
$ ls -lanf * .[^.] .??*So the -i file isn't shown. Now, reordering... Code:
$ echo .[^.] .??* *As can be seen, I've been playing around with some other weird filenames: .* and .\* |
Quote:
find . -delete Code:
% echo "rm -rf * .[^.] .??*" | wc -c |
That's weird... find . -delete
finds '.' but it doesn't delete '.' How come (no error nothin')? |
Quote:
Code:
if (strcmp(entry->fts_accpath, ".") == 0 || |
:D :D :D Thread Closed! :D :D :D No Further Information Necessary! ^^^ Well, there's still something to be said for the pattern approach: it can be done from *outside* the folder. (I've already added a Bash function to implement it... so it needs to be rationalized). rm -fr foo/{.[^.],.??*,*} will clear foo's content, but folder foo survives. ;) |
Quote:
Code:
Hal: here's a start on your function: Code:
rmstar () Add whatever additional error-checking code you deem appropriate. A nice refinement might be to loop across "$@" instead of just using "$1". Or have it accept files as well as directories. Or just ask yourself how often you're really going to need this. After all: rm -rf someDirectorywill almost always be good enough. |
Quote:
|
Can I ask to go on another tangent? :)
I would like to be able to remove all the thumbnail files iPhoto creates. In each "primary" folder hierarchy ~/Pictures/iPhoto\ Library/<year>/<month>/ there can be a "Thumbs" directory containing these thumbnail files. I've been trying to use a combination of 'find' (to find the "Thumbs" directory and 'xargs' to delete the contents of these folders, but I can't seem to get it to work. As usual, I'm sure I'm overlooking something quite simple... TIA |
This one is very aggressive, the Four Horsemen of `find` annihilating entire directory trees at a stroke:
find . -iname '*thumb*' -exec rm -rf {} \; Only two horses: find . -type d -name thumb -exec rm {}/*jpg \; |
Quote:
find . -type d -name Thumbs -exec rm {}/*jpg \; Simply to guard against the possibility (however unlikely) that there might be a folder with "thumb" somehow in its name. Thanks! |
| All times are GMT -5. The time now is 10:13 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.