In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "copy all the usages in the cp command under CentOS". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "copy all the usages in the cp command under CentOS".
I. preparation
Cp is a copy, and the easiest way to use it is:
Cp oldfile newfile
But you can only copy files, not directories, so you usually use:
Cp-r old/ new/
Then the entire old directory will be copied to the new directory. Note that instead of copying the files in the old directory to the new directory, copy the old directly to the new. The result is:
Quote
[root@dc5 test] # ll new/total 4drwxr-xr-x 2 root root 4096 Dec 15 11:55 old
So if you want to maintain all permissions on the source file, you can do this:
Cp-rp old/ new/
The-p parameter, which can maintain permissions, hosts, time stacks, and possibly link, etc., and, more simply, use:
Cp-an old/new/
The-a parameter equals-dpR.
Second, question 1
OK, let's take a look at the problem this time. The environment is:
◎ two directories: old, new, in which old has three contents: test1 file, test2 directory, and .test3, which is an implicit file.
Quote
[root@dc5 test] # ll-laR.:total 20drwxr-xr-x 4 root root 4096 Dec 15 11:55. Drwxrwxrwt 7 root root 4096 Dec 15 11:59.. drwxr-xr-x 2 root root 4096 Dec 15 12:14 newdrwxr-xr-x 3 root root 4096 Dec 15 12:14 old./new:total 8drwxr-xr-x 2 root root 4096 Dec 15 12:14 .drwxr-xr-x 4 root root 4096 Dec 15 11:55. / old:total 12drwxr-xr-x 3 root root 4096 Dec 15 12:14 .drwxr-xr-x 4 root root 4096 Dec 15 11:55.-rw-r--r-- 1 root root 0 Dec 15 12:07. Test3-rw-r--r-- 1 root root 0 Dec 15 12:05 test1drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2./old/test2:total 8drwxr-xr-x 2 root root 4096 Dec 15 12:14. Drwxr-xr-x 3 root root 4096 Dec 15 12:14.
◎ operation 1:
Quote
[root@dc5 test] # cp-an old/* new/ [root@dc5 test] # ll-laR new/new/:total 12drwxr-xr-x 3 root root 4096 Dec 15 12:15 .drwxr-xr-x 4 root root 4096 Dec 15 11:55.-rw-r--r-- 1 root root 0 Dec 15 12:05 test1drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2new/test2:total 8drwxr-xr-x 2 root root 4096 Dec 15 12:14 .drwxr-xr-x 3 root root 4096 Dec 15 12:15.
Here's the problem: the implied .test3 files were not copied to the new directory.
The reason is: * the parameter is not used correctly. This way of writing is usually because I am familiar with the past Dos format (including myself), but in fact, in the bash environment, cp use * can not match similar. Of the implied file at the beginning.
◎ operation two
The correct way to write it should be like this:
Quote
[root@dc5 test] # cp-an old/. New/ [root@dc5 test] # ll-laR new/new/:total 12drwxr-xr-x 3 root root 4096 Dec 15 12:14. Drwxr-xr-x 4 root root 4096 Dec 15 11:55.-rw-r--r-- 1 root root 0 Dec 15 12:07. Test3-rw-r--r-- 1 root root 0 Dec 15 12:05 test1drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2new/test2:total 8drwxr-xr-x 2 root root 4096 Dec 15 12:14. Drwxr-xr-x 3 root root 4096 Dec 15 12:14..
Don't use the * sign, use it. Instead of the number.
There is also a more complicated way of writing:
Quote
[root@dc5 test] # cp-an old/* old/. [^.] * new/ [root@dc5 test] # ll-laR new/new/:total 12drwxr-xr-x 3 root root 4096 Dec 15 12:25. Drwxr-xr-x 4 root root 4096 Dec 15 11:55.-rw-r--r-- 1 root root 0 Dec 15 12:07. Test3-rw-r--r-- 1 root root 0 Dec 15 12:05 test1drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2new/test2:total 8drwxr-xr-x 2 root root 4096 Dec 15 12:14. Drwxr-xr-x 3 root root 4096 Dec 15 12:25..
Please pay attention to the writing, do not write as. *. (for reasons, please see below)
Question 2
It is mentioned above that it should not be written as. * what does it stand for?
Quote
[root@dc5 test] # echo. *. ..
. * represents the current directory, as well as the previous directory.
So, using. * can lead to bigger problems:
Quote
[root@dc5 test] # cp-an old/.* new/cp: cannot copy a directory, `old/..', into itself, `new/'cp: cannot copy a directory, `old/..', into itself, `new/'cp: will not create hard link `new/old' to directory `new/.'cp: overwrite `new/.test3'? Y [root@dc5 test] # ll-laR new/new/:total 16drwxr-xr-x 4 root root 4096 Dec 15 11:55. Drwxr-xr-x 4 root root 4096 Dec 15 11:55.-rw-r--r-- 1 root root 0 Dec 15 12:07. Test3drwxr-xr-x 2 root root 4096 Dec 15 12:14 new-rw-r--r-- 1 root root 0 Dec 15 12:05 test1drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2new/ New:total 8drwxr-xr-x 2 root root 4096 Dec 15 12:14. Drwxr-xr-x 4 root root 4096 Dec 15 11:55.-rw-r--r-- 1 root root 0 Dec 15 12:07. Test3-rw-r--r-- 1 root root 0 Dec 15 12:05 test1new/test2:total 8drwxr-xr-x 2 root root 4096 Dec 15 12:14. Drwxr-xr-x 4 root root 4096 Dec 15 11:55.
In other words, using. * is tantamount to this:
Quote
[root@dc5 test] # cp-an old/. Old/.. Old/.test3 new/ [root@dc5 test] # echo old/.*old/. Old/.. Old/.test3
IV. Expansion
In fact, not only the cp command has such a problem, but all commands involving files with special characters need to be considered, such as rm:
Quote
[root@dc5 new] # ll-atotal 12drwxr-xr-x 3 root root 4096 Dec 15 12:14. Drwxr-xr-x 4 root root 4096 Dec 15 11:55.-rw-r--r-- 1 root root 0 Dec 15 12:07. Test3-rw-r--r-- 1 root root 0 Dec 15 12:05 test1drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2 [root@dc5 new] # rm-rf * [root@dc5 new] # ll-atotal 8drwxr -xr-x 2 root root 4096 Dec 15 12:40. Drwxr-xr-x 4 root root 4096 Dec 15 11:55.-rw-r--r-- 1 root root 0 Dec 15 12:07 .test3
The correct way to write it should be:
Quote
[root@dc5 new] # rm-rf. * * rm: cannot remove `. Or `..'rm: cannot remove'. Or `.'[root@dc5 new] # ll-atotal 8drwxr-xr-x 2 root root 4096 Dec 15 12:42. Drwxr-xr-x 4 root root 4096 Dec 15 11:55.
Of course, it's the same:
Quote
[root@dc5 new] # rm-rf *. [^.] * [root@dc5 new] # ll-atotal 8drwxr-xr-x 2 root root 4096 Dec 15 12:44. Drwxr-xr-x 4 root root 4096 Dec 15 11:55.
In many cases, the expected and actual results are completely different. Bash scripting requires particular attention.
At this point, I believe you have a deeper understanding of "copy all the usage of the cp command under CentOS". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.