How can I delete all files and subdirectories from current directory including current directory?
Use the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory.
To remove a directory that is not empty, use the rm command with the -r option for recursive deletion. Be very careful with this command, because using the rm -r command will delete not only everything in the named directory, but also everything in its subdirectories.
Delete a Directory ( rm -r ) To delete (i.e. remove) a directory and all the sub-directories and files that it contains, navigate to its parent directory, and then use the command rm -r followed by the name of the directory you want to delete (e.g. rm -r directory-name ).
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:
--
in cases our directory starts with a dash (non-bash: ./
before the filename)pwd -P
not just pwd
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 spacessome random info (bash version):
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
olddir=`pwd` && cd .. && rm -rf "$olddir"
The cd ..
is needed, otherwise it will fail since you can't remove the current directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With