国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

    Linux cp命令中拷貝所有的寫法

    時(shí)間: 志藝942 分享

      cp指令用于復(fù)制文件或目錄,如同時(shí)指定兩個(gè)以上的文件或目錄,且最后的目的地是一個(gè)已經(jīng)存在的目錄,則它會(huì)把前面指定的所有文件或目錄復(fù)制到此目錄中。接下來是小編為大家收集的Linux cp命令中拷貝所有的寫法,歡迎大家閱讀:

      Linux cp命令中拷貝所有的寫法

      一、預(yù)備

      cp就是拷貝,最簡(jiǎn)單的使用方式就是:

    1
    cp oldfile newfile

      但這樣只能拷貝文件,不能拷貝目錄,所以通常用:

    1
    cp -r old/ new/

      那就會(huì)把old目錄整個(gè)拷貝到new目錄下。注意,不是把old目錄里面的文件拷貝到new目錄,而是把old直接拷貝到new下面,結(jié)果是:

    1
    2
    3
    [root@dc5 test]# ll new/
    total 4
    drwxr-xr-x 2 root root 4096 Dec 15 11:55 old

      那如果要保持源文件的所有權(quán)限,可以這樣:

    1
    cp -rp old/ new/

      -p參數(shù),可以保持權(quán)限、宿主、時(shí)間棧,還可能包括link等;還有更簡(jiǎn)單的,就是用:

    1
    cp -a old/new/

      -a參數(shù),就等于-dpR。

      二、問題1

      好,我們來看看這次的問題。環(huán)境是:

      ◎兩個(gè)目錄:old、new,其中old里面有個(gè)三個(gè)內(nèi)容:test1文件、test2目錄,還有就是.test3,這是一個(gè)隱含文件。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    [root@dc5 test]# ll -laR
    .:
    total 20
    drwxr-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 new
    drwxr-xr-x 3 root root 4096 Dec 15 12:14 old
     
    ./new:
    total 8
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
    drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..
     
    ./old:
    total 12
    drwxr-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 test1
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2
     
    ./old/test2:
    total 8
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
    drwxr-xr-x 3 root root 4096 Dec 15 12:14 ..

      ◎操作一:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@dc5 test]# cp -a old/* new/
    [root@dc5 test]# ll -laR new/
    new/:
    total 12
    drwxr-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 test1
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2
     
    new/test2:
    total 8
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
    drwxr-xr-x 3 root root 4096 Dec 15 12:15 ..

      問題出來了:隱含的.test3文件沒有一齊拷貝到new目錄下。

      原因是:參數(shù)使用不正確。這樣的寫法,通常都是因?yàn)槭煜ち诉^去Dos的格式(包括我自己),而實(shí)際在bash環(huán)境下,cp使用是不能匹配類似.開頭的隱含文件的。

      ◎操作二

      正確的寫法應(yīng)該這樣:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [root@dc5 test]# cp -a old/. new/
    [root@dc5 test]# ll -laR new/
    new/:
    total 12
    drwxr-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 test1
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2
     
    new/test2:
    total 8
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
    drwxr-xr-x 3 root root 4096 Dec 15 12:14 ..

      不用*號(hào),而用.號(hào)代替。

      還有一種比較復(fù)雜一些的寫法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [root@dc5 test]# cp -a old/* old/.[^.]* new/
    [root@dc5 test]# ll -laR new/
    new/:
    total 12
    drwxr-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 test1
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2
     
    new/test2:
    total 8
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
    drwxr-xr-x 3 root root 4096 Dec 15 12:25 ..

      請(qǐng)注意寫法,不要寫成.*了。(原因請(qǐng)看下面)

      三、問題2

      上面提到不要寫成.,那.代表什么?

    1
    2
    [root@dc5 test]# echo .*
    . ..

      .*代表的是當(dāng)前目錄,以及上一層目錄。

      所以,使用.*會(huì)導(dǎo)致更大的問題:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    [root@dc5 test]# cp -a 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 16
    drwxr-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 .test3
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 new
    -rw-r--r-- 1 root root 0 Dec 15 12:05 test1
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2
     
    new/new:
    total 8
    drwxr-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 test1
     
    new/test2:
    total 8
    drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
    drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..

      也就是說,使用.*就等于這樣了:

    1
    2
    3
    [root@dc5 test]# cp -a old/. old/.. old/.test3 new/
    [root@dc5 test]# echo old/.*
    old/. old/.. old/.test3

      
    看了“Linux cp命令中拷貝所有的寫法”還想看:

    1.linux cp命令使用介紹

    2.Linux下如何使用cp命令

    3.Linux中cp和scp命令怎么使用

    4.Linux使用cp命令進(jìn)行強(qiáng)制覆蓋的方法

    2919066