Commandline shortcut for current directory similar to ~ for home directory?

Solution 1:

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 - .)

Solution 2:

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

Example:

echo $(pwd)

Solution 3:

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

Solution 4:

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:

mkdir a && mkdir a/b && mkdir a/b/c && mkdir a/b/c2
cd a/b/c
cp /etc/default/grub .
cp /etc/default/grub ..
cp /etc/default/grub ../c2
cd ../../
tree

The output from tree command appears like this:

.
└── b
    ├── c
    │   └── grub
    ├── c2
    │   └── grub
    └── grub

3 directories, 3 files

The . at the top of tree output represents the new current directory a which is the grandparent of a/b/c which we navigated to using the cd ../../ command. Underneath a we see the sub-directories a/b, a/b/c and a/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:

  • In the first copy command (cp) we set the destination to our current directory (c) with ..
  • In the second copy command we set the destination to the parent directory (b) with ...
  • In the third copy command we set the destination to the sibling directory (c2) with ../c2

Then, as stated earlier, we changed the current directory to a and ran the tree command to display all directories and files under a.

Cleanup

After we are done, we remove the three directories and files with:

cd ~/
rm -r tree