Linux cp command - Copy files and directories

Linux cp command: Copy files and directories.

Linux cp command Function Description

Use the cp command to copy files and directories to other directories. If more than two files or directories are specified at the same time and the final destination is a directory that already exists, it copies all the previously specified files or directories to that directory. If more than one file or directory is specified at the same time and the final destination is not an already existing directory, an error message appears.

The cp command is used to copy one or more source files or directories to the specified destination file or directory. It can copy a single source file to a specific file with the specified filename or to an already existing directory. cp command also supports copying multiple files at the same time, and when copying multiple files at once, the destination file parameter must be an already existing directory, otherwise an error will occur.

Linux cp command Syntax

cp [option] [source file|directory] [target file|directory]

The meaning of each option in the command is shown in the table.

Option Description
-a Keep the link, file properties when copying the directory, and copy the directory recursively, equivalent to the -dPR option
-d Keep links when copying
-f Does not prompt the user for confirmation before overwriting the target file
-i In contrast to the -f option, the user is prompted for confirmation before overwriting the target file
-p In addition to copying the contents of the source file, the modification time and access rights are also copied to the new file
-l Do not copy, just link files
-r If the source file given is a directory file, all subdirectories and files in that directory are recursively copied. The target must be a directory name

Linux cp command Examples

Copy the file /etc/host.conf to the /root directory and rename it to apidemos.conf

cp /etc/host.conf /root/apidemos.conf

Output:

Linux cp command

Copy the file /etc/grub.conf to the /root directory

[root@rhel ~]# cp /etc/grub.conf /root

Copy the /boot directory, including all the files and subdirectories in that directory, to the /root directory

[root@rhel ~]# cp -r /boot /root

More Linux cp command examples

The first line below shows the cp command and the specific arguments (-r for "recursive", -u for "update", and -v for "detail"). The next three lines show information about the file being copied, and the last line shows the command line prompt. Thus, to copy only the new files to my storage device, I use the "update" and "detail" options of cp.

Normally, the argument -r is also available in the more detailed style --recursive. But in a short way, you can also use -ruv in conjunction with this.

cp -r -u -v /usr/men/tmp ~/men/tmp

Version Backup --backup=numbered parameter means "I want to make a backup, and a sequential backup with a number". So one backup is number 1, the second one is number 2, etc.

$ cp --force --backup=numbered test1.py test1.py
$ ls
test1.py test1.py.~1~ test1.py.~2~

If you copy a file to a target file, which already exists, the contents of that target file will be corrupted. All parameters in this command can be either absolute or relative pathnames. It is common to use dot . or dot . form. For example, the following command copies the specified file to the current directory.

cp ../mary/homework/assign .

All the directories specified by the target file must already exist. The cp command cannot create directories. If you do not have the file copy permission, the system will display an error message.

Copy the file file to the directory /usr/men/tmp and rename it to file1

cp file /usr/men/tmp/file1

Copy all files and their subdirectories under the directory /usr/men to the directory /usr/zh

cp -r /usr/men /usr/zh

Interactively copy all .c files starting with m from the directory /usr/men to the directory /usr/zh

cp -i /usr/men m*.c /usr/zh

When we use the cp command to copy files under Linux, sometimes we need to overwrite some files with the same name, and when overwriting files, there will be a prompt: you need to keep pressing Y to determine the execution of the overwrite. The number of files is not too many, but if hundreds of estimates by Y will be vomiting blood, so tossed half a day to sum up a method.

cp aaa/* /bbb
# If there are files with the same name as aaa in the /bbb directory, press Y to confirm and skip the subdirectories in the aaa directory.

cp -r aaa/* /bbb
# This time, you still need to press Y to confirm the operation, but there is no subdirectory ignored.

cp -r -a aaa/* /bbb
# You still need to press Y to confirm the operation and pass the aaa directory and the subdirectory and file attributes to /bbb as well.

cp -r -a aaa/* /bbb
# Success, no prompt to press Y, passed directory attribute, no skipped directory.

Recursively force copy a directory to the specified directory to overwrite existing files

cp -rfb ./* ../backup
# Copy all files in the current directory to the backup folder of the current directory's sibling directory

Copy the hidden files in the directory like .babelrc

cp -r aaa/.* ./bbb
# Copy all '. 'files from aaa directory to bbb directory.

cp -a aaa ./bbb/ 
# Remember that the '/' is best followed by the `-a` argument

Copy to current directory

cp aaa.conf ./
# Copy aaa.conf to the current directory
Like(1)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?