how can I delete all files and sub directories from current directory including current directory?
views:
1049answers:
5
A:
operating system? on the *NIX-based stuff, you're looking for 'rm -rf directory/'
NOTE: the '-r' flag for 'recursive' can be dangerous!
neoice
2009-02-15 14:13:16
+2
A:
olddir=`pwd` && cd .. && rm -rf "$olddir"
The cd ..
is needed, otherwise it will fail since you can't remove the current directory.
dwc
2009-02-15 14:14:14
You *can* remove the current directory.
kmkaplan
2009-02-15 14:16:33
kmkaplan, are you *sure* you can delete the current directory with rm? How many operating systems did you base that knowledge on?
dwc
2009-02-15 14:24:20
wont work for a directory called --no-preserve-root for example.
Johannes Weiß
2009-02-15 14:28:20
dwc: yes I tested Linux, OpenBSD and MacOSX. But I am pretty sure every Unix would do the same and I even think every POSIX system will do it.
kmkaplan
2009-02-15 14:29:27
You can in Linux (just confirmed). Interestingly enough, once the directory is deleted, ls -al reports "total 0" instead of something like total X, with . and .. present
Mikeage
2009-02-15 14:29:50
deleting an open file is absolutely no problem on unix. Being in a directory is nothing more than having a file open. kmkaplan is correct!
Johannes Weiß
2009-02-15 14:30:48
I see: rm: "." and ".." may not be removedWhy not be conservative and have it work everywhere? What if this snippet gets put in a #!/bin/sh script? What if...? It's nicer to have something that works in a more portable fashion.
dwc
2009-02-15 14:42:55
wont work for a directory called --no-preserve-root for example, too
Johannes Weiß
2009-02-15 14:28:50
Johannes: pwd returns an absolute path. It will never start with dashes.
kmkaplan
2009-02-15 14:38:21
kmkaplan: sorry, thats correct! Didn't thing about that. I always use -- and most of the time it is necessary, but wiht pwd it's not.
Johannes Weiß
2009-02-15 14:50:49
+5
A:
Under bash with GNU tools, I would do it like that (should be secure in most cases):
rm -rf -- "$(pwd -P)" && cd ..
not under bash and without GNU tools, I would use:
TMP=`pwd -P` && cd "`dirname $TMP`" && rm -rf "./`basename $TMP`" && unset TMP
why this more secure:
- end the argument list with
--
in cases our directory starts with a dash (non-bash:./
before the filename) pwd -P
not justpwd
in cases where we are not in a real directory but in a symlink pointing to it."
s around the argument in cases the directory contains spaces
some random info (bash version):
- the
cd ..
at the end can be omitted, but you would be in a non-existant directory otherwise...
EDIT: As kmkaplan noted, the --
thing is not necessary, as pwd
returns the complete path name which always starts with /
on UNIX
Johannes Weiß
2009-02-15 14:21:18
As noted above, directories starting with “--” are not a problem: pwd’s result always starts with a “/”.
kmkaplan
2009-02-15 14:42:48
what if you would like to include first a small dialog aka are you sure you want to delete this directory? and make alias for it all?
Ib33X
2009-02-15 19:18:17
You can use rm -rfi instead of rm -rf and it will ask you "rm: remove directory `/tmp/dir_to_be_deleted'?", do you mean that?
Johannes Weiß
2009-02-15 21:20:17
rm -rfi would ask for every action in this directory, I was thinking something that will ask only once and delete everything.
Ib33X
2009-02-16 08:15:45
read -n1 -p"Do you really want to delete '$(pwd -P)' [yN]?" A then rm -rf -- "$(pwd -P)" fi
Johannes Weiß
2009-02-16 15:23:54