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?
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 commandcd ../..
would take you to/usr/house
.You can also use
$PWD
withecho
to get your current directory:Incidentally,
$OLDPWD
will give you your previous directory. (Which inbash
you can also reach by typingcd -
.)You can use $(pwd), it will resolve to the output from the pwd command.
Example:
./ represents the current directory. So you can use command
cp ~/anotherdir/dir2/file ./
This will copy the file "file" into currect working directory.To use the current directory as the destination directory use a single dot '
.
'Long Answer
Using your example you would type:
cp ~/anotherdir/dir2/file .
To see the dot
.
,..
and../../
directory names in action, copy and paste the following commands into your Terminal:The output from tree command appears like this:
The
.
at the top of tree output represents the new current directorya
which is the grandparent ofa/b/c
which we navigated to using thecd ../../
command. Underneatha
we see the sub-directoriesa/b
,a/b/c
anda/b/c2
Line by line analysis
First we created 4 directories on one line by using
&&
to join multiple lines together.Then we changed to the directory
a/b/c
, which is the current directory for the following copy commands:cp
) we set the destination to our current directory (c) with.
...
.../c2
Then, as stated earlier, we changed the current directory to
a
and ran thetree
command to display all directories and files undera
.Cleanup
After we are done, we remove the three directories and files with:
The environment variable for the current directory is $PWD
You can use the dot (
.
), the~+
tilde expansion, thepwd
command or the $PWD variable to represent the current working directory (CWD). All these commands can do that:mv file_old_dir .
mv file_old_dir ~+
mv file_old_dir $(pwd)
mv file_old_dir $PWD
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).
The back ticks mean to run the command that is between them and replace it all by the resulting output, in this case the working directory ("pwd" means "print working directory").
Of course, I prefer the "." version. Added this one only for completeness.