Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

When i use the cp or move command in a terminal window, i'm currently with bash in a certain folder like this.

NES@server:~/Desktop/dir1$

And now i wanna copy a file from here ~/anotherdir/dir2 into the current chosen folder in bash (dir1) i would use the command

cp ~/anotherdir/dir2/file ~/Desktop/dir1

does a shortcut string exist to refer to the current chosen directory? So that in this example i don't have to provide the full path to the target dir, but the command knows it should use the current chosen directory in bash? i.e. as ~ stands for the home directory?

share|improve this question

5 Answers 5

up vote 26 down vote accepted

Your current directory is . . So, cp /foo/fam/foo . copies the file to your current directory.

The analogous construction for "one directory up," or the parent directory of your current working directory, is two dots, i.e., .. . (Thanks @djeikyb .)

So, from /usr/house/firstfloor/basement , cd .. takes you one level up to /usr/house/firstfloor.

In the same example (starting from /usr/house/firstfloor/basement, the command cd ../.. would take you to /usr/house .

You can also use $PWD with echo to get your current directory:

echo $PWD

Incidentally, $OLDPWD will give you your previous directory. (Which in bash you can also reach by typing cd - .)

share|improve this answer
3  
Good a time as any to learn that .. means one directory lower. Can be used multiple times, ie cd ../.. –  djeikyb Feb 11 '11 at 14:04
1  
@djeikyb -- you meant above, I'm sure. So, cd .. will take you to the parent directory of your current directory. Good point. –  belacq Feb 11 '11 at 14:13
1  
And I'd mention popd and pushd but that's turning into an essay. –  belacq Feb 11 '11 at 14:24
2  
One small difference to remember: . and .. are filesystem-level, so any program will accept them. On the other hand, shortcuts such as ~ and ~- are part of the shell. –  grawity Feb 11 '11 at 19:46
2  
+1 for cd -; didn't know it existed. –  j-g-faustus Feb 11 '11 at 22:11

You can use $(pwd), it will resolve to the output from the pwd command.

Example:

echo $(pwd)
share|improve this answer
    
Or just echo $PWD. –  ulidtko Feb 11 '11 at 15:52

./ represents the current directory. So you can use command cp ~/anotherdir/dir2/file ./ This will copy the file "file" into currect working directory.

share|improve this answer
2  
You don't need the slash, "." is enough too. slash (as path component delimiter) is only needed if you want to put some other path component after it, but then it's easier to just left ./ since relative paths are meant starting with the current directory anyway :) –  LGB Feb 11 '11 at 14:42
    
i know / is not required but . alone feels confusing to me coz . is also used to execute commands from a file in current shell e.g. ". envsetup.sh". So adding a / at the end just makes it clearer. –  binW Feb 11 '11 at 19:27
1  
Tangentially, if you do ln -s /some/long/path/to/get/to/cake with no second argument, it will put the cake symbolic link in your current directly. Very good for the lazy. Saves a . . –  belacq Feb 23 '11 at 19:51

The environment variable for the current directory is $PWD

echo $PWD
share|improve this answer

Yes (as others noted), current directory is ".", that's why you can start programs/script from the current directory with ./script (just script won't work unless . is not the part of PATH, which is not recommended though). Using $PWD or $(pwd) is a bit overkill, even if others mentioned that, using a single dot character is shorter, for sure :) ".." is the parent directory, for sure "/" is the root. Also nice to mention that "cd -" will put you in the previous directory where you were before you changed cwd (current working directory). It can be also useful in the daily work. A single "cd" command without any other in the command line will put you into "~" (your home).

share|improve this answer
1  
$PWD isn't an overkill when creating symlinks to absolute paths. –  ulidtko Feb 11 '11 at 15:54
    
I thought about the "cp" example where it is, for sure. Even with symlinks it depends on the situation: "ln -s /this/file ." will create a symlink in the current directory pointing file /this/file with name "file" (people often think it's like with "cp" just not copy data, only some kind of link: it's not a correct definition but it's a good start). However, the opposite construction: "ln -s . /this/dir" is a somewhat interesting (create a symlink as /this/dir with refers for the current directory), but I think it's out of the scope of the original topic, and it was not the question either. –  LGB Feb 11 '11 at 22:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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