8

How can I delete all files and subdirectories from current directory including current directory?

Sam
  • 7,252
  • 16
  • 46
  • 65
Ib33X
  • 6,764
  • 4
  • 28
  • 30

7 Answers7

18

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 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 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 Weiss
  • 52,533
  • 16
  • 102
  • 136
  • 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 Feb 15 '09 at 19:18
  • 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 Weiss Feb 15 '09 at 21:20
  • rm -rfi would ask for every action in this directory, I was thinking something that will ask only once and delete everything. – Ib33X Feb 16 '09 at 08:15
  • read -n1 -p"Do you really want to delete '$(pwd -P)' [yN]?" A && if [ "$(echo $A | tr Y y)" = "y" ]; then rm -rf -- "$(pwd -P)" && cd .. ; fi – Johannes Weiss Feb 16 '09 at 15:23
  • `-P` is the opposite of what I'd expect -- if I'm in a symlinked dir and I try removing it, I'd expect the symlink to be unlinked, not the original dir, and certainly not leaving a broken symlink in its place. – mpen Dec 10 '22 at 08:17
4
olddir=`pwd` && cd .. && rm -rf "$olddir"

The cd .. is needed, otherwise it will fail since you can't remove the current directory.

dwc
  • 24,196
  • 7
  • 44
  • 55
  • 1
    You *can* remove the current directory. – kmkaplan Feb 15 '09 at 14:16
  • kmkaplan, are you *sure* you can delete the current directory with rm? How many operating systems did you base that knowledge on? – dwc Feb 15 '09 at 14:24
  • wont work for a directory called --no-preserve-root for example. – Johannes Weiss Feb 15 '09 at 14:28
  • 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 Feb 15 '09 at 14:29
  • 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 Feb 15 '09 at 14:29
  • 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 Weiss Feb 15 '09 at 14:30
  • I see: rm: "." and ".." may not be removed Why 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 Feb 15 '09 at 14:42
2
rm -fr "`pwd`"
kmkaplan
  • 18,655
  • 4
  • 51
  • 65
1

I think this would be possible under DOS / Windows CMD, but I can't quite find a way to pipe the data between commands. Someone else may know the fix for that?

FOR /F %i IN ('cd') DO SET MyDir=%i | CD .. | RD /S %MyDir%
Sam
  • 7,252
  • 16
  • 46
  • 65
Kristen
  • 4,227
  • 2
  • 29
  • 36
0

You just can go back the target folder's parent folder, then use 'rm -rf yourFolder'. or you can use 'rm -rf *' to delete all files and subfolders from the current folder.

0xTang
  • 776
  • 7
  • 11
0

operating system? on the *NIX-based stuff, you're looking for 'rm -rf directory/'

NOTE: the '-r' flag for 'recursive' can be dangerous!

neoice
  • 2,233
  • 3
  • 19
  • 15
0

Delete current directory with confirmation. Unlink if current dir is a symlink.

rmd() {
  if [[ -z "$1" ]]; then
    >&2 echo "usage: rmd <dir>"
    return 1
  fi

  dir=$(realpath -se "$1") || return 2

  if [[ -L "$dir" ]]; then
    read -s -k "?Unlink '$dir'? "
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      unlink -- "$dir"
    else
      return 3
    fi
  else
    read -s -k "?Recursively delete '$dir'? "
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      rm -rf -- "$dir"
    else
      return 4
    fi
  fi
}

rm.() {
  rmd "$(pwd -L)" && cd ..
}

Usage:

 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ tree .
.
├── bar -> foo
└── foo

2 directories, 0 files
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ cd bar
 /tmp/tmp.29mUflkHKU/bar                                                                                                             base
❯ rm.
Unlink /tmp/tmp.29mUflkHKU/bar?
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ tree .
.
└── foo

1 directory, 0 files
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ cd foo
 /tmp/tmp.29mUflkHKU/foo                                                                                                             base
❯ rm.
Recursively delete /tmp/tmp.29mUflkHKU/foo?
 /tmp/tmp.29mUflkHKU                                                                                                                 base
❯ tree .
.

0 directories, 0 files

Note: read -s -k is for zsh. Use read -n1 -p for bash (source)

mpen
  • 272,448
  • 266
  • 850
  • 1,236