29

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?

belacqua
  • 23,120
NES
  • 33,195

8 Answers8

49

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

muru
  • 197,895
  • 55
  • 485
  • 740
belacqua
  • 23,120
  • 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. – belacqua Feb 11 '11 at 14:13
  • Or is the only the use of 'parent directory' by consensus, and 'up' and 'down' are not the standard conventions I thought they were? – belacqua Feb 11 '11 at 14:23
  • 1
    And I'd mention popd and pushd but that's turning into an essay. – belacqua Feb 11 '11 at 14:24
  • 3
    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. – user1686 Feb 11 '11 at 19:46
  • You can also always brute force it by using something like this: cp something/else "\pwd`"`. But that's silly. – Naftuli Kay Feb 11 '11 at 20:35
  • 2
    +1 for cd -; didn't know it existed. – j-g-faustus Feb 11 '11 at 22:11
  • @jgbelacqua I think you are right on the standard convention. Occasionally I fall into my original (wrong) imagining of / as the root of a tree, with many branches of directories growing upwards. Of course the root metaphor really refers to the base stem and its many downward veins. Oops. – djeikyb Feb 21 '11 at 09:15
  • @djeikyb And yet the root to leaf metaphor does make sense, and is used in other settings. But 'sub'-directory is certainly used in English as convention. And I see that this appears normal in French, Spanish, German, Danish at least (going on cognates). Maybe super- or sur-directory just didn't work in English. I need someone to point me to a filesystem history link. :P – belacqua Feb 21 '11 at 18:14
  • Haha, super-directory just doesn't have a ring to it. The parent-child metaphor works well; clearly emphasises up vs down. – djeikyb Feb 21 '11 at 22:13
  • so then: ----> cp textfile.txt ./newDir ----> will copy the textfile which exists in the current directory into the newDir subdirectory? – BenKoshy Feb 11 '16 at 23:38
8

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

Example:

echo $(pwd)
João Pinto
  • 17,159
4

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

binW
  • 13,034
  • 5
    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 . . – belacqua Feb 23 '11 at 19:51
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
2

The environment variable for the current directory is $PWD

echo $PWD
Takkat
  • 142,284
2

You can use the dot (.), the ~+ tilde expansion, the pwd command or the $PWD variable to represent the current working directory (CWD). All these commands can do that:

  1. mv file_old_dir .
  2. mv file_old_dir ~+
  3. mv file_old_dir $(pwd)
  4. mv file_old_dir $PWD
0x2b3bfa0
  • 8,780
  • 6
  • 36
  • 55
1
cp ~/anotherdir/dir2/file `pwd`

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.

1

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

LGB
  • 1,567
  • 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