# Shortcut for fast recursive removal

Sometimes I want to delete large swaths of temporary files recursively, and recycle the directory name without waiting for the command to complete. (For example, if I want to nuke and re-checkout a version control working copy.)

While this doesn't happen all that often, when it does I usually use a command like:

mv "$oldname" dienow && rm -rf dienow &  I could create a function or alias for this, of course, but without doing that is there a shorter way to express the above? - That's pretty short already. – Mat Jan 4 '12 at 15:40 Can you clarify what you're asking for? As you've written your example, it looks equivalent to rm -rf "$oldname" &. I assume that doesn't do what you want, so a better example would be helpful. – Warren Young Jan 4 '12 at 16:22
@WarrenYoung it is equivalent, but frees up the name "$oldname" so I can use it without waiting for all its contents to be unlinked first. – kojiro Jan 4 '12 at 17:11 In that case, then I'd say a function or alias is the way to make it shorter. – Warren Young Jan 4 '12 at 20:51 ## 3 Answers I would stick with two separate commands: mv "$oldname" dienow
rm -rf dienow


This way, if you accidentally recall a line from your history, you don't risk causing major damage by running a single command. The mv command doesn't delete "$oldname" even if it's the new version, and the rm command only deletes something that you've already declared as to-be-deleted. If you feel lucky and insist on a single command, make it a function: mv_then_rm () { mv -- "$1" "$1.TO_BE_DELETED" && rm -rf -- "$1.TO_BE_DELETED" &
}


Here's a multi-parameter version that ensures the “to be deleted” directory doesn't exist yet.

mv_then_rm () {
local d tmpdir tmpdirs
tmpdirs=()
for d; do
tmpdir=$(mktemp -- "$(dirname -- "$d")/.deleting.XXXXXXXXXX") tmpdirs+=$tmpdir
mv -- "$d" "$tmpdir"
done
rm -rf -- "$tmpdirs" & }  - +1 for mktemp. – James Sneeringer Jan 6 '12 at 18:11 you are missing the recreation of "$oldname".

mv "$oldname" dienow && mkdir "$oldname" && \rm -rf dienow &


I prefer to use \rm just in case someone's aliased rm to something like rm -i

clean_old_now(){
\mv "$oldname" dienow && mkdir "$oldname" && \rm -rf dienow &
}


clean_old_now(){
read -p "Are you sure?: " sure
if [[ $? = 0 ]] && [[ "$sure" = "y" ]]; then
\mv "$workdir/$oldname" $workdir/dienow && mkdir "$workdir/$oldname" && \rm -rf$workdir/dienow &
fi
}


When adding explicit paths make sure the mv stays in the same filesystem to prevent the data being copied

-
Good idea on the prompt; however, I have to point out that (in clean_old_now) it's not a good idea to use the [ command without quoting variables, and the == makes it unportable without gaining anything. I gather this is bash, so if you want to edit it to use the [[ keyword or quote the vars and get rid of the extra equals sign, I can +1 it. – kojiro Jan 5 '12 at 12:54
done. One may embellish as much as wanted. $sure could be made local, tests for$workdir $oldname being set, etc. just trying to stick with brevity for now. Personally I'd add -v switches to mv and mkdir – bsd Jan 5 '12 at 13:11 +1 . Good one :-) – Nikhil Mulley Jan 5 '12 at 13:47 I changed || to &&, surprised no one caught that, self included – bsd Jan 6 '12 at 12:32 A very pragmatic approach would be to move the directory to /tmp and let the reboot of the machine do the rest. Or establish a cronjob that cleans up periodically (once an hour). mv "$oldname" /tmp/forgetme


And as cronjob

0 * * * * rm -rf /tmp/forgetme

-
This assumes /tmp is on the same filesystem as "\$oldname". If that's not the case, it would be very slow. – kojiro Jan 5 '12 at 15:35
True! You are right, I did not think about that. – ddeimeke Jan 5 '12 at 16:44