1

I was running some simple commands in bash, and one of the commands I ran deleted my current working directory (nothing destructive, I just used git to checkout a branch where that directory doesn't exist).

Now I'm stuck in a deleted directory and cd doesn't seem to want to let me out:

$ cd ..
bash: cd: ..: No such file or directory

What's the easiest way to get out of this directory and back to its parent?

Note that the directory I am in is stored on an NTFS partition, which might explain why cd .. is giving an error, when that behavior doesn't seem to be the norm when executing cd from a deleted directory in Linux.

(Presumably retyping the absolute directory path and giving that to cd will work here, but the path the the parent directory is really long, and I'm lazy. Is there a better way to get to the parent directory than retyping its lengthy absolute path?)

  • just type cd again and you'll switch to your home dir and out of deleted directory. – alexus Jun 25 '15 at 19:45
  • @alexus: the question isn't about getting out of the deleted directory, it's about getting to its parent directory. – fixer1234 Jun 25 '15 at 19:53
1

cd to an absolute path, not a path relative to the deleted directory.

E.g. cd /home/username/

| improve this answer | |
  • 1
    The question asks how to get to the parent directory of the deleted one. – fixer1234 Jun 25 '15 at 19:51
1

Since bash doesn't seem to be recognising relative paths like .. in this case, you'll have to use an absolute path instead. For example:

cd ~/path/to/dir

To simply get back to the parent directory, you could use the following, which is much faster than retyping the absolute path of the directory you're in:

cd "$(dirname "$(pwd)")"

Here's a simple function you can add to your .bashrc file to get this functionality with a single command (also supports an argument which you can use to specify how many levels to move up):

# Allow easily going up multiple directories, even when the current directory
# is a deleted folder on an NTFS share.
function up() {
  local dir=`pwd`
  local n=${1:-1};
  for ((; n>0; n--)) {
    dir=`dirname "$dir"`
  }
  cd "$dir"
}
| improve this answer | |
0

Tabbing

I suggest you read up on SSH, most shells allow for the use of tab to simplify typing commands.

If you type

cd /h

Then press TAB, it will either show a list of directories you can tab too or if only one directory exists in / beginning with the letter h, such as home, it will automatically fill it out to

cd /home

Once you get the grasp of it, it can save you a lot of time.

This solves you being lazy!

Absolute Path

As you know you must CD to an absolute path.

A quick way to do this is to simple run

cd

As this will in most systems take you to the users default directory, usually /home/$user

| improve this answer | |
  • The question asks how to get to the parent directory of the deleted one. – fixer1234 Jun 25 '15 at 19:49
  • 1
    The question also asks 'Is there a better way?' And also stating that the user is lazy, and if the user is going to be using shell frequently, most would say that knowing how to effectively use it, such features as knowing about tabbing are essential. – Craig Lowe Jun 25 '15 at 19:52
0

I'm not sure why you're getting stuck

[alexus@wcmisdlin02 ~]$ mkdir 1
[alexus@wcmisdlin02 ~]$ cd 1
[alexus@wcmisdlin02 1]$ rm -rf ../1
[alexus@wcmisdlin02 1]$ cd ..
[alexus@wcmisdlin02 ~]$ 

... but you can always just run cd or cd ~

| improve this answer | |
  • My guess is it has something to do with the directory being on an NTFS partition. – Ajedi32 Jun 25 '15 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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