• 文件属性及find命令总结


    原文转载来源:https://www.cnblogs.com/struggle-1216/

    第1章   文件属性    

    1.1   文件的属性

      1.1.1    查看文件的详细属性

          PS:ls查看的文件或目录默认的是按照名字的第一个字母进行正序排序

          ls 参数选项:

                    -t      # 按照时间排序

                    -r      # 逆序排序 

           按照时间进行逆序排序

    1
    2
    3
    4
    5
    [root@oldboyedu-lnb ~]# ls -lrt /etc   
     
    [root@oldboyedu-lnb ~]# ls -l
     
    -rw-r--r--. 1 root root  138 Jul 30 09:34 1.s 

      1.1.2  如何识别linux中的文件或目录

     

                  a. 使用颜色分辨

     

                  b. 使用后缀名  windows使用的方法 linux的后缀名是给我们自己看的 命令ll 查看

     

                  c. 使用命令来辨别

     

    1.2  文件属性每列含义    

     

         -   rw-r--r--   .  1  root root  138 Jul 30 09:34 1.sh

        ---  ---------- --  -  ---  ----  ----------------    ----

         1     2        3   4   5   6             7             8      

     

      1.2.1  文件的类型

     

              什么是文件的类型:

     

             普通文件   -

     

               目录       d

     

             可执行文件  x

     

             .mp4 .PDF

     

          1.2.1.1   windows系统文件的类型:

     

                     特点:

     

                          以后缀名来区分的

     

                         修改后缀名 无法使用

     

                         .exe

     

                         .txt

     

                         .doc

     

                         .pdf

     

                         .avi .mp4

     

                         ...........

     

        1.2.1.2   Linux系统文件的类型:

     

                  不是以后缀名来区分 后缀名只是给我们自己看的

     

                     .txt 普通文件

     

                     .sh   shell脚本

     

                     .rpm 安装包

     

                     .jpg  图片

     

                     .py     python脚本         

     

        1.2.1.3    Linux文件系统类型分类

     

                     -   普通文件

     

                     有三种文件的类型都是使用-来表示

     

                         第一种:

     

                          -  普通文本文件    # 使用echo vim vi touch 等创建的文件

     

                         第二种:

     

                         -  二进制文件      # 系统中的命令

     

                         第三种:

     

                         -  数据文件              # .rpm包 压缩包

     

                          d    directory 目录

     

                  drwxr-xr-x. 2 root root   6 Jul 30 08:22 3 # 文件详细信息的最前面的第一列 d 代表目录

     

                           l  小写的L  软链接文件  softlink 类似于windows的快捷方式  符号链接

     

                          /etc/rc.local -> rc.d/rc.local     # 访问/etc/rc.local---> 真实访问的是/etc/rc.d/rc.local                         

     

                          c  字符设备文件       

     

    1
    2
    [root@oldboyedu-lnb ~]# ll /dev/urandom               # 生成随机数  一直不停的吐
    crw-rw-rw- 1 root root 1, 9 May 22  2018 /dev/urandom

     

                    PS:tr 

     

                              -c  取反

     

                              -d  删除  

     

    1
    2
    3
    4
    [root@oldboyedu-lnb ~]# tr -cd "a-z0-9A-Z" < /dev/urandom|head -c10
     扩展: 使用random生成随机数 获取前8个字符
     [root@oldboyedu-lnb ~]# ll /dev/null    # 系统中的黑洞 把任意输出内容都可以定向到null
     crw-rw-rw- 1 root root 1, 3 May 22  2018 /dev/null   

     

    PS: null的意义 在执行命令的时候 我们不想输出查看命令的结果 因为结果太多太长 把结果定向到空 使用的特定的命令来判定上一条命令是否执行成功

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@oldboyedu-lnb ~]# ping -c4 -W1 www.baidu.com > /dev/null
    [root@oldboyedu-lnb ~]# echo $?
    0
    ping的结果放入黑洞null 使用$?来判定是否ping的通  上一条命令是否执行成功 0为成功 非0失败[root@oldboyedu-lnb ~]# ll /etc/hehehe.txt
    ls: cannot access /etc/hehehe.txt: No such file or directory
    [root@oldboyedu-lnb ~]# echo $?
     2
    [root@oldboyedu-lnb ~]# ll /etc/hosts
    -rw-r--r--. 1 root root 158 Jul 24 10:18 /etc/hosts
    [root@oldboyedu-lnb ~]# echo $?
     0
      

    33689416 -   rw-r--r-- .  1  root root          5 Jul 30 11:10        1.txt

    --------     ---------- -- -- --- -----       ----------------  ----

    1            2       3      4  5   6          7                   8                        9

    1  node index node 索引节点

      文件的权限 详细信息 存储在inode索引节点 存放着文件具体的位置

    2  文件的类型

       - 普通文件

       - 二进制

       - 数据文件

       d   目录文件

       l   软链接文件

       c   字符设备文件

       b   块设备

       ----------

       p  网络套接字

       s  管道文件

    3 文件的权限 决定了当前的用户对当前的文件拥有什么权限

       r    # read    读       cat file.txt less file.txt more head tail...

       w    # write   写入    vi vim cat echo ...

       x    # execute 可执行  可以运行文件中的命令         可执行颜色变绿

        如何执行文件 ./1.txt 或者全路径方式执行 /root/1.txt

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [root@oldboyedu-lnb ~]# ./1.txt
    -bash: ./1.txt: Permission denied
    [root@oldboyedu-lnb ~]# chmod +x 1.txt
    [root@oldboyedu-lnb ~]# ll 1.txt
    -rwxr-xr-x 1 root root 4 Aug  3 08:16 1.txt
    [root@oldboyedu-lnb ~]# ./1.txt
     /root
    [root@oldboyedu-lnb ~]# cat 1.txt
     pwd
    [root@oldboyedu-lnb ~]# /root/1.txt
    /root

           Linux中文件默认的权限是  rw-r--r--

           为什么Linux中文件默认的权限没有x权限

           普通文本存放什么内容: 字符串信息    配置文件

           文件中有可执行的命令: 不是普通文本  SHLL脚本  需要有x 执行权限

    4  .开启了selinux后创建文件自带的  关闭selinux点消失

    5 数字1 代表了硬链接的个数

           什么是硬链接:

           相当于文件有多个入口   类似于超市有个入口  删除一个入口 对文件没有影响

           默认的目录的硬链接个数为2

    1
    2
    3
    4
    5
    6
    7
    [root@oldboyedu-lnb test]# ll -ldi ../test
     17454490 drwxr-xr-x 2 root root 6 Aug  3 08:27 ../test
    [root@oldboyedu-lnb test]# ll -lai
    total 0
    17454490 drwxr-xr-x  2 root root  6 Aug  3 08:27 .
    17454451 drwxr-xr-x. 3 root root 44 Aug  3 08:27 ..
    [root@oldboyedu-lnb test]# #cp /etc/hosts /root/3/test==== cp /etc/hosts .

    6 文件的属主

           当前的文件属于哪个用户

    7 文件的属组

              当前的文件属于哪个组

           Access     访问时间

           Modify     修改时间

           Change     改变时间

    8  文件名称

     

    第2章    正确与错误重定向

     

    Linux给程序提供三种 I/O 设备

     

    1
    2
    3
    4
    5
    标准输入(STDIN) -文件描述符为0 默认接受来自键盘的输入
     
    标准输出(STDOUT)-文件描述符为1 默认输出到终端窗口
     
    标准错误(STDERR)-文件描述符为2 默认输出到终端窗口

     

    2.1  标准正确输出重定向

     

    所有命令执行成功后的结果称为正确的把正确的结果定向到文件中或者空称为标准正确输出重定向.

     

           1>     正确重定向                  PS: 1只接收命令返回的正确的结果 错误的结果扔到屏幕

     

           1>>   正确追加重定向         PS: 加1和不加都是只接收正确的结果

     

           1>  ======= >

     

           1>> ====== >>

     

                   ------------------------------- 只接收正确的结果

     

    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
    27
    28
    29
    30
    31
    [root@oldboyedu-lnb ~]# ls 1.sh
     
    1.sh
     
    [root@oldboyedu-lnb ~]# ls 1.sh > ok.txt
     
    [root@oldboyedu-lnb ~]# cat ok.txt
     
    1.sh
     
    [root@oldboyedu-lnb ~]# ls 1.sh >> ok.txt
     
    [root@oldboyedu-lnb ~]# cat ok.txt
     
    1.sh
     
    1.sh
     
    [root@oldboyedu-lnb ~]# ls 1.sh 1> ok.txt
     
    [root@oldboyedu-lnb ~]# cat ok.txt
     
    1.sh
     
    [root@oldboyedu-lnb ~]# ls 1.sh 1>> ok.txt
     
    [root@oldboyedu-lnb ~]# cat ok.txt
     
    1.sh
     
    1.sh   

     

    2.2  标准错误输出重定向

     

    所有命令执行失败后的结果 称为错误的 把错误的结果定向到文件或者空 称为标准错误输出重定向

     

              2>        错误重定向     # 只接收错误的结果     不接收正确的结果

     

              2>>      错误追加重定向 # 只追加接收错误的结果 不接收正确的结果    

     

             &>file 把标准输出和标准错误都重定向到file

     

            &>>file 把标准输出和标准错误都追加重定向到file

     

            1>&2  标准输出值传递给2输出通道 &2表示2输出通道

     

            2>&1  标准错误值传递给1输出通道 &1表示1输出通道.      

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@oldboyedu-lnb ~]# ls 1.txt 2> error.txt
    1.txt
    [root@oldboyedu-lnb ~]# cat error.txt
    [root@oldboyedu-lnb ~]# ls 1.txtt 2> error.txt
    [root@oldboyedu-lnb ~]# cat error.txt
    ls: cannot access 1.txtt: No such file or directory
    [root@oldboyedu-lnb ~]# ls 1.txtt 2>> error.txt
    [root@oldboyedu-lnb ~]# cat error.txt
    ls: cannot access 1.txtt: No such file or directory
    ls: cannot access 1.txtt: No such file or directory

     

    2.3  接收正确和错误的结果

     

           方法1

     

    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
    27
    28
    29
    30
    31
    [root@oldboyedu-lnb ~]# cat error.txt
     
     [root@oldboyedu-lnb ~]# ls aa.txt >>ok.txt 2>>error.txt
     
     [root@oldboyedu-lnb ~]# cat error.txt
     
     ls: cannot access aa.txt: No such file or directory
     
     [root@oldboyedu-lnb ~]# ls 1.txt >>ok.txt 2>>error.txt
     
     [root@oldboyedu-lnb ~]# cat ok.txt
     
     ls: cannot access aa.txt: No such file or directory
     
    ls: cannot access a.txt: No such file or directory
     
     1.txt
     
    1.txt
     
     [root@oldboyedu-lnb ~]# > ok.txt
     
     [root@oldboyedu-lnb ~]# ls 1.txt >>ok.txt 2>>error.txt
     
     [root@oldboyedu-lnb ~]# cat ok.txt
     
    1.txt
     
     [root@oldboyedu-lnb ~]# cat error.txt
     
     ls: cannot access aa.txt: No such file or directory

     

         方法2: 

     

    1
    2
    3
    [root@oldboyedu-lnb ~]# ls 1.txttt >>ok.txt 2>&1
    [root@oldboyedu-lnb ~]# cat ok.txt
    ls: cannot access 1.txttt: No such file or directory

     

           方法3:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@oldboyedu-lnb ~]# ls 1.txt &>>ok.txt
     
    [root@oldboyedu-lnb ~]# ls 1.txttt &>>ok.txt
     
    [root@oldboyedu-lnb ~]# cat ok.txt
     
    1.txt
     
    ls: cannot access 1.txttt: No such file or directory

     

    2.4  扩展

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    [root@oldboyedu-lnb ~]# sh 1.sh www.baidu.com
     
    网站通
     
    [root@oldboyedu-lnb ~]# sh 1.sh www.baidu.commmm
     
    网站不通
     
    [root@oldboyedu-lnb ~]# cat 1.sh
     
    ping -c1 -W1 $1 &>/dev/null
     
    [ $? -eq 0 ] && echo "网站通" || echo "网站不通"
     
    ---------------------------------------
     
    [root@oldboyedu-lnb ~]# ll /dev/zero # 生成大文件
     
    crw-rw-rw- 1 root root 1, 5 May 22  2018 /dev/zero

     

      扩展:  生成1G的文件

     

            dd if=/dev/zero of=/root/1G bs=10M count=100 

             b  块设备   硬件设备 磁盘 光驱 U盘

             brw-rw---- 1 root disk 8, 0 May 22  2018 /dev/sda

             brw-rw---- 1 root cdrom 11, 0 May 22  2018 /dev/sr0

              s  网络套接字

              p  管道文件

     

    第3章    文件相关的命令

     

    3.1  which  # 查看命令的全路径

     

      

    1
    2
    3
    4
    5
    6
    7
    [root@oldboyedu-lnb ~]# which mkdir
     
    /usr/bin/mkdir
     
    [root@oldboyedu-lnb ~]# ll /usr/bin/mkdir
     
    -rwxr-xr-x. 1 root root 79760 Apr 11  2018 /usr/bin/mkdir

     

    3.2  whereis  # 查看命令的全路径和命令的帮助文件位置 了解

     

    3.3     file       # 查看文件的类型

     

    1
    2
    3
    4
    5
    6
    7
    8
    [root@oldboyedu-lnb ~]# file 1.txt
    1.txt: ASCII text
    [root@oldboyedu-lnb ~]# file 3
     3: directory
    [root@oldboyedu-lnb ~]# file all.tar.gz
     all.tar.gz: gzip compressed data, from Unix, last modified: Fri Jul 31 10:42:15 2020
    [root@oldboyedu-lnb ~]# file /etc/rc.local
     /etc/rc.local: symbolic link to `rc.d/rc.local' 3) find按照文件的类型进行查找文件  用来查找文件 不支持查找文件中的内容

     

    3.4    find                                                                                       

     

                  find 语法

     

     find 在哪里找  类型  特点

     

                  find 北京      男的  高富帅

     

                  find ./       -type  f

     

                                   -type的类型

     

                                   f  普通文件

     

                                   d  目录

     

                                   l  软链接

     

                                   b  块设备

     

                                   c  字符设备

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    `b'    for 512-byte blocks (this is the default if no suffix is used)
     
    `c'    for bytes   (推荐)
     
    `w'    for two-byte words
     
    `k'    for Kilobytes (units of 1024 bytes) (推荐)
     
    `M'   for Megabytes (units of 1048576 bytes) (推荐)
     
    `G'   for Gigabytes (units of 1073741824 bytes)

     

      3.4.1  精确查找:

     

          find 路径信息 -type 文件类型 -name "文件名"

     

      3.4.2  模糊查找:

     

          find 路径信息 -type 文件类型 -name "文件名*"

     

          find 路径信息 -type 文件类型 -name "*文件名" 

     

          find 路径信息 -type 文件类型 -name "文*件名"

     

      3.4.3  忽略大小写查找:

     

          find 路径信息 -type 文件类型 -iname "文件名"

     

        3.4.3.1   查找当前目录的所有普通文件 默认会显示隐藏的文件        

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    [root@oldboyedu-lnb 3]# find ./ -type f
     
     ./1.txt
     
     ./2.txt
     
     ./3.txt
     
     ./4.txt
     
     ./5.txt
     
     ./6.txt
     
     ./7.txt
     
     ./8.txt
     
     ./9.txt
     
     ./10.txt
     
        

     

        3.4.3.2     查找当前目录下所有的目录文件        

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@oldboyedu-lnb 3]# find /root/3/ -type d
    /root/3/
    /root/3/stu01
    /root/3/stu02
    /root/3/stu03
    /root/3/stu04
    /root/3/stu05
    /root/3/stu06
     /root/3/stu07
     /root/3/stu08
     /root/3/stu09
     /root/3/stu10

     

        3.4.3.3     查找当前目录下的oldboy.txt

     

                -name    #按照名称精确查找       

    1
    2
    [root@oldboyedu-lnb 3]# find ./ -type f -name "oldboy.txt"
     ./oldboy.txt

                -name   #按照名称模糊查找  

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    [root@oldboyedu-lnb 3]# find ./ -type f -name "*.txt"
     ./1.txt
     ./2.txt
     ./3.txt
     ./4.txt
     ./5.txt
     ./6.txt
     ./7.txt
     ./8.txt
     ./9.txt
     ./10.txt
    ./oldboy.txt<br>[root@oldboyedu-lnb 3]# find ./ -type d -name "stu*"
    ./stu01
    ./stu02
    ./stu03
    ./stu04<br>./stu05
    ./stu06
    ./stu07
    ./stu08
    ./stu09<br>./stu10

     

        3.4.3.4    -iname  不区分大小来查找文件     

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@oldboyedu-lnb 3]# find ./ -type f -iname "*.txt"
      ./1.txt
      ./2.txt
      ./3.txt
      ./4.txt
      ./5.txt
      ./6.txt
      ./7.txt
      ./8.txt
      ./9.txt
      ./10.txt
      ./oldboy.txt
       ./1.TXT
       ./2.TXT
       ./3.TXT

     

      find 默认的是查找所有的目录及目录下面的所有文件

     

       -maxdepth  深度等级

     

    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
    27
    28
    29
    30
    31
    32
    [root@oldboyedu-lnb 3]# touch stu{1..3}/{bbs,blog,www}.conf
     
    [root@oldboyedu-lnb 3]# find ./ -maxdepth 1 -type f
    ./1.txt
     
    ./2.txt
     
     ./3.txt
     
    [root@oldboyedu-lnb 3]# find ./ -maxdepth 2 -type f
    ./1.txt
    ./2.txt
     ./3.txt
    ./stu1/bbs.conf
    ./stu1/blog.conf
    ./stu1/www.conf
    ./stu2/bbs.conf
    ./stu2/blog.conf
    ./stu2/www.conf
    ./stu3/bbs.conf
    ./stu3/blog.conf
    ./stu3/www.conf
    [root@oldboyedu-lnb 3]# find stu{1..3}/ -maxdepth 1 -type f
    stu1/bbs.conf
    stu1/blog.conf
    stu1/www.conf
    stu2/bbs.conf
    stu2/blog.conf
    stu2/www.conf
    stu3/bbs.conf
    stu3/blog.conf
    stu3/www.conf

     

        3.4.3.5    按照文件的大小查找

     

                  -size   -k -M -G

     

                  a.在当前目录查找大于1M的文件

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@oldboyedu-lnb 3]# find ./ -type f -size +1M
     
    ./all.txt
     
    [root@oldboyedu-lnb 3]# find ./ -type f -size +500k
     
    ./services
     
    ./all.txt
     
     [root@oldboyedu-lnb ~]# find ./ -type f -size +1G
     
     ./1G

     

              b.查找/root目录下 大于1M并且小于2G的文件

     

    1
    2
    3
    [root@oldboyedu-lnb ~]# find /root/ -type f -size +1M -and  -size -3G
    /root/3/all.txt
    /root/1G

     

    -and  并且 并集关系 默认就是并且

     

    find执行的结果调用

     

    第4章    文件的类型

     

    4.1 使用find按照类型查找文件

     

            -type f  d l      #

     

            find按名字查找

     

            -name file.txt

     

           模糊查找

     

           -name "*.txt"

     

           -name "stu.*"

     

           -name "*.*"

     

           不区分大小

     

           -iname "*.txt"

     

           按照深度等级查找

     

           --maxdepth 1

     

           find ./ -maxdepth 1 -type f

     

           按照文件的大小查找

     

           -size + 大于多少 - 小于多少

     

           find ./ -type f -size +1G

     

           -and 并且 两端同时成立  -o  or 或者

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@oldboyedu-lnb ~]# find ./ -type f -name "*.bak" -size +1G
     ./1.bak
     -o 或者
    [root@oldboyedu-lnb ~]# find ./ -type f -name "*.txt" -o -size +1G
    ./3/1.txt
    ./3/2.txt
    ./2.txt
    ./all.txt
    ./error.txt
    ./1.txt
    ./1.bak
    ./ok.txt

     

    4.2  find命令补充   

     

    PS:注意中文语法的问题

     

      find可以查找文件目录

     

      对查找到的文件或者目录做cp ls mv rm操作

     

      只要是输出到屏幕上的内容称为普通字符串 只有用命令调用普通字符串的时候 才称为文件或目录名称

     

    4.3 xargs                    

     

    作用:可以把其他命令输出的字符串信息 传递到其他命令的最后面 来当做文件或目录名称.

     

        语法格式:

     

           命令 [参数选项] file|xargs 命令 最后面这个位置

     

           示例:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@oldboyedu-lnb 3]# echo 1.txt 2.txt|xargs ls -l
    -rw-r--r-- 1 root root 0 Aug  3 08:20 1.txt
     -rw-r--r-- 1 root root 0 Aug  3 08:20 2.txt
    示例:
    [root@oldboyedu-lnb 3]# echo 111 >1.txt
    [root@oldboyedu-lnb 3]# echo 222 >2.txt
    [root@oldboyedu-lnb 3]# echo 1.txt 2.txt|xargs cat
    111
    222

     

    4.4  find结合xargs   find不支持别名

     

      4.4.1  把find查找到的文件 交给ls命令

     

    1
    2
    3
    4
    [root@oldboyedu-lnb 3]# find ./ -type f|xargs ls -l
     -rw-r--r-- 1 root root 4 Aug  3 08:57 ./1.txt
     -rw-r--r-- 1 root root 4 Aug  3 08:57 ./2.txt
     -rw-r--r-- 1 root root 0 Aug  3 08:41 ./hehe.

     

      4.4.2  把find查找的文件交给 rm 命令  find不支持别名 所以 rm慎用

     

    1
    [root@oldboyedu-lnb 3]# find ./ -type f -name "1.txt"|xargs rm

     

      4.4.3  把find查找到的文件 交给 mv命令

     

           格式:

     

    1
    2
    3
    4
    5
    6
    [root@oldboyedu-lnb 3]# find ./ -type f -name 1.txt|xargs mv 1.bak 1.txt  # 错误的使用方式
    [root@oldboyedu-lnb 3]# find ./ -type f -name 1.txt|xargs –i(使用大括号要用这个) mv {} /tmp/1.bak    # 正确的格式
    [root@oldboyedu-lnb 3]# find ./ -type f -name 1.txt|xargs -i mv {} /tmp/1.bak
    [root@oldboyedu-lnb 3]# ll 1.txt /tmp/1.bak
     ls: cannot access 1.txt: No such file or directory
     -rw-r--r-- 1 root root 0 Aug  3 09:21 /tmp/1.bak

     

      4.4.4  把find查找到的文件 交给 cp命令

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@oldboyedu-lnb 3]# find ./ -type f -name 2.txt|xargs cp -t /tmp/
     
     [root@oldboyedu-lnb 3]# ll 2.txt /tmp/2.txt
     
     -rw-r--r--  1 root root 0 Aug  3 09:21 2.txt
     
     -rw-r--r--. 1 root root 0 Aug  3 09:28 /tmp/2.txt
     
     [root@oldboyedu-lnb 3]# find ./ -type f -name 3.txt|xargs -i cp {} /tmp
     
     [root@oldboyedu-lnb 3]# ll 3.txt /tmp/3.txt
     
     -rw-r--r--  1 root root 0 Aug  3 09:21 3.txt
     
     -rw-r--r--. 1 root root 0 Aug  3 09:29 /tmp/3.txt

     

    4.4.5    结合grep过滤查找到文件的字符串

     

    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@oldboyedu-lnb 3]# find ./ -type f
     
      ./hehe.
     
      ./2.txt
     
      ./3.txt
     
      ./4.txt
     
      ./5.txt
     
      ./6.txt
     
      ./7.txt
     
      ./8.txt
     
      ./9.txt
     
      ./10.txt
     
      [root@oldboyedu-lnb 3]# find ./ -type f |xargs grep oldboy
     
      ./10.txt:oldboy

     

           PS: grep 默认只过滤当前目录 递归过滤文件内容使用-R

     

    1
    2
    [root@oldboyedu-lnb 3]# grep -R oldboy ./*
     ./test/10.txt:oldboy    

     

           ---------------- 批量查找文件 批量改名

     

        find ./ -type f |xargs -i cp {} {}.bak

     

    4.5   find查找到的内容交给其他命令

     

        命令格式:

     

        find ./ -type -name 1.txt -exec 命令 {} ;

     

      4.5.1    find的结果交给ls 查看

     

    1
    2
    [root@oldboyedu-lnb 3]# find ./ -type f -name 2.txt -exec ls -l {} ;
    -rw-r--r-- 1 root root 0 Aug  3 09:21 ./2.txt

     

      4.5.2    find的结果交给rm 使用

     

    1
    2
    3
    [root@oldboyedu-lnb 3]# find ./ -type f -name 2.txt -exec rm {} ;
    [root@oldboyedu-lnb 3]# ll 2.txt
    ls: cannot access 2.txt: No such file or directory

     

      4.5.3   find结果交给mv 使用

     

    1
    2
    3
    4
    [root@oldboyedu-lnb 3]# find ./ -type f -name 3.txt -exec mv {} /tmp/3.bak ;
    [root@oldboyedu-lnb 3]# ll 3.txt /tmp/3.bak
    ls: cannot access 3.txt: No such file or directory
    -rw-r--r-- 1 root root 0 Aug  3 09:21 /tmp/3.bak

     

      4.5.4    把find的结果交给cp使用

     

    1
    2
    3
    4
    [root@oldboyedu-lnb 3]# find ./ -type f -name 4.txt -exec cp {} /tmp/ ;
    [root@oldboyedu-lnb 3]# ll 4.txt /tmp/4.txt
    -rw-r--r--  1 root root 0 Aug  3 09:21 4.txt
    -rw-r--r--. 1 root root 0 Aug  3 09:42 /tmp/4.txt

     

      4.6   find查找的内容交给其他的命令

     

           语法格式:

     

           命令 `find ./ -type f -name``         # 反引号中执行后的结果 留在原地 供其他命令使用

     

      4.6.1    find的结果交给ls查看命令行支持别名

     

    1
    2
    [root@oldboyedu-lnb 3]# ll `find ./ -type f`
    -rw-r--r-- 1 root root 0 Aug  3 09:21 ./4.txt

     

      4.6.2    把find 结果交给rm

     

    1
    2
    3
    [root@oldboyedu-lnb 3]# rm -f `find ./ -type f -name 4.txt`
    [root@oldboyedu-lnb 3]# ll 4.txt
    ls: cannot access 4.txt: No such file or directory

     

      4.6.3    把find的结果交给mv

     

    1
    2
    3
    4
    [root@oldboyedu-lnb 3]# mv `find ./ -type f -name 5.txt` /tmp/5.bak
    [root@oldboyedu-lnb 3]# ll 5.txt /tmp/5.bak
    ls: cannot access 5.txt: No such file or directory
    -rw-r--r-- 1 root root 0 Aug  3 09:21 /tmp/5.bak

     

      4.6.4    把find的结果交给cp

     

    1
    [root@oldboyedu-lnb 3]# cp -r `find ./ -type d` /tmp/test

     

          PS: ;分号在命令行中有特殊含义  执行多条命令使用 不管前面的命令是否执行成功都会继续执行用来分隔命令

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [root@oldboyedu-lnb 3]# ls -l;mkdir oldboy;touch oldboy/test.txt
     total 0
     -rw-r--r-- 1 root root  0 Aug  3 09:21 6.txt
     -rw-r--r-- 1 root root  0 Aug  3 09:21 7.txt
     -rw-r--r-- 1 root root  0 Aug  3 09:21 8.txt
     -rw-r--r-- 1 root root  0 Aug  3 09:21 9.txt
     -rw-r--r-- 1 root root  0 Aug  3 08:41 hehe.
     drwxr-xr-x 2 root root 20 Aug  3 09:32 test
     [root@oldboyedu-lnb 3]# ll oldboy/
     total 0
     -rw-r--r-- 1 root root 0 Aug  3 10:10 test.txt

     

           -----命令执行失败 继续执行后面的命令

     

    1
    2
    3
    [root@oldboyedu-lnb 3]# lll -l;mkdir alex;touch lidao/hehe.txt
    -bash: lll: command not found
     touch: cannot touch ‘lidao/hehe.txt’: No such file or directory

     

      4.6.5    &&命令       # 前面的命令必须执行成功才执行后面的命令

     

             示例1:

     

    1
    2
    3
    4
    5
    [root@oldboyedu-lnb 3]# mkdir test && mkdir alex
    [root@oldboyedu-lnb 3]# ll
     total 0
     drwxr-xr-x 2 root root 6 Aug  3 10:13 alex
     drwxr-xr-x 2 root root 6 Aug  3 10:13 test

     

             示例2:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@oldboyedu-lnb 3]# mkdir oldboy
    [root@oldboyedu-lnb 3]# ll
     total 0
     drwxr-xr-x 2 root root 6 Aug  3 10:14 oldboy
     [root@oldboyedu-lnb 3]# mkdir oldboy && mkdir ale
     mkdir: cannot create directory ‘oldboy’: File exists
     [root@oldboyedu-lnb 3]# ll
     total 0
     drwxr-xr-x 2 root root 6 Aug  3 10:14 oldboy
     示例3:
     [root@oldboyedu-lnb 3]# ls -ld oldboy && echo hehe
     drwxr-xr-x 2 root root 6 Aug  3 10:14 oldboy
     hehe

     

      4.6.6    ||命令    # 前面的命令必须执行失败 才执行后面的命令 

     

    1
    2
    3
    4
    5
    6
    7
    8
    [root@oldboyedu-lnb 3]# ls test.txt || echo hehe
    ls: cannot access test.txt: No such file or directory
    hehe
    [root@oldboyedu-lnb 3]# ls test.txt || echo hehe && ls hehe.txt || echo ok
    ls: cannot access test.txt: No such file or directory
    hehe
    ls: cannot access hehe.txt: No such file or directory
    ok

     

       示例:

     

    1
    2
    3
    4
    5
    [root@oldboyedu-lnb 3]# cd alex|| mkdir alex
     -bashcd: alex: No such file or directory
    [root@oldboyedu-lnb 3]# ll
    total 0
    drwxr-xr-x 2 root root 6 Aug  3 10:18 alex

     

    第5章       文件压缩

     

    5.1 windows的压缩方式   .rar .rar5 .zip           # 提前安装rar压缩软件

     

    5.2 Linux的压缩方式:

     

           命令格式:

     

                         命令 [参数选项] 压缩包的名称.tar.gz  文件/目录

     

                         tar  -zcvf      test.tar.gz   /etc/hosts

     

                         命令 参数选项   筐子                 水果/香蕉/荔枝

     

                         tar  -zcvf      test.tar.gz   /etc/hosts 1.txt /tmp/test.txt

     

                         参数选项:

     

                        z   # 使用gzip方式压缩

                        c   # create  创建压缩包

                        v   # verbose 显示压缩的过程

                        f   # 指定文件

                       x   # 解压

                       t   # 查看压缩包的文件名称

                      C   # 指定解压到哪里

                      -P  # 不提示从成员中删除/    PS: 进入到相对路径打包 不提示

                    --exclude    # 排除文件  --exclude=file

                    --exclude-from # 排除文件中的文件名 --exclude-from=file

     

                     示例1: 把/etc/hosts文件打包成test.tar.gz

     

    1
    2
    3
    [root@oldboyedu-lnb ~]# tar zcvf hosts.tar.gz /etc/hosts
    tar: Removing leading `/' from member names
    /etc/hosts

     

        示例2: 不显示压缩过程   

     

    1
    2
    3
    [root@oldboyedu-lnb ~]# touch 1.txt 2.txt
    [root@oldboyedu-lnb ~]# tar zcf test.tar.gz {1..2}.txt
    -rw-r--r-- 1 root root 116 Aug  3 10:51 test.tar.gz

     

       示例3: 同时压缩打包压缩多个文件   

     

    1
    2
    3
    4
    5
    6
    [root@oldboyedu-lnb ~]# tar zcvf all.tar.gz *.txt /etc/hosts /etc/passwd
    1.txt
    2.txt
    tar: Remov  ing leading `/' from member names
    /etc/hosts
    /etc/passwd

     

      不显示删除/

     

    1
    2
    3
    4
    5
    [root@oldboyedu-lnb ~]# tar zcvfP all.tar.gz *.txt /etc/hosts /etc/passwd
    1.txt
    2.txt
    /etc/hosts
    /etc/passwd

     

    示例4: 指定压缩包的位置   

     

    1
    2
    3
    4
    5
    6
    [root@oldboyedu-lnb ~]# tar zcvf /opt/all.tar.gz 1.txt 2.txt
    1.txt
    2.txt
    [root@oldboyedu-lnb ~]# ll /opt/
    total 4
    -rw-r--r-- 1 root root 116 Aug  3 10:57 all.tar.gz

     

       示例5:如何解压 xf

     

    1
    [root@oldboyedu-lnb ~]# tar xf hosts.tar.gz

     

       示例6: 解压到固定的目录   

     

    1
    2
    3
    4
    5
    6
    7
    [root@oldboyedu-lnb ~]# tar xf all.tar.gz -C /opt/
    [root@oldboyedu-lnb ~]# ll /opt/
    total 4
    -rw-r--r-- 1 root root   0 Aug  3 10:51 1.txt
    -rw-r--r-- 1 root root   0 Aug  3 10:51 2.txt
    -rw-r--r-- 1 root root 116 Aug  3 10:57 all.tar.gz
     drwxr-xr-x 2 root root  33 Aug  3 10:59 etc

     

     示例7: 查看压缩包里面的文件或目录名称  tf

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@oldboyedu-lnb ~]# tar tf hosts.tar.gz
    etc/hosts
    [root@oldboyedu-lnb ~]# tar tf test.tar.gz
    1.txt
    2.txt
    [root@oldboyedu-lnb ~]# echo hehe > 1.txt
    [root@oldboyedu-lnb ~]# tar tf test.tar.gz |xargs cat
    hehe
    [root@oldboyedu-lnb ~]# cat 1.txt 2.txt

     

       示例8: 排除压缩某个文件 exclude 排除

     

                     打包当前目录所有的文件   

     

    1
    2
    3
    4
    5
    [root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./*
     ./1.txt
     ./2.txt
     ./etc/
     ./etc/hosts

     

         排除当前目录的1.txt       

     

    1
    2
    3
    4
    5
    6
    7
    8
    [root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude=1.txt
     ./2.txt
     ./etc/
     ./etc/hosts
    [root@oldboyedu-lnb ~]# tar tf exclude.tar.gz
     ./2.txt
     ./etc/
     ./etc/hosts   

     

         排除当前目录的1.txt和2.txt 

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude=1.txt --exclude=2.txt
     ./etc/
     ./etc/hosts
     [root@oldboyedu-lnb ~]# ll
     total 4
      -rw-r--r-- 1 root root  5 Aug  3 11:02 1.txt
      -rw-r--r-- 1 root root  0 Aug  3 10:51 2.txt
      -rw-r--r-- 1 root root  0 Aug  3 11:13 alex.txt
      drwxr-xr-x 2 root root 19 Aug  3 10:56 etc
      -rw-r--r-- 1 root root  0 Aug  3 11:13 oldboy.txt
    [root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude={1,alex}.txt
      ./2.txt
      ./etc/
      ./etc/hosts
      ./oldboy.txt

     

      排除多个文件: 可以把多个文件名称 写入到文本中 排除文本中的文件

     

        第一步: 排除的文件名写入exclude.txt

     

    1
    2
    3
    4
    5
    [root@oldboyedu-lnb ~]# echo -e "1.txt alex.txt oldboy.txt" > exclude.txt
    [root@oldboyedu-lnb ~]# cat exclude.txt
    1.txt
    alex.txt
    oldboy.txt

     

        第二步: 打包排除 --exclude-from=file.txt

     

    1
    2
    3
    4
    5
    6
    [root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude-from=exclude.txt
    ./2.txt
    ./all.tar.gz
     ./etc/
     ./etc/hosts
     ./exclude.txt     

     

          PS:    xargs -n  输出的内容显示n列            

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@oldboyedu-lnb ~]# echo {1..10}|xargs -n3
     1 2 3
     4 5 6
     7 8 9
     10
    [root@oldboyedu-lnb ~]# echo {1..10}|xargs -n4
    1 2 3 4
     5 6 7 8
    9 10

     

    第6章     文件权限

     

       33575026 -rw-r--r-- 1 root root 5 Aug  3 11:02 1.txt

     

       rw-r--r--      # 9位权限位 三位一组

     

       wx            # 三位最大的权限

     

    6.1  前三位      # 属主的权限位  文件的主人 文件的创建者对文件的权限

     

                   rw-

     

                     可读

     

                    可写

     

                  -   没有权限

     

                  最大权限 rwx

     

                  类似于 笔记本(文件) 属于 张三的

     

                  当前的1.txt文件是属于root用户的

     

    6.2  中三位    # 属组的权限位  在组内的成员对我的文件拥有什么权限

     

                 r--

     

                 r   可读

     

                 -    没有权限

     

                 -   没有权限

     

                   类似于 笔记本属于张三 也属于某个组 张三的家庭

     

    6.3  后三位  # 其他用户的权限位 对于张三来说 陌生人拥有什么权限

     

               不是属主 也不在组内的人 对文件的权限

     

                 r--    可读

     

                  -       没有权限

     

                 -       没有权限

     

               类似于 张三不认识的人 也不在组内的成员 对笔记本的权限          

     

               PS:组的权限和其他用户的权限默认相同                     

     

               rwx 相对应的数字来表示权限

     

              r  read----> 4

     

              w  write---> 2

     

              x  write---> 1    

     

           rw-r--r--  # 使用数字来表示权限

     

           每个权限位相加 rw-

     

           把所有位置的数字组合在一起     

     

        r(4)w(2)-(0)===6     # 属主 前三位相加

        r(4)-(0)-(0)===4     # 属组 中三位相加

        r(4)-(0)-(0)===4     # 其他 后三位相加

     

           权限组合在一起 644   # Linux文件默认的权限

     

           目录的权限: 默认的权限755

     

           rwxr-xr-x

     

           rwx=7

     

           r-x=5

     

           r-x=5

     

    6.4  文件的属主

     

           rw-r--r-- 1 root root   5 Aug  3 11:02 1.txt

     

                           ----- ------ 

     

                            1      2

     

      1.文件的属主 文件的主人 文件的创建者

     

    6.5  文件的属组

     

           2.文件的属组 用户组 对文件的权限                        

     

    6.6 练习题:

     

      6.6.1    在oldboy目录下创建lidao1..lidao5 5个目录

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [root@oldboyedu-lnb~]# mkdir  /oldboy/lidao{1..5}
     
    drwxr-xr-x  2 root root    6 Aug  3 15:52 lidao1
     
    drwxr-xr-x  2 root root    6 Aug  3 15:52 lidao2
     
    drwxr-xr-x  2 root root    6 Aug  3 15:52 lidao3
     
    drwxr-xr-x  2 root root    6 Aug  3 15:52 lidao4
     
    drwxr-xr-x  2 root root    6 Aug  3 15:52 lidao5

     

     6.6.2    在oldboy目录下创建alex1.txt aelx2.txt 然后在5个目录下创建2个文件 www.conf bbs.conf

     

    1
    2
    3
    [root@oldboyedu-lnb~]#  touch  /oldboy/alex{1..2}.txt
    -rw-r--r--  1 root root    0 Aug  3 15:57 alex1.txt
    -rw-r--r--  1 root root    0 Aug  3 15:57 alex2.txt

     

    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
    27
    28
    29
    30
    31
    32
    [root@oldboyedu-lnb~]# touch /oldboy/lidao{1..5}/{www,bbs}.conf
    [root@oldboyedu-lnb~]# tree /oldboy/lidao{1..5}
     
    /oldboy/lidao1
     
    ├── bbs.conf
     
    └── www.conf
     
    /oldboy/lidao2
     
    ├── bbs.conf
     
    └── www.conf
     
    /oldboy/lidao3
     
    ├── bbs.conf
     
    └── www.conf
     
    /oldboy/lidao4
     
    ├── bbs.conf
     
    └── www.conf
     
    /oldboy/lidao5
     
    ├── bbs.conf
     
    └── www.conf

     

        6.6.3    使用find的三种方法查找oldboy的一级目录下所有的文件 复制到/tmp下

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    ① find  /oldboy/ –maxdepth 1 -type f |xargs –i cp {} /tmp
     
    ② find  /oldboy/ –maxdepth 1 –type f –exec cp {} /tmp/ ;
     
    cp  `find  /oldboy/ –maxdepth 1 –type f`  /tmp
     
    [root@oldboyedu-lnb~]# tree -L 1 /oldboy
     
    /oldboy
     
    ├── alex1.txt
     
    ├── alex2.txt
     
    ├── lidao1
     
    ├── lidao2
     
    ├── lidao3
     
    ├── lidao4
     
    └── lidao5

     

      6.6.4   使用find的三种方式把oldboy下所有的文件mv 到 /opt目录下

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    ①  find  /oldboy/* –type f |xargs –i mv  {} /opt
     
    ②  find /oldboy/* –type f –exec mv  /opt/ ;
     
    ③  mv `find /oldboy/* -type f`  /opt/
     
    [root@oldboyedu-lnbopt]# ll
     
    total 0
     
    -rw-r--r-- 1 root root 0 Aug  3 15:57 alex1.txt
     
    -rw-r--r-- 1 root root 0 Aug  3 15:57 alex2.txt
     
    -rw-r--r-- 1 root root 0 Aug  3 17:09 bbs.conf
     
    -rw-r--r-- 1 root root 0 Aug  3 17:09 www.conf

     

      6.6.5     使用find的三种方式把 oldboy目录下所有目录删除  把 /opt所有文件删除

     

    1
    2
    3
    ①   find  oldboy/ -type d |xargs –i  rm –rf          find  /opt/ type f | xargs rm -f      
    ②   find  /oldboy/ -type d –exec rm -r {} +;(不报错) find  /opt/ type f –exec rm –f {} ;
    ③  rm -rf `find /oldboy/ -type d`                      rm –rf `find /opt/ type f`

     

    6.7    find命令如何取反

     

    1
    2
    3
    4
    5
    6
    ! EXPR
    -not EXPR
    find命令如何排除某个文件
    -prune
    find  -path “./sk”-prune -o  -name “*.txt” –print

     

     

  • 相关阅读:
    房地产英语 Real estate词汇
    自制Flash FLV视频播放器
    .net反编译工具Reflector下载
    JQUery插件thickbox
    50 Great Photoshop Tutorials for Clever Beginners
    AspNet中你或许不知道的技巧(转)
    常用的设计网站(收藏)
    35 Green and Earthy Photoshop Effects
    使用 ASP.NET 2.0 增强网站的安全性
    asp.net中实现登陆的时候用SSL
  • 原文地址:https://www.cnblogs.com/xzlive/p/13604373.html
Copyright © 2020-2023  润新知