zoukankan      html  css  js  c++  java
  • Linux常用基本命令[cp]

    cp:复制文件或者目录

    用法格式:

    cp [option] [source] [dest]

    cp [选项] [源文件] [目标文件]

    >用root账户,创建文件,复制文件

    root@dev:/home/ghostwu/linux/cp# vim 1.txt 
    root@dev:/home/ghostwu/linux/cp# ls -l
    total 4
    -rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
    root@dev:/home/ghostwu/linux/cp# cp 1.txt 2.txt
    root@dev:/home/ghostwu/linux/cp# ls -l
    total 8
    -rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
    -rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
    root@dev:/home/ghostwu/linux/cp# su - ghostwu
    ghostwu@dev:~$ cd -
    -su: cd: OLDPWD not set
    ghostwu@dev:~$ cd linux/cp
    ghostwu@dev:~/linux/cp$ ls -l
    total 8
    -rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
    -rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
    ghostwu@dev:~/linux/cp$ cp 2.txt 3.txt
    cp: cannot create regular file '3.txt': Permission denied

    上面,当我切换到ghostwu这个账户去复制的时候,权限不允许,因为2.txt 这个文件 的其他组只有 只读 权限, 而cp需要写权限,所以就报了一个无权限创建复制的文件。

    方法一,用sudo提权

    ghostwu@dev:~/linux/cp$ ls -l
    total 8
    -rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
    -rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
    ghostwu@dev:~/linux/cp$ sudo cp 2.txt 3.txt
    [sudo] password for ghostwu: 
    ghostwu@dev:~/linux/cp$ ls -l
    total 12
    -rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
    -rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
    -rw-r--r-- 1 root root 19 5月   6 17:52 3.txt

    方法二,用root用户给文件的其他组用户 可写权限,同时普通用户要对文件所属的目录拥有写权限, 也就是要对 "cp" 这个目录拥有写权限

    ghostwu@dev:~/linux$ ls -l
    total 4
    drwxr-xr-x 2 root root 4096 5月   6 17:52 cp
    ghostwu@dev:~/linux$ sudo chmod o+w cp
    ghostwu@dev:~/linux$ ls -l
    total 4
    drwxr-xrwx 2 root root 4096 5月   6 17:52 cp
    ghostwu@dev:~/linux$ cd cp
    ghostwu@dev:~/linux/cp$ ls -l
    total 12
    -rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
    -rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
    -rw-r--rw- 1 root root 19 5月   6 17:52 3.txt
    ghostwu@dev:~/linux/cp$ sudo chmod o+w 2.txt 
    ghostwu@dev:~/linux/cp$ ls -l
    total 12
    -rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
    -rw-r--rw- 1 root root 19 5月   6 17:48 2.txt
    -rw-r--rw- 1 root root 19 5月   6 17:52 3.txt
    ghostwu@dev:~/linux/cp$ cp 2.txt 4.txt
    ghostwu@dev:~/linux/cp$ ls -l
    total 16
    -rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
    -rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
    -rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
    -rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt

    用普通用户去复制root账户创建的2.txt文件,起一个新名字4.txt,默认情况下cp 改变了文件的权限和时间属性,如果在复制的时候想保留文件原有的权限信息以及时间属性时,可以加参数 -p

    ghostwu@dev:~/linux/cp$ ls -l
    total 16
    -rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
    -rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
    -rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
    -rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
    ghostwu@dev:~/linux/cp$ cp -p 2.txt 5.txt
    ghostwu@dev:~/linux/cp$ ls -l
    total 20
    -rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
    -rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
    -rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
    -rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
    -rw-r--rw- 1 ghostwu ghostwu 19 5月   6 17:48 5.txt

    -i: 带提示信息的复制,默认情况下,cp命令会直接覆盖

    ghostwu@dev:~/linux/cp$ ls -l
    total 20
    -rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
    -rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
    -rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
    -rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
    -rw-r--rw- 1 ghostwu ghostwu 19 5月   6 17:48 5.txt
    ghostwu@dev:~/linux/cp$ cp 2.txt 5.txt 
    ghostwu@dev:~/linux/cp$ cp -i 2.txt 5.txt 
    cp: overwrite '5.txt'? y

    -r参数: 递归复制目录以及文件

    ghostwu@dev:~/linux/cp$ ls -l
    total 20
    -rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
    -rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
    -rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
    -rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
    -rw-r--rw- 1 ghostwu ghostwu 19 5月   6 18:04 5.txt
    ghostwu@dev:~/linux/cp$ mkdir -p a/b
    ghostwu@dev:~/linux/cp$ mv *.txt a/b/
    ghostwu@dev:~/linux/cp$ tree
    .
    └── a
        └── b
            ├── 1.txt
            ├── 2.txt
            ├── 3.txt
            ├── 4.txt
            └── 5.txt
    
    2 directories, 5 files
    ghostwu@dev:~/linux/cp$ cp a a2
    cp: omitting directory 'a'
    ghostwu@dev:~/linux/cp$ ls
    a
    ghostwu@dev:~/linux/cp$ cp -r a a2
    ghostwu@dev:~/linux/cp$ tree
    .
    ├── a
    │   └── b
    │       ├── 1.txt
    │       ├── 2.txt
    │       ├── 3.txt
    │       ├── 4.txt
    │       └── 5.txt
    └── a2
        └── b
            ├── 1.txt
            ├── 2.txt
            ├── 3.txt
            ├── 4.txt
            └── 5.txt
    
    4 directories, 10 files
    ghostwu@dev:~/linux/cp$ 

    通过alias别名,给cp命令加提示信息

    ghostwu@dev:~/linux/cp$ alias cp='cp -i'
    ghostwu@dev:~/linux/cp$ ls
    a  a2
    ghostwu@dev:~/linux/cp$ touch 1.txt
    ghostwu@dev:~/linux/cp$ cp 1.txt 2.txt
    ghostwu@dev:~/linux/cp$ cp 1.txt 2.txt 
    cp: overwrite '2.txt'? y
    ghostwu@dev:~/linux/cp$ 

    使用命令的绝对路径(全路径),可以屏蔽别名

    ghostwu@dev:~/linux/cp$ alias | grep cp
    alias cp='cp -i'
    ghostwu@dev:~/linux/cp$ ls
    1.txt  2.txt  a  a2
    ghostwu@dev:~/linux/cp$ cp 1.txt 2.txt 
    cp: overwrite '2.txt'? y
    ghostwu@dev:~/linux/cp$ which cp
    /bin/cp
    ghostwu@dev:~/linux/cp$ /bin/cp 1.txt 2.txt 

    使用反斜杠,也可以屏蔽系统别名

    ghostwu@dev:~/linux/cp$ cp 1.txt 2.txt 
    ghostwu@dev:~/linux/cp$ cp 2.txt 1.txt 

    -a参数,相当于-r -d -p三个参数的综合作用效果

    ghostwu@dev:~/linux/cp$ ls
    1.txt  2.txt  a  a2
    ghostwu@dev:~/linux/cp$ cp -a a a3
    ghostwu@dev:~/linux/cp$ ls
    1.txt  2.txt  a  a2  a3
  • 相关阅读:
    CentOS7升级系统内核
    ASP.NET MVC控制器里捕获视图的错误验证信息(ErrorMessage)
    Android Studio小技巧
    ASP.NET 预编译笔记
    EnyimMemcached(64位)使用实例
    SQL Server Profiler小技巧——筛选请求
    [疑难杂症]解决实际开发中各种问题bug
    [整理]EF6.X更新了什么(版本历史中文版)
    史上最全的ASP.NET MVC路由配置,以后RouteConfig再弄不懂神仙都难救你啦~
    微信开发调试小工具进化→微信用户发送信息模拟器发布!——这标题起真是好数码暴龙的说
  • 原文地址:https://www.cnblogs.com/ghostwu/p/8999060.html
Copyright © 2011-2022 走看看