Virgins post, questions about cp commands updated

Source: Internet
Author: User
For more information about cp commands-general Linux technology-Linux technology and applications, see the following. [I = s] This post was last edited by yanyongkg

Read the masterpiece of laruence again, and have some questions about the cp command.
1) cp-s in the book: link files, same as ln usage
Here is my experiment (centos5.4 'uname-a' = 2.6.18-164. el5 ):
[Yan @ localhost tmp] $ pwd
/Home/yan/tmp
[Yan @ localhost tmp] $ ll
-Rw-r -- 1 yan 21 01-17 18:39 aaa # aaa is a text file
Drwxrwxr-x 2 yan 4096 01-17 test # A directory

Current Directory connection Creation
[Yan @ localhost tmp] $ cp-s aaa bbb
[Yan @ localhost tmp] $ ll
Lrwxrwxrwx 1 yan 3 01-17 :02 bbb-> aaa # No problem, OK

Non-current directory connection Creation
[Yan @ localhost tmp] $ cp-s aaa test/bbb2
Cp: "test/bbb2": You can only create a relative symbolic link in the current directory # Only the current directory can be created
[Yan @ localhost tmp] $ cp-s ~ /Tmp/aaa test/bbb2
[Yan @ localhost tmp] $ ll test
Lrwxrwxrwx 1 yan 17 01-17 19:08 bbb2->/home/yan/tmp/aaa
Here we can see that cp-s is used as the link file, and the source file must use an absolute path? It is not prompted that a symbolic link can be created only in the current directory.
/Home/yan/tmp/aaa, called absolute path
~ /Tmp/aaa. Is this an absolute path? Maybe full path?

Try ln-s and create a link in the current directory:
Create a link to the current directory
Ln-s aaa bbb is the same as cp-s aaa bbb.

Create a link from a non-current directory
First look at the relative path
[Yan @ localhost tmp] $ ln-s aaa test/bbb3 # No problem. It seems okay, just say it looks like it, and then you will understand it later.
Use the absolute path.
[Yan @ localhost tmp] $ ln-s ~ /Tmp/aaa test/bbb4
[Yan @ localhost tmp] $ ll test
Lrwxrwxrwx 1 yan 3 01-17 19:13 bbb3-> aaa
Lrwxrwxrwx 1 yan 17 01-17 :40 bbb4->/home/yan/tmp/aaa
We can see that the linked file bbb3 points to the aaa file in the current directory, and the current directory is the directory where bbb3 is located. This is definitely incorrect. The aaa file is not in this directory. Do not believe it. cat bbb3.
[Yan @ localhost test] $ cat bbb3
Cat: bbb3: No file or directory
[Yan @ localhost test] $ cat bbb4
Hello, Linux
Justaaaa

Conclusion: when ln-s or cp-s links to a non-current directory, the source file must use an absolute path. Otherwise, even if the link file is used, the path to the link file is incorrect.
The above is not clear, so it is easier to understand the following statement.
When creating a link symbol file, when the source file and the link file are not in the same directory, the source file path must use an absolute path, and the absolute or relative path of The Link file can both be used.
When the source file and the linked file are in the same directory, it is no longer called. Both the source file and the linked file can use relative paths or absolute paths.
Ps: Here is the soft link, there is also a hard link, ln-d, my centos environment directly ln default-d parameter is there, at first I ln aaa bbb, I thought I copied an aaa as bbb, but I found it online. If you are interested, please check it out.
Http://hi.baidu.com/zhongbin104/... 49c75c564e00a8.html

Not finished yet, there are still 2) Don't write, this question is relatively big, want to eat dinner, huh, huh

Continued 2) cp-u
If the source file of cp-u is relatively new or there is no target file, the copy operation will be executed, which can be used in backup operations.
Laruence gave an example and added a note:
Cp-u/home /. bashrc/root /. bashrc # Check/home/first /. whether bashrc matches/root /. bashrc is different. If it is different, copy one copy. If it is the same, no action is performed.

If you look at it carefully, you will surely think of this problem. Is the source file different from the target file, or is the document modification time different?
So I did the following tests:
[Yan @ localhost tmp] $ ll
Total 8
-Rw-r -- 1 yan 12 01-17 aaa # Last modified
-Rw-r -- 1 yan 24 01-17 bb # Last modified
[Yan @ localhost tmp] $ cat aaa
Hello, Linux
[Yan @ localhost tmp] $ cat bb
Hello, Linux
Just a test # the content of the aaa and bb files is different.
[Yan @ localhost tmp] $ cp-u aaa bb
[Yan @ localhost tmp] $ cat bb
Hello, Linux
Just a test # Although the content of the source document is different from that of the target file, the copy action is not performed, indicating that they are not compared with the file content.
# The source file aaa has no target file bb. The last modification time is new, so no copy action is performed. Is this the reason? I will verify it later.
After a while, modify the aaa file.
[Yan @ localhost tmp] $ ll
-Rw-r -- 1 yan 16 01-17 23:18 aaa
-Rw-r -- 1 yan 24 01-17 bb # Here the time is still the original
[Yan @ localhost tmp] $ cat aaa
Hello, Linux
Hey
[Yan @ localhost tmp] $ cat bb
Hello, Linux
Just a test
[Yan @ localhost tmp] $ cp-u aaa bb
[Yan @ localhost tmp] $ ll
Total 8
-Rw-r -- 1 yan 16 01-17 23:18 aaa
-Rw-r -- 1 yan 16 01-17 bb # The last modification time has changed to. It seems that the copy operation has been performed. Let's take a look at the cat below.
[Yan @ localhost tmp] $ cat bb
Hello, Linux
Hey # Do you understand?

I think it is more extreme. The last modification time of the comparison is accurate to the minute. Is it accurate to the second?
[Yan @ localhost tmp] $ ll
Total 8
-Rw-r -- 1 yan 6 01-17 aaa # aaa modified first
-Rw-r -- 1 yan 12 01-17 bb # The last modification time of the modified bb is newer than aaa
[Yan @ localhost tmp] $ cat aaa
Hello
[Yan @ localhost tmp] $ cat bb
Hello, Linux
[Yan @ localhost tmp] $ cp-u bb aaa
[Yan @ localhost tmp] $ ll
Total 8
-Rw-r -- 1 yan 12 01-17 23:40 aaa
-Rw-r -- 1 yan 12 01-17 23:36 bb
[Yan @ localhost tmp] $ cat aaa
Hello, Linux # Is really accurate!

Because XP has been replaced early, it is not easy to send, laruence did not understand cp-u, directly to the careless, although the Bird brother is more authoritative, I still want to challenge, don't beat me. I have no disrespect for laruence.
It's a long time for a virgin post. Thank you for reading it.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.