zoukankan      html  css  js  c++  java
  • Linux命令——cp、rm、mv、touch、file、dir

    cp

    copy 拷贝文件

    拷贝过程不指定目标文件名 则目标文件名和源文件名一样

    [root@WebServer ~]# cp /91xueit/teacher.txt 51cto/
    View Code

    拷贝过程指定目标文件名称

    [root@WebServer ~]# cp /91xueit/teacher.txt 51cto/teacher1.txt
    [root@WebServer ~]# cp 51cto/teacher.txt 51cto/teacher1.txt /tmp
    View Code

    51cto文件夹中扩展名是txt的文件拷贝到当前目录

    [root@WebServer ~]# cp 51cto/*.txt .
    View Code

    51cto文件夹拷贝到/tmp文件夹,-R  或 -r 递归复制目录及其子目录的所有内容

    [root@WebServer ~]# cp -R 51cto/ /tmp
    View Code

    默认情况下,拷贝文件时文件权限会变化。只有管理员能够将文件拷贝 权限不变,使用-p

    [root@51cto ~]# cp /home/wangyan/wangyan.txt ./
    [root@51cto ~]# ll
    total 20
    -rw-------. 1 root root 1041 May 25 05:25 anaconda-ks.cfg
    -rw-r--r--. 1 root root 9714 May 25 05:25 install.log
    -rw-r--r--. 1 root root 3161 May 25 05:25 install.log.syslog
    -rw-r--r--. 1 root root    0 May 30 03:34 wangyan.txt
    [root@51cto ~]# cp /home/wangyan/wangyan.txt ./ -p
    cp: overwrite `./wangyan.txt'? y
    [root@51cto ~]# ll
    total 20
    -rw-------. 1 root    root    1041 May 25 05:25 anaconda-ks.cfg
    -rw-r--r--. 1 root    root    9714 May 25 05:25 install.log
    -rw-r--r--. 1 root    root    3161 May 25 05:25 install.log.syslog
    -rw-rw-r--. 1 wangyan wangyan    0 May 30 03:33 wangyan.txt
    View Code

    -d 保留文件的软链接属性

    -a 保留所有属性

    大家知道linux下复制目录可以通过,cp -r dirname destdir,但是这样复制的目录属性会发生变化,想要使得复制之后的目录和原目录完全一样,可以使用cp -a dirname destdir 

    -p(小P)复制时保留mode、ownership、timestamps

    -i :  覆盖时提示

    -n   不覆盖已存在文件

    -f:覆盖已经存在的目标文件而不给出提示

    -u:只有源文件较目标文件新时复制

    命令格式为:cp -u 源文件 目标文件

    这个命令很实用,尤其是在更新文件时。如下图所示,只有源文件比目标文件新时,才会将源文件复制给目标文件,否则,及时执行了命令,也不会执行复制。

    -s :创建文件的软链接

    命令格式为:cp -s 源文件 目标文件

    也可以用ln命令实现同样的功能。当一个文件路径太深(如下述的a/b/c/d/e/orginalFile.txt),访问起来十分不方便时,就会创建这个文件的软链接,使之访问起来更方便些。软链接就相当于windows上的快捷方式。

    -l:创建文件的硬链接

    rm

    remove 删除

    删除单个文件

    [root@WebServer ~]# rm 51cto/teacher.txt
    View Code

    -r 递归删除51cto目录中的文件和文件夹,51cto目录不删

    [root@WebServer ~]# rm -r 51cto/*
    View Code

    删除51cto目录

    [root@WebServer ~]# rm -r 51cto
    View Code

    以上删除,会有提示,因为alias里面rm时rm -i的别名。-f 强制删除 没有提示

    [root@WebServer ~]# rm -f /tmp/123
    View Code

    -i:删除前逐一询问确认。通常Linux对rm进行了从命名,默认就把-i加上了

    [root@localhost ~]# alias rm
    alias rm='rm -i'
    View Code

     

    mv

    move 移动文件 或 文件夹

    https://www.cnblogs.com/MenAngel/p/5465162.html

    -b:当文件存在时,覆盖前,为其创建一个备份。备份文件以~结尾

    [root@localhost Document]# cat >myword <<EOF
    > this is my word!
    > EOF
    [root@localhost Document]# cat >text <<EOF
    > this is my text!
    > EOF
    [root@localhost Document]# mv -b myword text                                            //在一个文件即将覆盖另一个文件时,默认是提醒的,所以加上-i参数和不加是一样的
    mv:是否覆盖"text"? y
    [root@localhost Document]# cat myword
    cat: myword: 没有那个文件或目录
    [root@localhost Document]# cat text
    this is my word!
    [root@localhost Document]# ll
    总用量 8
    drwxr-xr-x. 2 root      root      23 5月   5 22:35 mytext                      //这里text里存的是前面myword的内容,text的内容备份到text~中,需要特殊软件才能查看
    -rw-r--r--. 1 root      root      17 5月   5 22:41 text
    -rw-rw-r--. 1 sunjimeng sunjimeng 17 5月   5 22:41 text~
    View Code

    -i:交互式操作,覆盖前先行询问用户,如果源文件与目标文件或目标目录中的文件同名,则询问用户是否覆盖目标文件。这样可以避免误将文件覆盖。默认是以alias别名的方式带-i参数

    [root@localhost ~]# alias mv
    alias mv='mv -i'
    View Code

    -n  不覆盖已存在文件

    -f:强制的意思,如果目标文件已经存在,不会询问而直接覆盖

    -u:若目标文件已存在需移动的同名文件,且源文件比较新,才会更新文件

    -t :  移动多个源文件到一个目录的情况,此时target在前,source在后。

    mv  dir/  anaconda-ks.cfg

    -v:verbose

    touch

    能够改变文件的时间戳,touch文件一下,文件的3个时间都会改变。

    最近一次访问时间

    最近一次修改时间  改变文件的内容。 内容变化——>文件大小变化——>元数据变化

    最近一次改变时间  元数据的改变 eg:文件名 大小 权限

    单独更改access时间,-a,change时间也会跟着变

    单独更改modify时间,-m,change时间也会跟着变

    因此,也就没有参数单独修改change时间。

    使用指定时间,而非当前时间更改modify时间。-t 或者 -d

    [root@51cto ~]# touch -m -t 199312312359.59 wangyan.txt 
    [root@51cto ~]# stat wangyan.txt 
      File: `wangyan.txt'
      Size: 0             Blocks: 0          IO Block: 4096   regular empty file
    Device: 802h/2050d    Inode: 783368      Links: 1
    Access: (0664/-rw-rw-r--)  Uid: (  500/ wangyan)   Gid: (  500/ wangyan)
    Access: 2018-05-30 03:34:36.776992594 +0800
    Modify: 1993-12-31 23:59:59.000000000 +0800
    Change: 2018-05-30 04:07:58.569981915 +0800
    View Code

    touch file时file不存在默认创建,file存在则修改3个时间。使用-c可以取消这种默认行为,即文件不存在也不创建

    file

    查看文件类型

    https://www.cnblogs.com/Dodge/p/4278306.html

    通常file可以告诉我们目标文件是ELF文件还是脚本文件(得知是脚本文件是通过脚本内部#!后面的信息判断的)

    -b:列出文件辨识结果时,不显示文件名称。

    [root@localhost ~]# file /usr/bin/sh
    /usr/bin/sh: symbolic link to `bash'
    [root@localhost ~]# file /usr/bin/sh -b
    symbolic link to `bash'
    View Code

    -f :列出文件中文件名的文件类型

    [root@localhost ~]# cat haha.txt 
    /usr/bin/cp
    /usr/bin/gzip
    [root@localhost ~]# file -f haha.txt 
    /usr/bin/cp:   ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=47c2259e084c64fb00ec01bda8a57c005e3516d5, stripped
    /usr/bin/gzip: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=526d77ff7164870f948d8f97aaf0a888cc561b30, stripped
    View Code

    -L:查看对应软链接指向文件的文件类型

    [root@localhost ~]# ll /usr/bin/sh
    lrwxrwxrwx. 1 root root 4 Oct  5 17:14 /usr/bin/sh -> bash
    [root@localhost ~]# file /usr/bin/sh
    /usr/bin/sh: symbolic link to `bash'
    [root@localhost ~]# file /usr/bin/sh -L
    /usr/bin/sh: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=765c505bb8a234fcd64ede405fa7fcb25734f06a, stripped
    View Code

    -z:尝试去解读压缩文件的内容

    --help:显示命令在线帮助

    -version:显示命令版本信息

    [root@51cto ~]# file /etc/passwd
    /etc/passwd: ASCII text
    [root@51cto ~]# file /bin/cat 
    /bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
    View Code

    dir

    功能和ls一样,知道就行,没啥人用

    Linux dir command for beginners (10 examples)

    What's the difference between “dir” and “ls”?

  • 相关阅读:
    剑指offer——从尾到头打印链表节点的值
    1, sync_with_stdio(), tie()的应用
    Python基础1:一些小知识汇总
    HTML
    CSS
    周总结
    十三章
    十二章总结
    十一章总结
    第十一章
  • 原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9105849.html
Copyright © 2011-2022 走看看