#第五章:Linux文件类型及查找命令实践

编程

1、文件属性

  • 文件属性是指:

    • 文件的大小,创建时间,类型,权限,属组等。

    [root@oldboyedu ~]# ls -lih

    total 4.0K

    33574978 -rw-------. 1 root root 1.7K Apr 4 18:09 anaconda-ks.cfg

    [root@oldboyedu ~]# ls -lih /etc/

    total 1.1M

    51000206 drwxr-xr-x. 3 root root 101 Apr 4 18:06 abrt

    16881475 -rw-r--r--. 1 root root 16 Apr 4 18:08 adjtime

    16777784 -rw-r--r--. 1 root root 1.5K Jun 7 2013 aliases

    16777285 -rw-r--r--. 1 root root 12K Apr 4 18:17 aliases.db

    50334171 drwxr-xr-x. 2 root root 261 Apr 4 18:22 alternatives

    16881620 -rw-------. 1 root root 541 Aug 9 2019 anacrontab

    17167203 -rw-r--r--. 1 root root 55 Aug 8 2019 asound.conf

    51150791 drwxr-x---. 3 root root 43 Apr 4 18:06 audisp

    17528264 drwxr-x---. 3 root root 83 Apr 4 18:17 audit

    50331775 drwxr-xr-x. 2 root root 79 Apr 4 18:23 bash_completion.d

    16777785 -rw-r--r--. 1 root root 2.8K Oct 31 2018 bashrc

    34060666 drwxr-xr-x. 2 root root 6 Mar 18 07:46 binfmt.d

    16777318 -rw-r--r--. 1 root root 37 Sep 5 2019 centos-release

    16777319 -rw-r--r--. 1 root root 51 Sep 5 2019 centos-release-upstream

    35647 drwxr-xr-x. 2 root root 6 Aug 4 2017 chkconfig.d

    539069 drwxr-xr-x. 2 root root 36 Apr 4 18:20 cron.d

    17167190 drwxr-xr-x. 2 root root 42 Apr 4 18:06 cron.daily

    16881621 -rw-------. 1 root root 0 Aug 9 2019 cron.deny

  • 文件的属性总共10列

    16881621 -rw-------.  1 root root    0 Aug  9  2019 cron.deny

    1 2 3 4 5 6 7 8 9 10

    • 第一列

      • 是文件的唯一标识,也称之为 inode(节点)号

    • 第二列

      • 第一个字符

        • 是指文件类型

          • - 是代表文件类型为普通文件 (用f表示)

          • d 是代表文件类型为目录 (用d表示)

            [root@oldboyedu ~]# ls -ld /etc/

            drwxr-xr-x. 79 root root 8192 Apr 9 17:51 /etc/

            [root@oldboyedu ~]# ls -lp /etc/

            total 1076

            drwxr-xr-x. 3 root root 101 Apr 4 18:06 abrt/ #以斜线结尾的都是目录

            -rw-r--r--. 1 root root 16 Apr 4 18:08 adjtime

            -rw-r--r--. 1 root root 1518 Jun 7 2013 aliases

            -rw-r--r--. 1 root root 12288 Apr 4 18:17 aliases.db

            drwxr-xr-x. 2 root root 261 Apr 4 18:22 alternatives/

            -rw-------. 1 root root 541 Aug 9 2019 anacrontab

            -rw-r--r--. 1 root root 55 Aug 8 2019 asound.conf

            drwxr-x---. 3 root root 43 Apr 4 18:06 audisp/

            [root@oldboyedu ~]# ls -F /etc/

            abrt/ gnupg/ motd sasl2/

            adjtime GREP_COLORS mtab@ securetty

            aliases groff/ my.cnf security/

            #@指的是连接文件

          • l 是代表文件类型为连接文件(用l表示)

            [root@oldboyedu ~]# ls -l /etc/rc.local 

            lrwxrwxrwx. 1 root root 13 Apr 4 18:22 /etc/rc.local -> rc.d/rc.local

          • c是代表文件类型为字符设备 (用c表示)

            [root@oldboyedu ~]# ls -l /dev/ttyS2

            crw-rw----. 1 root dialout 4, 66 Apr 9 17:51 /dev/ttyS2

          • b是代表文件类型为连接文件(用b表示)

            [root@oldboyedu ~]# ls -l /dev/ | grep "^d"

            drwxr-xr-x. 2 root root 140 Apr 9 17:51 block

            drwxr-xr-x. 2 root root 80 Apr 9 17:51 bsg

            drwxr-xr-x. 3 root root 60 Apr 9 17:51 bus

            drwxr-xr-x. 2 root root 2980 Apr 9 17:51 char

            drwxr-xr-x. 3 root root 60 Apr 9 17:51 cpu

            drwxr-xr-x. 5 root root 100 Apr 9 17:51 disk

            drwxr-xr-x. 2 root root 80 Apr 9 17:51 dri

            drwxr-xr-x. 2 root root 0 Apr 9 17:51 hugepages

            drwxr-xr-x. 4 root root 300 Apr 9 17:51 input

            drwxr-xr-x. 2 root root 60 Apr 9 17:51 mapper

            drwxrwxrwt. 2 root root 40 Apr 9 17:51 mqueue

            drwxr-xr-x. 2 root root 60 Apr 9 17:51 net

            drwxr-xr-x. 2 root root 0 Apr 9 17:51 pts

            drwxr-xr-x. 2 root root 60 Apr 9 17:51 raw

            drwxrwxrwt. 2 root root 40 Apr 9 17:51 shm

            drwxr-xr-x. 3 root root 200 Apr 9 17:51 snd

            drwxr-xr-x. 2 root root 60 Apr 9 17:51 vfio

          • s是代表文件类型为连接文件(用s表示)

            • .sock文件也是一类特殊的文件,这类文件通常用在网络之间,进行数据连接,如:我们可以启动一个程序来监听客户端的请求,客户端可以通过套接字来进行数据通信。
            • 进程之间通信的文件

            [root@oldboyedu ~]# ls -l /dev/ | grep "^s"

            srw-rw-rw-. 1 root root 0 Apr 9 17:51 log

2、查命令所在的路径

  • which

    [root@oldboyedu ~]# which ls

    alias ls="ls --color=auto"

    /usr/bin/ls

    [root@oldboyedu ~]# which cp

    alias cp="cp -i"

    /usr/bin/cp

    [root@oldboyedu ~]# which ip

    /usr/sbin/ip

    [root@oldboyedu ~]# which cd

    /usr/bin/cd

3、系统变量(系统查找命令的位置)

  • $PATH 超重点

[root@oldboyedu ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

#系统默认从上面路径查找,以:分割,每分割一次为一个路径

#示例一

[root@oldboyedu ~]# cat /usr/bin/cc

echo $1

[root@oldboyedu ~]# chmod +x /usr/bin/cc

[root@oldboyedu ~]# cc sun

sun

[root@oldboyedu ~]# which cc

/usr/bin/cc

4、显示命令及其相关文件全路径

  • whereis

    [root@oldboyedu ~]# whereis ls

    ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

    [root@oldboyedu ~]# whereis cp

    cp: /usr/bin/cp /usr/share/man/man1/cp.1.gz

    • -b 显示二进制命令文件路径

      [root@oldboyedu ~]# whereis -b cp

      cp: /usr/bin/cp

5、快速定位文件路径

  • locate

    • 查找文件路径,是从数据库查询

    [root@oldboyedu ~]# yum provides locate  #查看命令所需要的rpm包

    [root@oldboyedu ~]# yum install mlocate -y

    [root@oldboyedu ~]# updatedb #更新库

    [root@oldboyedu ~]# locate ls

6、查找目录下的文件

  • find

    • 查找原理:磁盘遍历 速度慢

    [root@oldboyedu ~]# touch oldboy.txt

    [root@oldboyedu ~]# find ~ -name "oldboy.txt"

    /root/oldboy.txt

    • 模糊查找

    [root@oldboyedu ~]# find ~ -name "oldboy*"

    /root/oldboy.txt

    [root@oldboyedu ~]# find /etc/sysconfig/ -name "ifcfg*"

    /etc/sysconfig/network-scripts/ifcfg-lo

    /etc/sysconfig/network-scripts/ifcfg-eth0

    • -type 按文件类型查找

    [root@oldboyedu ~]# find /etc/sysconfig/ -type f -name "ifcfg-eth0"   #f文件

    /etc/sysconfig/network-scripts/ifcfg-eth0

    [root@oldboyedu ~]# find /etc/ -type d  -name "sysconfig"	#目录

    /etc/sysconfig

    [root@oldboyedu ~]# find /etc/ -type d -name "sys*"

    /etc/selinux/targeted/active/modules/100/sysadm

    /etc/selinux/targeted/active/modules/100/sysadm_secadm

    /etc/selinux/targeted/active/modules/100/sysnetwork

    /etc/selinux/targeted/active/modules/100/sysstat

    /etc/selinux/targeted/active/modules/100/systemd

    /etc/systemd

    /etc/systemd/system

    /etc/systemd/system/system-update.target.wants

    /etc/systemd/system/sysinit.target.wants

    /etc/dbus-1/system.d

    /etc/sysctl.d

    /etc/sysconfig

    /etc/xdg/systemd

    /etc/NetworkManager/system-connections

    /etc/latrace.d/headers/sysdeps

    • !取反

      [root@oldboyedu ~]# find /etc/sysconfig/network-scripts/ -type f ! -name "ifcfg-eth0"

      /etc/sysconfig/network-scripts/ifcfg-lo

      /etc/sysconfig/network-scripts/ifdown-bnep

      /etc/sysconfig/network-scripts/ifdown-eth

      /etc/sysconfig/network-scripts/ifdown-ippp

      /etc/sysconfig/network-scripts/ifdown-ipv6

      /etc/sysconfig/network-scripts/ifdown-post

      /etc/sysconfig/network-scripts/ifdown-ppp

      /etc/sysconfig/network-scripts/ifdown-routes

      /etc/sysconfig/network-scripts/ifdown-sit

      /etc/sysconfig/network-scripts/ifdown-tunnel

      /etc/sysconfig/network-scripts/ifup-aliases

      /etc/sysconfig/network-scripts/ifup-bnep

      /etc/sysconfig/network-scripts/ifup-eth

      /etc/sysconfig/network-scripts/ifup-ippp

      /etc/sysconfig/network-scripts/ifup-ipv6

      /etc/sysconfig/network-scripts/ifup-plip

      /etc/sysconfig/network-scripts/ifup-plusb

      /etc/sysconfig/network-scripts/ifup-post

      /etc/sysconfig/network-scripts/ifup-ppp

      /etc/sysconfig/network-scripts/ifup-routes

      /etc/sysconfig/network-scripts/ifup-sit

      /etc/sysconfig/network-scripts/ifup-tunnel

      /etc/sysconfig/network-scripts/ifup-wireless

      /etc/sysconfig/network-scripts/init.ipv6-global

      /etc/sysconfig/network-scripts/network-functions

      /etc/sysconfig/network-scripts/network-functions-ipv6

      /etc/sysconfig/network-scripts/ifdown-Team

      /etc/sysconfig/network-scripts/ifdown-TeamPort

      /etc/sysconfig/network-scripts/ifup-Team

      /etc/sysconfig/network-scripts/ifup-TeamPort

    • -exec 对查找的动作在处理

      • {} 把前面查找到的结果放到这里
      • 注意使用这个命令时{}和;中间必须有空格
      • 代表转义,这里是把;转换成原本的意思

      [root@oldboyedu ~]# find ~ -type f -name "oldboy*"

      /root/oldboy.txt

      [root@oldboyedu ~]# find ~ -type f -name "oldboy*" -exec rm -f {} ;

      [root@oldboyedu ~]# ls

      anaconda-ks.cfg

    • -mtime 按修改时间查找

      • +n 几天之前
      • n 几天
      • -n 几天之后

      [root@oldboyedu ~]# find ~ -type f -mtime +3  #三天之前

      /root/.bash_logout

      /root/.bash_profile

      /root/.bashrc

      /root/.cshrc

      /root/.tcshrc

      /root/anaconda-ks.cfg

      /root/.bash_history

      [root@oldboyedu ~]# find ~ -type f -mtime +1 #一天之前

      /root/.bash_logout

      /root/.bash_profile

      /root/.bashrc

      /root/.cshrc

      /root/.tcshrc

      /root/anaconda-ks.cfg

      /root/.bash_history

      [root@oldboyedu ~]# find ~ -type f -mtime 3 #第三天

      [root@oldboyedu ~]# find ~ -type f -mtime -3 #三天之后

      /root/.viminfo

7、分组(默认空格)

  • xargs

    • -n 指定几个为一组

    [root@oldboyedu ~]# echo {1..10} > text.txt

    [root@oldboyedu ~]# xargs -n 3 < text.txt

    1 2 3

    4 5 6

    7 8 9

    10

    [root@oldboyedu ~]# xargs -n 5 < text.txt

    1 2 3 4 5

    6 7 8 9 10

    [root@oldboyedu ~]# xargs -n 1 < text.txt

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    [root@oldboyedu ~]# xargs < text.txt

    1 2 3 4 5 6 7 8 9 10

    • -d 指定分隔符

    [root@oldboyedu ~]# xargs -n 1 < text.txt > text1.txt

    [root@oldboyedu ~]# cat text.txt

    1 2 3 4 5 6 7 8 9 10

    [root@oldboyedu ~]# xargs -d "

    " < text1.txt

    1 2 3 4 5 6 7 8 9 10

    [root@oldboyedu ~]# xargs -d "

    " -n 1 < text1.txt

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    [root@oldboyedu ~]# xargs -d "

    " -n 3 < text1.txt

    1 2 3

    4 5 6

    7 8 9

    10

    [root@oldboyedu ~]# xargs < text1.txt 

    1|2|3|4|5|6|7|8|9|10

    [root@oldboyedu ~]# xargs -d "|" < text1.txt

    1 2 3 4 5 6 7 8 9 10

    • | 管道符

      • 把管道的左边的结果赋给右边执行

    [root@oldboyedu ~]#  find /etc/sysconfig/network-scripts/ -type f  -name "ifcfg-eth0" | xargs ls -l 

    -rw-r--r--. 1 root root 362 Apr 4 18:08 /etc/sysconfig/network-scripts/ifcfg-eth0

    • 特殊 -i 把{}当做前面只查找的结果

      [root@oldboyedu ~]#  find /etc/sysconfig/network-scripts/ -type f  -name "ifcfg-eth0" | xargs  cp {}  /opt/

      cp: target ‘/etc/sysconfig/network-scripts/ifcfg-eth0’ is not a directory

      [root@oldboyedu ~]# find /etc/sysconfig/network-scripts/ -type f -name "ifcfg-eth0" | xargs -i cp {} /opt/

      [root@oldboyedu ~]# ll /opt/

      total 4

      -rw-r--r--. 1 root root 362 Apr 9 19:00 ifcfg-eth0

      [root@oldboyedu ~]# find /etc/sysconfig/network-scripts/ -type f -name "ifcfg-eth0" | xargs -i mv {} /opt/

      [root@oldboyedu ~]# ll /opt/

      total 4

      -rw-r--r--. 1 root root 362 Apr 4 18:08 ifcfg-eth0

8、显示系统时间和日期

  • date

    • -s 设置系统时间

      [root@oldboyedu ~]# date 

      Thu Apr 9 19:01:57 CST 2020

      [root@oldboyedu ~]# date -s "2020/01/01 00:00:00"

      Wed Jan 1 00:00:00 CST 2020

      [root@oldboyedu ~]# date

      Wed Jan 1 00:00:03 CST 2020

      [root@oldboyedu ~]# clock -w #计入到主板里

      [root@localhost ~]# date +%F  #年月日

      2020-01-01

      [root@localhost ~]# date +%Y #年

      2020

      [root@localhost ~]# date +%y #年后两位

      20

      [root@localhost ~]# date +%m #月

      01

      [root@localhost ~]# date +%d #日

      01

      [root@localhost ~]# date +%H #时

      00

      [root@localhost ~]# date +%M #分

      02

      [root@localhost ~]# date +%S #秒

      15

9、打包压缩

  • tar

    • -z 压缩
    • -c 创建
    • -v 详情
    • -f 文件
    • -t 查看
    • -C 指定位置解压
    • -x 解压
    • -h 压缩链接文件

[root@oldboyedu /]# tar -zcvf data.tar.gz ./data

./data/

./data/1.txt

./data/2.txt

./data/3.txt

./data/4.txt

./data/5.txt

./data/6.txt

./data/7.txt

./data/8.txt

./data/9.txt

./data/10.txt

./data/a/

./data/b/

[root@oldboyedu /]# tar -zcvf /opt/data.tar.gz ./data

./data/

./data/1.txt

./data/2.txt

./data/3.txt

./data/4.txt

./data/5.txt

./data/6.txt

./data/7.txt

./data/8.txt

./data/9.txt

./data/10.txt

./data/a/

./data/b/

[root@oldboyedu /]# tar -tf /opt/data.tar.gz

./data/

./data/1.txt

./data/2.txt

./data/3.txt

./data/4.txt

./data/5.txt

./data/6.txt

./data/7.txt

./data/8.txt

./data/9.txt

./data/10.txt

./data/a/

./data/b/

以上是 #第五章:Linux文件类型及查找命令实践 的全部内容, 来源链接: utcz.com/z/515289.html

回到顶部