24
xx@xx-PC ~/xampp/htdocs/sites
$ rmdir /s "yo-2"
rmdir: `/s': No such file or directory
rmdir: `yo-2': Directory not empty

xx@xx-PC ~/xampp/htdocs/sites
$ rmdir "yo-2"
rmdir: `yo-2': Directory not empty

I cant seem to get rmdir to work in git bash. Its not in a git repo and I've tried the above. Mkdir works as expected, why doesnt this?

7 Answers 7

39

rmdir will not work if directory is empty

Try

  rm -rf yo-2

git-bash is a Linux like shell

2
  • 2
    This works except for the node components folder - saying 'directory not empty' OR 'file / path too long'. Any ideas how to get round this? Commented Mar 11, 2014 at 13:26
  • 2
    rm could fail when doing rm dir/* and node contains a lot of files, and substitution fails, or when there is a really long path, or maybe dealing with path names that contains strange chars, or when there are open files into the path. Any clue? Commented Mar 11, 2014 at 14:05
8

If you are trying to remove an entire directory regardless of contents, you could use:

rm <dirname> -rf
2
  • This works as the marked answer does but is much slower. Thanks for your input though! Commented Mar 11, 2014 at 13:27
  • 2
    it is the same command how did it run slower? thanks for the feedback
    – chrcoe
    Commented Mar 12, 2014 at 16:15
1

just use the command below:

rm -rfv mydirectory

0

After trying out a couple of other commands, this worked for me:

rm dirname -rf
0

A bit late, but I believe it still can help someone with performance problems on Windows systems. It is REALLY FAST to delete on Windows using git bash comparing with just the ordinary rm -rf. The trick here is to move the file/directory to another random name in a temporary directory at the same drive (on Windows) or at the same partition (on *nix systems) and invoke the rm -rf command in background mode. At least you don't need to wait for a blocking IO task and OS will perform the deletion as soon it gets idle.

Depending on the system you are using you may need to install the realpath program (ie macOS). Another alternative is to write a bash portable function like in this post: bash/fish command to print absolute path to a file.

fast_rm() {
    path=$(realpath $1) # getting the absolute path
    echo $path
    if [ -e $path ]; then
        export TMPDIR="$(dirname $(mktemp -u))"
        kernel=$(uname | awk '{print tolower($0)}')
        # if windows, make sure to use the same drive
        if [[ "${kernel}" == "mingw"* ]]; then # git bash
            export TMPDIR=$(echo "${path}" | awk '{ print substr($0, 1, 2)"/temp"}')
            if [ ! -e $TMPDIR ]; then mkdir -p $TMPDIR; fi
        fi
        if [ "${kernel}" == "darwin" ]; then MD5=md5; else MD5=md5sum; fi
        rnd=$(echo $RANDOM | $MD5 | awk '{print $0}')
        to_remove="${TMPDIR}/$(basename ${path})-${rnd}"
        mv "${path}" "${to_remove}"
        nohup rm -rf "${to_remove}" > /dev/null 2>&1 &
    fi
}

# invoking the function
directory_or_file=./vo-2
fast_delete $directory_or_file
0

I have faced same issue. this is worked for me

rimraf is a Node.js package, which is the UNIX command rm -rf for node, so you will need to install Node.js which includes npm. Then you can run:

npm install -g rimraf

Then you can run rimraf from the command line.

rimraf directoryname

visit https://superuser.com/questions/78434/how-to-delete-directories-with-path-names-too-long-for-normal-delete

I found this solution because npm itself was causing this problem due to the way it nests dependencies.

-1

Late reply, but for those who search a solution, for me the

rm <dirname> -rf

wasn't good, I always get the directory non-empty or path too long on node directories.

A really simple solution : Move the directory you want to delete to the root of your disk (to shorten your path) and then you can delete it normally.

1
  • This is a problem if you have too long a pathname, but it shouldn't be relevant to the question here. Commented Apr 18, 2016 at 12:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.