第1章   文件属性

1.1   文件的属性

  1.1.1    查看文件的详细属性

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

      ls 参数选项:

-t      # 按照时间排序

-r      # 逆序排序

按照时间进行逆序排序

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

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

PS:tr

  -c  取反

-d  删除

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

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

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

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

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

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

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

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

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

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

什么是硬链接:

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

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

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

6 文件的属主

当前的文件属于哪个用户

7 文件的属组

   当前的文件属于哪个组

       Access     访问时间

       Modify     修改时间

       Change     改变时间

8  文件名称

第2章    正确与错误重定向

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

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

2.1  标准正确输出重定向

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

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

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

1>  ======= >

1>> ====== >>

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

  1. [root@oldboyedu-lnb ~]# ls 1.sh
  2.  
  3. 1.sh
  4.  
  5. [root@oldboyedu-lnb ~]# ls 1.sh > ok.txt
  6.  
  7. [root@oldboyedu-lnb ~]# cat ok.txt
  8.  
  9. 1.sh
  10.  
  11. [root@oldboyedu-lnb ~]# ls 1.sh >> ok.txt
  12.  
  13. [root@oldboyedu-lnb ~]# cat ok.txt
  14.  
  15. 1.sh
  16.  
  17. 1.sh
  18.  
  19. [root@oldboyedu-lnb ~]# ls 1.sh 1> ok.txt
  20.  
  21. [root@oldboyedu-lnb ~]# cat ok.txt
  22.  
  23. 1.sh
  24.  
  25. [root@oldboyedu-lnb ~]# ls 1.sh 1>> ok.txt
  26.  
  27. [root@oldboyedu-lnb ~]# cat ok.txt
  28.  
  29. 1.sh
  30.  
  31. 1.sh

2.2  标准错误输出重定向

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

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

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

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

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

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

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

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

2.3  接收正确和错误的结果

       方法1

  1. [root@oldboyedu-lnb ~]# cat error.txt
  2.  
  3. [root@oldboyedu-lnb ~]# ls aa.txt >>ok.txt 2>>error.txt
  4.  
  5. [root@oldboyedu-lnb ~]# cat error.txt
  6.  
  7. ls: cannot access aa.txt: No such file or directory
  8.  
  9. [root@oldboyedu-lnb ~]# ls 1.txt >>ok.txt 2>>error.txt
  10.  
  11. [root@oldboyedu-lnb ~]# cat ok.txt
  12.  
  13. ls: cannot access aa.txt: No such file or directory
  14.  
  15. ls: cannot access a.txt: No such file or directory
  16.  
  17. 1.txt
  18.  
  19. 1.txt
  20.  
  21. [root@oldboyedu-lnb ~]# > ok.txt
  22.  
  23. [root@oldboyedu-lnb ~]# ls 1.txt >>ok.txt 2>>error.txt
  24.  
  25. [root@oldboyedu-lnb ~]# cat ok.txt
  26.  
  27. 1.txt
  28.  
  29. [root@oldboyedu-lnb ~]# cat error.txt
  30.  
  31. ls: cannot access aa.txt: No such file or directory

     方法2:

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

       方法3:

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

2.4  扩展

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

块设备   硬件设备 磁盘 光驱 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

网络套接字

管道文件

第3章    文件相关的命令

3.1  which  # 查看命令的全路径

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

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

3.3     file       # 查看文件的类型

  1. [root@oldboyedu-lnb ~]# file 1.txt
  2. 1.txt: ASCII text
  3. [root@oldboyedu-lnb ~]# file 3
  4. 3: directory
  5. [root@oldboyedu-lnb ~]# file all.tar.gz
  6. all.tar.gz: gzip compressed data, from Unix, last modified: Fri Jul 31 10:42:15 2020
  7. [root@oldboyedu-lnb ~]# file /etc/rc.local
  8. /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. `b' for 512-byte blocks (this is the default if no suffix is used)
  2.  
  3. `c' for bytes (推荐)
  4.  
  5. `w' for two-byte words
  6.  
  7. `k' for Kilobytes (units of 1024 bytes) (推荐)
  8.  
  9. `M' for Megabytes (units of 1048576 bytes) (推荐)
  10.  
  11. `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. [root@oldboyedu-lnb 3]# find ./ -type f
  2.  
  3. ./1.txt
  4.  
  5. ./2.txt
  6.  
  7. ./3.txt
  8.  
  9. ./4.txt
  10.  
  11. ./5.txt
  12.  
  13. ./6.txt
  14.  
  15. ./7.txt
  16.  
  17. ./8.txt
  18.  
  19. ./9.txt
  20.  
  21. ./10.txt

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

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

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

      -name    #按照名称精确查找

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

    -name   #按照名称模糊查找

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

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

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

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

-maxdepth  深度等级

  1. [root@oldboyedu-lnb 3]# touch stu{1..3}/{bbs,blog,www}.conf
  2.  
  3. [root@oldboyedu-lnb 3]# find ./ -maxdepth 1 -type f
  4. ./1.txt
  5.  
  6. ./2.txt
  7.  
  8. ./3.txt
  9.  
  10. [root@oldboyedu-lnb 3]# find ./ -maxdepth 2 -type f
  11. ./1.txt
  12. ./2.txt
  13. ./3.txt
  14. ./stu1/bbs.conf
  15. ./stu1/blog.conf
  16. ./stu1/www.conf
  17. ./stu2/bbs.conf
  18. ./stu2/blog.conf
  19. ./stu2/www.conf
  20. ./stu3/bbs.conf
  21. ./stu3/blog.conf
  22. ./stu3/www.conf
  23. [root@oldboyedu-lnb 3]# find stu{1..3}/ -maxdepth 1 -type f
  24. stu1/bbs.conf
  25. stu1/blog.conf
  26. stu1/www.conf
  27. stu2/bbs.conf
  28. stu2/blog.conf
  29. stu2/www.conf
  30. stu3/bbs.conf
  31. stu3/blog.conf
  32. stu3/www.conf

    3.4.3.5    按照文件的大小查找

-size   -k -M -G

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

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

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

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

4.2  find命令补充

PS:注意中文语法的问题

 find可以查找文件目录

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

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

4.3 xargs

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

语法格式:

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

示例:

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

4.4  find结合xargs   find不支持别名

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

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

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

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

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

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

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

  1. [root@oldboyedu-lnb 3]# grep -R oldboy ./*
  2. ./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. [root@oldboyedu-lnb 3]# find ./ -type f -name 2.txt -exec ls -l {} \;
  2. -rw-r--r-- 1 root root 0 Aug 3 09:21 ./2.txt

  4.5.2    find的结果交给rm 使用

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

  4.5.3   find结果交给mv 使用

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

  4.5.4    把find的结果交给cp使用

  1. [root@oldboyedu-lnb 3]# find ./ -type f -name 4.txt -exec cp {} /tmp/ \;
  2. [root@oldboyedu-lnb 3]# ll 4.txt /tmp/4.txt
  3. -rw-r--r-- 1 root root 0 Aug 3 09:21 4.txt
  4. -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. [root@oldboyedu-lnb 3]# ll `find ./ -type f`
  2. -rw-r--r-- 1 root root 0 Aug 3 09:21 ./4.txt

  4.6.2    把find 结果交给rm

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

  4.6.3    把find的结果交给mv

  1. [root@oldboyedu-lnb 3]# mv `find ./ -type f -name 5.txt` /tmp/5.bak
  2. [root@oldboyedu-lnb 3]# ll 5.txt /tmp/5.bak
  3. ls: cannot access 5.txt: No such file or directory
  4. -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. [root@oldboyedu-lnb 3]# ls -l;mkdir oldboy;touch oldboy/test.txt
  2. total 0
  3. -rw-r--r-- 1 root root 0 Aug 3 09:21 6.txt
  4. -rw-r--r-- 1 root root 0 Aug 3 09:21 7.txt
  5. -rw-r--r-- 1 root root 0 Aug 3 09:21 8.txt
  6. -rw-r--r-- 1 root root 0 Aug 3 09:21 9.txt
  7. -rw-r--r-- 1 root root 0 Aug 3 08:41 hehe.
  8. drwxr-xr-x 2 root root 20 Aug 3 09:32 test
  9. [root@oldboyedu-lnb 3]# ll oldboy/
  10. total 0
  11. -rw-r--r-- 1 root root 0 Aug 3 10:10 test.txt

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

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

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

         示例1:

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

示例2:

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

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

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

示例:

  1. [root@oldboyedu-lnb 3]# cd alex|| mkdir alex
  2. -bash: cd: alex: No such file or directory
  3. [root@oldboyedu-lnb 3]# ll
  4. total 0
  5. 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. [root@oldboyedu-lnb ~]# tar zcvf hosts.tar.gz /etc/hosts
  2. tar: Removing leading `/' from member names
  3. /etc/hosts

示例2: 不显示压缩过程

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

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

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

  不显示删除/

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

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

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

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

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

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

打包当前目录所有的文件

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

     排除当前目录的1.txt  

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

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

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

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

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

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

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

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

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

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

r   可读

w   可写

-   没有权限

最大权限 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. [root@oldboyedu-lnb~]# mkdir /oldboy/lidao{1..5}
  2.  
  3. drwxr-xr-x 2 root root 6 Aug 3 15:52 lidao1
  4.  
  5. drwxr-xr-x 2 root root 6 Aug 3 15:52 lidao2
  6.  
  7. drwxr-xr-x 2 root root 6 Aug 3 15:52 lidao3
  8.  
  9. drwxr-xr-x 2 root root 6 Aug 3 15:52 lidao4
  10.  
  11. 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. [root@oldboyedu-lnb~]# touch /oldboy/alex{1..2}.txt
  2. -rw-r--r-- 1 root root 0 Aug 3 15:57 alex1.txt
  3. -rw-r--r-- 1 root root 0 Aug 3 15:57 alex2.txt
  1. [root@oldboyedu-lnb~]# touch /oldboy/lidao{1..5}/{www,bbs}.conf
  2. [root@oldboyedu-lnb~]# tree /oldboy/lidao{1..5}
  3.  
  4. /oldboy/lidao1
  5.  
  6. ├── bbs.conf
  7.  
  8. └── www.conf
  9.  
  10. /oldboy/lidao2
  11.  
  12. ├── bbs.conf
  13.  
  14. └── www.conf
  15.  
  16. /oldboy/lidao3
  17.  
  18. ├── bbs.conf
  19.  
  20. └── www.conf
  21.  
  22. /oldboy/lidao4
  23.  
  24. ├── bbs.conf
  25.  
  26. └── www.conf
  27.  
  28. /oldboy/lidao5
  29.  
  30. ├── bbs.conf
  31.  
  32. └── www.conf

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

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

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

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

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

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

6.7    find命令如何取反


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

文件属性及find命令总结的更多相关文章

  1. (47)LINUX应用编程和网络编程之二Linux文件属性

    Linux下的文件系统为树形结构,入口为/ 树形结构下的文件目录: 无论哪个版本的Linux系统,都有这些目录,这些目录应该是标准的.各个Linux发行版本会存在一些小小的差异,但总体来说,还是大体差 ...

  2. 爆肝整理:Linux常用命令,建议收藏!

    目录管理命令:mkdir.rmdir mkdir命令 rmdir命令 文件管理命令:cp.mv.rm cp命令 mv命令 rm命令 文件查看命令:cat.tac.head.tail.more.less ...

  3. 在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world

    一.题目 编写一个内核模块,在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world.内核版本要求2.6.18 二.实验环境 物理主机:w ...

  4. 观【史上最牛linux视频教程】整理笔记,持续更新……

    //文件处理命令 命令格式:命令 [-参数] [参数] 例:ls -la /etc -a等于--all //目录处理命令:ls 英文原意:list 所在路径:/bin/ls 语法:ls 选项[-ald ...

  5. Linux入门篇(一)——文件

    这一系列的Linux入门都是本人在<鸟哥的Linux私房菜>的基础上总结的基本内容,主要是记录下自己的学习过程,也方便大家简要的了解 Linux Distribution是Ubuntu而不 ...

  6. Linux入门篇(二)——文件

    这一系列的Linux入门都是本人在<鸟哥的Linux私房菜>的基础上总结的基本内容,主要是记录下自己的学习过程,也方便大家简要的了解 Linux Distribution是Ubuntu而不 ...

  7. [国嵌笔记][004][Linux快速体验]

    Linux文件系统 bin目录:可执行的程序 boot目录:与Linux启动相关的文件 dev目录:设备以文件的方式存放 etc目录:配置文件 home目录:用户文件 lib目录:与库相关的文件 ro ...

  8. Linux下文件权限(一)用户ID和用户组ID

    最近在读<unix环境高级编程>,看到文件权限这里比较糊涂,主要设计多个用户ID和用户组ID,包括下面两个: (1)实际用户ID和实际用户组ID:这一部分表示我们究竟是谁.这两个字段在登录 ...

  9. Linux 文件/文件夹无法删除问题解决方案

    最近我们的服务器被黑客攻击,然后有些文件的属性被修改,导致我们无法删除病毒文件,同时采用 root 用户也无法删除,现在把解决方案记录下来. 普通删除 如果文件是当前用户的,那么使用 rm 命令就可以 ...

随机推荐

  1. E CF R 85 div2 1334E. Divisor Paths

    LINK:Divisor Paths 考试的时候已经想到结论了 可是质因数分解想法错了 导致自闭. 一张图 一共有D个节点 每个节点x会向y连边 当且仅当y|x,x/y是一个质数. 设f(d)表示d的 ...

  2. mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)

    前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...

  3. 【HEOI2015】公约数数列 题解(分块)

    前言:毒瘤数据结构题,半个下午都在搞它了…… --------------------------- 题目链接 题目大意:给定一个长度为$n$的序列,有两种操作:1.把$a_x$的值改成$y$.2.求 ...

  4. Nginx MogileFS 配置

    配置好MogileFS, 见mogilefs的安装与配置随笔 下载nginx.1.10.3.tar.gz, nginx_mogilefs_module.1.0.4.tar.gz 编译安装 将连个tar ...

  5. C#LeetCode刷题之#884-两句话中的不常见单词(Uncommon Words from Two Sentences)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3816 访问. 给定两个句子 A 和 B . (句子是一串由空格分 ...

  6. K8s集群verification error" while trying to verify candidate authority certificate "kubernetes"

    问题内容 because of "crypto/rsa: verification error" while trying to verify candidate authorit ...

  7. Python 为什么要在 18 年前引入布尔类型?且与 C、C++ 和 Java 都不同?

    花下猫语:在上一篇<Python 为什么能支持任意的真值判断? >文章中,我们分析了 Python 在真值判断时的底层实现,可以看出 Python 在对待布尔值时,采用了比较宽泛的态度.官 ...

  8. 阙乃祯:网龙在教育领域Cassandra的使用

    网龙是一家游戏公司,以前是做网络在线游戏的,现在开始慢慢转型,开始从事在线教育. 在线教育已经做了5-6年时间了.为什么我们会用Cassandra呢?那我们就来介绍今天的议题. 首先介绍我们的业务背景 ...

  9. Web框架的原理详情

    Web框架的原理 Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. socket服务 ...

  10. java class类和object类

    Class类 介绍 Java的Class类是java反射机制的基础,通过Class类我们可以获得关于一个类的相关信息 Java.lang.Class是一个比较特殊的类,它用于封装被装入到JVM中的类( ...