Wednesday, April 11, 2012

cp - 10 different copy command examples



  cp command is used to copy files in Unix . There are some options in cp which add lot of value to this command. In this article, we will see about some of the rarely used options of the cp command which makes cp really powerful.

1. Copy a file
cp command has the arguments source file and destination file. Here, we are copying file1 to file2.
$ cp file1 file2
$ ls -l file*
-rw-r--r-- 1 guru users 163 Apr 11 15:36 file1
-rw-r--r-- 1 guru users 163 Apr 11 15:40 file2
   The same thing can also be achieved using the below command. It might look tricky if you are seeing this for the first time.
$ cp file{1,2}
      When this above command runs, shell expands "file{1,2}" to "file1 file2". And hence it becomes in the right format as needed by the cp command. To understand it better, simply put an "echo " ahead of the cp command:
$ echo cp file{1,2}
cp file1 file2
2.  Copy a file named "file" to "file1":
Copying file1 to file2 is fine. Using the same shortcut as above, how to copy "file" to "file1"?
$ cp file{,1}
$ ls -l file*
-rw-rw-r-- 1 guru users 163 Apr 10 13:50 file
-rw-r--r-- 1 guru users 163 Apr 11 15:36 file1
      If this command is not clear, as did earlier, put an echo before cp and check it.
     On carefully noticing the above output file listing, 2 things will be clear:
  • cp command creates the new file with the current timestamp.
  • The file permission is the default file permissions, not the file permissions of the original file.
3.   How to copy a file by preserving the timestamp of the original file? In other words, how to copy a file with the same timestamp as that of the original file?
$ cp --preserve=timestamp file file1
$ ls -l file*
-rw-rw-r-- 1 guru users 163 Apr 10 13:50 file
-rw-r--r-- 1 guru users 163 Apr 10 13:50 file1
     GNU cp provides an option --preserve using which the timestamp of the original file can be retained. 3 properties of a file are of importance while copying a file:
  • Time stamp
  • File Modes
  • Ownership
4. Copy a file retaining the modes of the source file:
Like timestamp, cp can also retain the mode of the files and ownership as well. The same --preserve switch does it.
$ cp --preserve=mode file file1
$ ls -l file*
-rw-rw-r-- 1 guru users 163 Apr 10 13:50 file
-rw-rw-r-- 1 guru users 163 Apr 11 15:35 file1
     The mode of the "file" has been retained in "file1" as well.

5. Copy a file preserving timestamp, mode and ownership:
To copy a file with all the 3 properties of the original file, use the "-p" option. "-p" is a refined form of "--preserve" wherein all the 3 properties(timestamp, mode and ownership) are copied as is from the original file.
$ cp -p file file1
$ ls -l file*
-rw-rw-r-- 1 guru users 163 Apr 10 13:50 file
-rw-rw-r-- 1 guru users 163 Apr 10 13:50 file1
6.  Create a link using the cp command:
 ln command is the one which is used in creating soft links and hard links. cp command can also create soft links and hard links.
$ cp -l file file1
$ ls -li file*
9962525 -rw-rw-r-- 2 guru users 163 Apr 10 13:50 file
9962525 -rw-rw-r-- 2 guru users 163 Apr 10 13:50 file1
     The "-l" option of the cp command does not copy files, instead creates a hard link of the source file. The same inode numbers of file and file1 proves it.

7.  Create a soft link using the cp command:
   cp can  create soft links using the "-s" option.
$ cp -s file file1
$ ls -l file*
-rw-rw-r-- 1 guru users 163 Apr 10 13:50 file
lrwxrwxrwx 1 guru users   4 Apr 11 14:59 file1 -> file
8.  Copy only if the source file is updated:
Sometimes, we copy a file the need for which could be to copy only if the source file is updated(time stamp). Keep in mind, cp is an external command. Some extra code need to be written to check if the source file is updated of late. cp provides an option "-u" for this purpose. With -u, the file will be copied only if the source file is newer than the destination file.
$ date
Wed Apr 11 16:33:42 IST 2012
$ cp -u file1 file2
$ ls -l file*
-rw-r--r-- 1 guru users  163 Apr 11 15:36 file1
-rw-r--r-- 1 guru users  163 Apr 11 15:40 file2
    As shown above, cp did not copy the file since file2 is newer than file1.

9. Let us just touch the file file1 and try the cp.
$ touch file1
$ cp -u file1 file2
$ ls -l file*
-rw-r--r-- 1 guru users  163 Apr 11 16:31 file1
-rw-r--r-- 1 guru users  163 Apr 11 16:31 file2
      As seen above, file2 is copied since the file1 has a timestamp later than file2.

10. Copy a file with a pre-defined time stamp:
    Say, we want to have the copied file with a specific time stamp of the user choice. cp does not provide an option for this. Instead, we can copy the file normally, and use the touch command to change the timestamp of our choice.
$ cp file1 file2
$ touch -t 1204091524 file2
$ ls -l file2
-rw-r--r-- 1 guru users 163 Apr  9 15:24 file2
    As above, the touch command can be used to change the timestamp of the file to any date using the "-t" option. The time stamp of the file file2 has now been changed to "1204091524" which stands for 2012(12), April(04), 9th(08), 15 hours(15) and 24 minutes(24).

Happy Copying!!!

1 comment: