Linux file management cp

Keywords: Attribute

Copy copy a file or directory

Original meaning of order

  • copy

Command format

  • cp [options] [original file or directory] [destination directory]

Common parameters

  • -r copy directory

    -p copy with file attributes

    -d if the original file is a linked file, copy the link properties

    -a is equivalent to - pdr select all. The time of the copied file is the time of executing the copy command. To make the copied file completely consistent with the original file attribute, add - a

    -s create a link file (shortcut)

Common examples

Example 1

Copy entire directory

When the destination directory exists:

[root@localhost test]# cp -a test3 test5 
[root@localhost test]# ll
-rw-r--r-- 1 root root    0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]# cd test5/
[root@localhost test5]# ll
-rw-r--r-- 1 root root    0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root    0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root    0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root    0 10-28 14:48 log.log
drwxrwxrwx 2 root root 4096 10-28 14:47 test3

The destination directory does not exist is:

[root@localhost test]# cp -a test3 test4
[root@localhost test]# ll
-rw-r--r-- 1 root root    0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]#

The result is different whether the target directory exists or not. When the target directory exists, the entire source directory is copied to the target directory!!!

Example 2

Create a link file of the copied log.log

[root@localhost test]# cp -s log.log log_link.log
[root@localhost test]# ll
lrwxrwxrwx 1 root root    7 10-28 15:18 log_link.log -> log.log
-rw-r--r-- 1 root root    0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5

Posted by tecmeister on Sun, 03 May 2020 15:16:35 -0700