Linux Tutorial 05


cp – Copy Files And Directories


  • Copy zikzak_file_01 to zikzak_file_02.
  • If zikzak_file_02 exists, it is overwritten with the contents of zikzak_file_01.
  • If zikzak_file_02 does not exist, it is created.
$ ls -l
total 0
-rw-r----- 1 zikzak u41687 0 Aug  2 17:05 zikzak_file_01

$ cp zikzak_file_01 zikzak_file_02

$ ls -l
total 0
-rw-r----- 1 zikzak u41687 0 Aug  2 17:05 zikzak_file_01
-rw-r----- 1 zikzak u41687 0 Aug  2 17:07 zikzak_file_02


Same as above, except that if zikzak_file_02 exists, the user is prompted before it is overwritten.

$ cp -i zikzak_file_02 zikzak_file_03

$ ls -l
total 0
-rw-r----- 1 zikzak u41687 0 Aug  2 17:05 zikzak_file_01
-rw-r----- 1 zikzak u41687 0 Aug  2 17:07 zikzak_file_02
-rw-r----- 1 zikzak u41687 0 Aug  2 17:08 zikzak_file_03

$ cp -i zikzak_file_02 zikzak_file_03
cp: overwrite `zikzak_file_03'? yes

$ ls -l
total 0
-rw-r----- 1 zikzak u41687 0 Aug  2 17:05 zikzak_file_01
-rw-r----- 1 zikzak u41687 0 Aug  2 17:07 zikzak_file_02
-rw-r----- 1 zikzak u41687 0 Aug  2 17:08 zikzak_file_03

Copy zikzak_file_01 and zikzak_file_02 into directory archive. archive must already exist.

$ mkdir archive

$ cp zikzak_file_01 archive

$ ls -l archive
total 0
-rw-r----- 1 zikzak u41687 0 Aug  2 17:10 zikzak_file_01

$ cp zikzak_file_01 zikzak_file_02 archive

$ ls -l
total 4
drwxr-x--- 2 zikzak u41687 4096 Aug  2 17:10 archive
-rw-r----- 1 zikzak u41687    0 Aug  2 17:05 zikzak_file_01
-rw-r----- 1 zikzak u41687    7 Aug  2 17:09 zikzak_file_02
-rw-r----- 1 zikzak u41687    7 Aug  2 17:09 zikzak_file_03

$ ls -l archive
total 0
-rw-r----- 1 zikzak u41687 0 Aug  2 17:10 zikzak_file_01
-rw-r----- 1 zikzak u41687 7 Aug  2 17:10 zikzak_file_02


Using a wildcard, all the files in archive are copied into archive02. archive02 must already exist.

$ mkdir archive02

$ ls -l
total 8
drwxr-x--- 2 zikzak u41687 4096 Aug  2 17:10 archive
drwxr-x--- 2 zikzak u41687 4096 Aug  2 17:11 archive02
$ cp archive/* archive02

$ ls -l archive
total 0
-rw-r----- 1 zikzak u41687 0 Aug  2 17:10 zikzak_file_01
-rw-r----- 1 zikzak u41687 7 Aug  2 17:10 zikzak_file_02

$ ls -l archive02
total 0
-rw-r----- 1 zikzak u41687 0 Aug  2 17:11 zikzak_file_01
-rw-r----- 1 zikzak u41687 7 Aug  2 17:11 zikzak_file_02

  • Copy the contents of directory archive to directory archive03.
  • If directory archive03 does not exist, it is created and, after the copy, will contain the same contents as directory archive .
  • If directory archive03 does exist, then directory archive (and its contents) will be copied into archive03 .
$ cp -r archive archive03

$ ls -l archive03
total 0
-rw-r----- 1 zikzak u41687 0 Aug  2 17:12 zikzak_file_01
-rw-r----- 1 zikzak u41687 7 Aug  2 17:12 zikzak_file_02


Copy only the files that dont exist in the destination directory

$ pwd
/home/zikzak/letsCodeIt
$ ls -l
total 8
drwxr-x--- 2 zikzak u41687 4096 Jul 18 20:03 myDestDir
drwxr-x--- 2 zikzak u41687 4096 Jul 18 20:02 mySourceDir

$ ls -ltr mySourceDir/
total 0
-rw-r----- 1 zikzak u41687 13 Jul 18 20:02 one.txt
-rw-r----- 1 zikzak u41687  9 Jul 18 20:02 two.txt
-rw-r----- 1 zikzak u41687 12 Jul 18 20:02 three.txt

$ ls -ltr myDestDir
total 0
-rw-r----- 1 zikzak u41687 13 Jul 18 20:03 one.txt
-rw-r----- 1 zikzak u41687  9 Jul 18 20:03 two.txt

$ cp -u mySourceDir/*.txt myDestDir/

$ ls -ltr myDestDir
total 0
-rw-r----- 1 zikzak u41687 13 Jul 18 20:03 one.txt
-rw-r----- 1 zikzak u41687  9 Jul 18 20:03 two.txt
-rw-r----- 1 zikzak u41687 12 Jul 18 20:16 three.txt

 


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s