linux copy file command

Definition: cp is short for copy, which means to copy files. Used to copy files or directories from one location to another.

Parameter list preview:

img

img

Usage 1:

$ cp sourceFile destinationFile

When the sourceFile and destinationFile parameters are both file names, the cp command copies the contents of the source file to a new file, which is named after the destinationFile. The content of the source file remains unchanged. New files have brand new starting time and modification time.

Command Line:

img

result:

imgimg

Note: When the destinationFile already exists, the general Linux system will remind the user whether to overwrite the original file.

Usage 2: Copy the file with the same name

$ cp /home/sheepcore/Documents/newfile /home/sheepcore/Downloads (the absolute file path must be given)

Command Line:

img

If you only need to copy to the current directory, you can simplify it as follows:

$ cp /home/sheepcore/Documents/newfile .

('.' indicates the current file directory, which is /home/sheepcore/Documents)

img

Usage 3:

$ cp -p destinationFile newDestnFile

Use the -p (preserve mode) parameter to preserve the access time and modification time of the source file for the target file!

Command Line:

img

Usage 4:

$ cp -R sourceDir destinationDir

Use the -R (recursive) parameter to recursively copy the entire source directory to the target directory. Its function is extremely powerful!

Command Line:

img

Usage 5: Use the cp command to create hard and soft links for files

$ cp -l srcFile linkedFile (hard link -l means link)

$ cp -s srcFile linkedFile (soft link -s means soft)

Command Line:

img

A hard link will create a separate file that includes the information and location of the source file. Therefore, referencing a hard-linked file is equivalent to referencing a source file.

As shown in the above command line, create the hard link file fileLink of the file file. From the file list, you can see that the index node numbers of file and fileLink are exactly the same, which shows that they are actually the same file. Pay careful attention, the number 2 after -rw-r--r-- indicates the link count of the file, indicating that there are currently two files with two links.

Of course, it is not allowed to create hard links between files on different storage media. At this time, you can use the following method to create a soft link through -s.

The following is the result display:

img

Notice the difference in the points I marked in the figure:

\1. The soft link file has a new index node number;

\2. The file size of the soft link file is significantly smaller than the source file and the hard link file;

\3. The link number of the soft link file is displayed as 1; while the source file and hard link file are displayed as 2?

Then, let me briefly explain the differences. When creating a soft link file, the Linux system treats it as a separate file, so it has a unique index number. The reason why the file becomes smaller is that the link file only needs to store the information of the source file, such as access time, file size, address, etc., but not the content of the source file, that is, the data inside. Because the soft link essentially creates a new file, the link count is 1.

supplement:

You can use the wildcard character * to batch copy files with the same name prefix to a directory!

$ cp -f file* destinationDir

-f (force) Forcibly overwrite the existing target file without prompting!

Command line example:

img

Tips: You can use relative path. (current directory) or .. (parent directory of current directory)

$ cp -v ./file* ../destinationDir

Command line example:

img

The above is the Linux-related knowledge shared by Liangxu Tutorial Network for all friends.

Guess you like

Origin blog.csdn.net/manongxianfeng/article/details/113054374