Copy file to current directory?

237,438

Solution 1

You can refer to the current directory with a dot (.).

So in your case:

cp /path/to/source.txt .

Solution 2

For the destination directory use a single dot '.'

Long Answer

From your home directory type the following:

rick@dell:~$ mkdir a && mkdir a/b && mkdir a/b/c && mkdir a/b/c2
────────────────────────────────────────────────────────────────
rick@dell:~$ cd a/b/c
────────────────────────────────────────────────────────────────
rick@dell:~/a/b/c$ cp /etc/default/grub .
────────────────────────────────────────────────────────────────
rick@dell:~/a/b/c$ cp /etc/default/grub ..
────────────────────────────────────────────────────────────────
rick@dell:~/a/b/c$ cp /etc/default/grub ../c2
────────────────────────────────────────────────────────────────
rick@dell:~/a/b/c$ cd ../../
────────────────────────────────────────────────────────────────
rick@dell:~/a$ tree
.
└── b
    ├── c
    │   └── grub
    ├── c2
    │   └── grub
    └── grub

3 directories, 3 files

We created 4 directories on one line by using && to join multiple lines together. Then 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 target / destination to our current directory (c) with ..
  • In the second copy command we set the directory to the parent directory (b) with ...
  • In the third copy command we set the directory to the sibling directory (c2) with ../c2

Next we changed directory to our grand-parent directory (a) using cd ../../.

Finally we use tree to show all the directories and files under directory a.

Share:
237,438

Related videos on Youtube

Avani badheka
Author by

Avani badheka

Updated on September 18, 2022

Comments

  • Avani badheka
    Avani badheka over 1 year

    How to copy a file in current directory? e.g. I have a file at /abc/xyz and I am in the current directory mno -- I want to copy a file from /abc/xyz/file.txt into mno.

    cp command:

    cp /cp_file_path /Destination_path 
    

    But what about destination path as my current directory?

  • cat
    cat over 7 years
    maybe that's a code '.' eh?
  • cat
    cat over 7 years
    Haha :) But I meant, it looks weird and tiny to just have the '.' -- it should be in backticks
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 7 years
    @cat I'm on my computer now and threw in the back ticks, plus parent directory, sibling directory and grandparent directory examples in addition to current directory example. Sorry the original answer was posted a few minutes after user asked as there were no other answers. I was using my phone which is AskUbuntu-challenged :(
  • cat
    cat over 7 years
    Well, now this answer deserves to be at the top :)