Provides various official and user-released code examples. For code reference, you are welcome to learn how to copy files and folders using cp.
Command Name:
cp(copy)
Function Description: copy one file to another, or copy several files to another directory.
Syntax format:
cp [options] source dest
cp [options] source... directory
Common parameters:
-A tries its best to copy the file status, permissions, and other information as they are.
-R if the source contains the directory name, the files under the directory are also copied to the destination in sequence.
-F if the destination already has an archive with the same file name, delete it before copying.
Command instance:
1. copy the file aaa (already exists) and name it bbb:
cp aaa bbb
2. Copy all C language programs to the Finished subdirectory:
cp *.c Finished
3. copy the file in the specified directory to the current directory and rename it.
[root@rehat root]# cp ~/.bashrc bashrc_bak
4. Force copy the file from the specified directory to the current directory, regardless of whether the current directory contains the file
[root@rehat root]# cp -f ~/.bashrc bashrc
5. Copy the specified directory to the current directory.
[root@rehat root]# cp -r /root/test .
[root@rehat root]# cp -r /root/test/ .
The two have the same effect. When copying a directory, all the last-level directories in the source path will be copied, including the directory itself.
6. copy the files in the specified directory to the specified directory.
[root@rehat root]# cp ~/.bashrc /bak/.bashrc
7. Copy all the attributes of the source file during replication. If no parameter is specified, the properties of the target file and the source file may be inconsistent.
[root@rehat root]# cp -a ~/.bashrc /bak/.bashrc
8. If the two folders need to be synchronized, one file is changed, and the other file is changed, but ensure that the files of the two files are the latest.
[root@rehat root]# cp -u /src/.bashrc /bak_src/bashrc
9. Copy all files in the specified directory to the current directory.
[root@rehat root]# cp -r /root/test/* .
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB