179

I have read about copying files with terminal but these examples will help me a lot. So here is what I want to do:

Examples:

  1. I have a file in /home/levan/kdenlive untitelds.mpg and I want to copy this file to /media/sda3/SkyDrive and do not want to delete any thing in SkyDrive directory.

  2. I have a file in /media/sda3/SkyDrive untitelds.mpg and I want to copy this file to /home/levan/kdenlive and do not want to delete any thing in kdenlive directory

  3. I want to copy a folder from home directory to sda3 and do not want to delete any thing on sda3 directory and opposite

  4. I want to cut a folder/file and copy to other place without deleting files in that directory I cut it into.

Braiam
  • 66,947
  • 30
  • 177
  • 264
Levan
  • 10,690
  • 22
  • 67
  • 93

6 Answers6

230

1) By using -i for interactive you will be asked if you would like to replace the file:

cp -i /home/levan/kdenlive/untitelds.mpg /media/sda3/SkyDrive/

or you can use -b to create a backup of your file:

cp -b /home/levan/kdenlive/untitelds.mpg /media/sda3/SkyDrive



2) Same as the above:

cp (-i or -b) /media/sda3/SkyDrive/untitelds.mpg /home/levan/kdenlive



3) Use -R for recursive and -i for interactive:

cp -Ri ~/MyFolder /sda3/



4) This last one can be done via the mv command, move is like cutting:

mv -i ~/MyFile ~/OtherFolder/MyFile

if you want to move a directory, use:

mv -Ri ~/MyDirectory ~/OtherDirectory/
Honey
  • 111
  • 5
Evandro Silva
  • 9,472
  • 6
  • 35
  • 44
  • Neat syntax highlighting! Not sure how you triggered that. – Knowledge Cube Oct 03 '12 at 16:05
  • @WarriorIng64 you can use four spaces identation for code blocks, or you can surround small pieces of code with `s. – Evandro Silva Oct 03 '12 at 16:11
  • @EvandroSilva I know. I was referring to the fact that some of your code is shown in blue. – Knowledge Cube Oct 03 '12 at 16:12
  • @WarriorIng64 I've noticed that too but I really have no clue of how does that work... :) – Evandro Silva Oct 03 '12 at 16:13
  • 4
    @WarriorIng64 That is triggered by the `bash` tag in the question. highlighting will be enabled by default by the system. If you answer a question with `bash` tag, the hightlighting will follow bash style and so on. – Anwar Oct 03 '12 at 16:19
  • 2
    @Anwar I was just partway through making a [Meta post](http://meta.askubuntu.com/q/4180/18612) on this when I saw your comment. Of course, I credited you in the answer. :) – Knowledge Cube Oct 03 '12 at 16:24
  • I noticed that some of the directory paths in your answer (and others' too) have a trailing `/` at the end, while others don't. Does it make any difference? –  Jun 28 '18 at 20:27
  • @Ploni Actually, yes. See [this answer](https://unix.stackexchange.com/a/50533/24442) for more info – Evandro Silva Jun 29 '18 at 15:20
16

When ~/Dropbox/RECENT/ is your current directory:

cp input.txt SORT/

And I want to copy input.txt with another name in my current directory.

Again with ~/Dropbox/RECENT/ as current directory:

cp  input.txt newname.txt

Existing filenames can be auto-completed using TAB.

Long version of the same copy command (when you are not in ~/Dropbox/RECENT/):

cp /home/$USER/Dropbox/RECENT/input.txt /home/$USER/Dropbox/RECENT/SORT/

I put a / behind every directory. If SORT does NOT exist a cp will also create a file named SORT making you think something went wrong. Adding the / will have cp error out and not copy the file.

muru
  • 193,181
  • 53
  • 473
  • 722
Rinzwind
  • 293,910
  • 41
  • 570
  • 710
4

Use the cp command.

Copying a file something.txt to file folder: use cp something.txt folder/

Copying a file something.txt to the current directory as something2.txt: use cp something.txt something2.txt

ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:53 Folder1
-rw-rw-r-- 1 ubuntu ubuntu   14 Mar 12 21:52 something.txt
ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l Folder1/
total 4
-rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:53 something.txt
ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:54 folder
-rw-rw-r-- 1 ubuntu ubuntu   14 Mar 12 21:52 something.txt
ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l folder/
total 0
ubuntu@ubuntu-T100TA:~/TestFolder$ cp something.txt folder/
ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l folder/
total 4
-rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:55 something.txt
ubuntu@ubuntu-T100TA:~/TestFolder$ cp something.txt something2.txt 
ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l
total 12
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:55 folder
-rw-rw-r-- 1 ubuntu ubuntu   14 Mar 12 21:55 something2.txt
-rw-rw-r-- 1 ubuntu ubuntu   14 Mar 12 21:52 something.txt
The Holy See
  • 444
  • 3
  • 7
  • 16
4

you will better add the option -a to cp command to preserve file-datetime, file-stats, etc.:

cp -a input.txt ./SORT
cp -a input.txt newname.txt
cmks
  • 1,874
  • 1
  • 11
  • 17
1
rsync -aAXhv /home/levan/kdenlive/untitelds.mpg  /media/sda3/SkyDrive/

This will copy the untitelds.mpg file in the SkyDrive directory without deleting anything

Sparhawk
  • 6,872
  • 7
  • 48
  • 78
BillV
  • 51
  • 5
0

use cp command. Type man cp from the terminal for more info.

Vishnu N K
  • 595
  • 5
  • 22