目录与路径

常见处理目录的命令

  • cd:切换目录
  • pwd:显示当前目录
  • mkdir:创建目录
  • rmdir:删除空目录

    目录相关

    递归创建目录/文件
    mkdir [-p] 目录名称 ```shell [root@xxxxxx app]# mkdir -p dir/doc/test [root@xxxxxx app]# tree dir/ dir/ └── doc └── test 2 directories, 0 files

删除目录

[root@xxxxxx app]# cd dir/ [root@xxxxxx dir]# rmdir doc/ rmdir: failed to remove ‘doc/’: Directory not empty

删除空目录

[root@xxxxxx dir]# cd doc/ [root@xxxxxx doc]# rmdir test/ [root@xxxxxx doc]#

  1. <a name="bALJW"></a>
  2. #### 关于执行文件路径的变量:$PATH
  3. A:ls命令完整的文件路径为/bin/ls,但我们确认在任何路径下都能执行ls?<br />Q:因为环境变量PATH所致
  4. 输出环境变量(PATH一定是大写)
  5. ```shell
  6. root@xxxxxx doc]# echo $PATH
  7. /sbin:/bin:/usr/sbin:/usr/bin

PATH注意事项

  • 不同身份用户默认的PTH不同,默认能够随意执行的命令不同(如root、shigl)
  • PATH是可以修改的
  • 使用绝对路径或相对路径直接指定某个命令的文件来执行,会比查PATH来的准确
  • 命令应该要放置到正确的目录下,执行才会方便
  • 本目录(.) 最好不要放到PATH中

    文件与目录管理

    文件与目录相关操作

    文件/目录查看命令

  • ls
    参数与选项

    1. -a:全部文件,连同隐藏文件一起列出 (常用)
    2. -A:全部文件,连同隐藏文件,但不包括.和..两个目录
    3. -d:仅列出目录本身,而不列出目录内的文件数据 (常用)
    4. -f:直接列出结果,而不进行排序(ls默认会按文件名排序)
    5. -h:将文件容量以人类易读的方式(如GBKB等)列出来
    6. -i:列出inode号码
    7. -l:列出详细属性与权限等数据 (常用)
    8. -n:列出UIDGID而非使用者和用户组的名称
    9. -r:将排序结果反向输出
    10. -R:连同子目录内容一起列出来,等于该目录下的所有文件都会显示出来
    11. -S:以文件容量大小排序,而非文件名
    12. -t:以时间排序,而非文件名
    13. --full-time:以完整时间(包含 年/月/日/时/分)模式输出

    文件/目录操作命令

  • cp(复制文件或目录)
    cp [-adfilprsu] 源文件(source) 目标文件(destination)
    cp [options] source1 source2 source3 directory
    参数与选项
    注意:

    1. -a:相当于-dr --preserver=all
    2. -d:若源文件为链接文件的属性,则复制链接文件的属性而非文件本身
    3. -f:为强制(force)的意思,若目标文件已经存在且无法启用,则删除后再尝试一次
    4. -i:若目标文件见已经存在,在覆盖时会先询问操作的进行 (常用)
    5. -l:进行硬链接(hard link)的链接文件建立,而非复制文件本身
    6. -p:连同文件的属性(权限、用户、时间)一起复制过去,而非使用默认属性 (备份常用)
    7. -r:递归复制,用于目录的复制操作 (常用)
    8. -s:复制为符号链接文件(symbolic link),亦称 '快捷方式'文件
    9. -u:destinationsource旧才更新destinationsource,或destination不存在的情况才复制
    10. --preserver=all:除-p的权限相关参数外,还加入SELinux的属性,linksxattr等也复制
    1. 如果源文件是两个以上的文件,则目标文件一定要是 “目录”
    2. 默认情况,cp的源文件和目标文件权限是不同的,目标文件拥有者通常会是命令操作的本身


软连接&硬链接

  1. [root@xxxxxx linux_basic]# ll
  2. total 0
  3. -rw-r--r-- 1 root root 0 Aug 5 19:52 test-shig.py
  4. [root@xxxxxx linux_basic]# cp -l test-shig.py test-william.py
  5. [root@xxxxxx linux_basic]# ll
  6. total 0
  7. -rw-r--r-- 2 root root 0 Aug 5 19:52 test-shig.py
  8. -rw-r--r-- 2 root root 0 Aug 5 19:52 test-william.py
  9. [root@xxxxxx linux_basic]# cp -s test-shig.py test-william001.py
  10. [root@xxxxxx linux_basic]# ll
  11. total 0
  12. -rw-r--r-- 2 root root 0 Aug 5 19:52 test-shig.py
  13. lrwxrwxrwx 1 root root 12 Aug 5 20:15 test-william001.py -> test-shig.py
  14. -rw-r--r-- 2 root root 0 Aug 5 19:52 test-william.py
  • mv(删除文件或目录)
    rm [-fir] 文件或目录
    选项与参数
    注意:直接rm 不带任何参数时,默认有-i选项

    1. -f:force的意思,忽略不存在的文件,也不会出现警告信息
    2. -i:交互模式,删除前会询问使用者是否操作
    3. -r:递归删除,最常用于目录的删除,这是非常危险的选项
  • rm(移动文件与目录,或重命名)
    mv [-fiu] source destination
    mv [options] source1 source2 source3 directory
    选项与参数

    1. -f:force的意思,如果目标文件存在,不会询问直接覆盖
    2. -i:若目标文件已经存在,就会询问是否覆盖
    3. -u:若目标文件已经存在,且source比较新,才会更新

    获取文件名与目录名

  • basename ```shell [root@xxxxxx linux_basic]# ll total 0 -rw-r—r— 1 root root 0 Aug 5 19:52 test-william.py

取到的是文件名

[root@xxxxxx linux_basic]# basename /app/linux_basic/test-william.py test-william.py

  1. - dirname
  2. ```shell
  3. # 取到的是目录名
  4. [root@xxxxxx linux_basic]# dirname /app/linux_basic/test-william.py
  5. /app/linux_basic

直接查看文件内容

  • cat(concatenate)
    cat [-AbEnTv]
    选项与参数
    基本使用

    1. -A:相当于-vET
    2. -b:列出行号,仅针对非空白做行号显示,空白行不标行号
    3. -E:将结尾的$显示出来
    4. -n:打印出行号,连同空行也会有行号,与-b的选项不同
    5. -T:将[tab]案件已^I显示出来
    6. -v:列出一些看不出的特殊字符
    1. [root@cnsz92vl12951 linux_basic]# cat -n test-william.py
    2. 1 # coding=utf-8
    3. 2
    4. 3 print 123
  • tac(反向列示) ```shell [root@xxxxxx linux_basic]# cat test-william.py

    coding=utf-8

print 123 [root@xxxxxx linux_basic]# tac test-william.py print 123

coding=utf-8

  1. - nl(添加行号打印)<br />nl [-bnw] 文件<br />选项与参数 <br />nl及带参数使用 <br />说明:nl cat -n都能对文件内容自动加行号,区别是nl可以将行号做比较多的显示设计,包括位数是否自动补齐0等功能
  2. ```shell
  3. -b:指定行号指定的方式,主要有两种:
  4. -b a:表示无论是否为空行,也同样列出行号
  5. -b t:表示有空行,空的行不要列出行号(默认)
  6. -n:列出行号表示的方法,主要有三种:
  7. -n ln:行号在屏幕最左方显示
  8. -n rn:行号在自己栏最右方显示,且不加0
  9. -n rz:行号在自己栏最右方显示,且加0
  1. [root@xxxxxx linux_basic]# nl test-william.py
  2. 1 # coding=utf-8
  3. 2 print 123
  4. # 空行也列出行号
  5. [root@cnsz92vl12951 linux_basic]# nl -ba test-william.py
  6. 1 # coding=utf-8
  7. 2
  8. 3 print 123
  9. # 行号在屏幕最左方显示
  10. [root@cnsz92vl12951 linux_basic]# nl -n ln test-william.py
  11. 1 # coding=utf-8
  12. 2 print 123
  13. # 行号在自己栏最右方显示,且不加0
  14. [root@cnsz92vl12951 linux_basic]# nl -n rn test-william.py
  15. 1 # coding=utf-8
  16. 2 print 123
  17. # 行号在自己栏最右方显示,且加0
  18. [root@cnsz92vl12951 linux_basic]# nl -n rz test-william.py
  19. 000001 # coding=utf-8
  20. 000002 print 123

翻页查看

数据截取

练习:查看文件第11到20行的数据

  1. # 原文件内容
  2. 10
  3. 11
  4. 12 class SmallRice(SimpleFactory):
  5. 13
  6. 14 def make(self):
  7. 15 return "make small rice"
  8. 16
  9. 17
  10. 18 class Apple(SimpleFactory):
  11. 19
  12. 20 def make(self):
  13. # 查看文件第11到20行的数据的命令
  14. [root@cnsz92vl12951 linux_basic]# head -n 20 factory.py | tail -n 10
  15. class SmallRice(SimpleFactory):
  16. def make(self):
  17. return "make small rice"
  18. class Apple(SimpleFactory):
  19. def make(self):
  20. # 查看文件第11到20行的数据并显示行号
  21. [root@cnsz92vl12951 linux_basic]# cat -n factory.py | head -n 20 | tail -n 10
  22. 11
  23. 12 class SmallRice(SimpleFactory):
  24. 13
  25. 14 def make(self):
  26. 15 return "make small rice"
  27. 16
  28. 17
  29. 18 class Apple(SimpleFactory):
  30. 19
  31. 20 def make(self):

非纯文本文件:od

修改文件时间&创建新文件:touch

  • 修改时间(modification time, mtime)
    文件内容更新时,就会更新这个时间。
  • 状态时间(status time, ctime)
    文件状态改变,就会更新这个时间,比如:权限、属性
  • 读取时间(access time, atime)
    文件内容被读取时,就会更新这个时间,比如cat操作 ```shell [root@cnsz92vl12951 etc]# date; ls -l —full-time man_db.conf ; ls -l —full-time —time=atime man_db.conf ; ls -l —full-time —time=ctime man_db.conf Fri Aug 6 10:02:07 CST 2021 -rw-r—r— 1 root root 5171 2018-10-31 04:26:28.000000000 +0800 man_db.conf # 修改时间 -rw-r—r— 1 root root 5171 2021-08-06 09:53:30.089485561 +0800 man_db.conf # 更新状态时间 -rw-r—r— 1 root root 5171 2021-05-24 20:04:41.941080211 +0800 man_db.conf # 读取时间

[root@cnsz92vl12951 etc]# ll —full-time man_db.conf -rw-r—r— 1 root root 5171 2018-10-31 04:26:28.000000000 +0800 man_db.conf

  1. 说明:默认情况下,ls显示的是该文件的mtime
  2. - touch<br />touch [-acdmt] 文件<br />选项与参数 <br />修改文件时间 <br />新建空文件
  3. ```shell
  4. -a:仅自定义access time
  5. -c:仅修改文件的时间,若文件不存在则不建立新文件
  6. -d:后面接要自定义的日期,也可以使用--date="日期或时间"
  7. -m:仅修改mtime
  8. -t:后面接要自定义的时间,格式为[YYYYMMDDhhmm]
  1. # 查看当前时间
  2. [root@cnsz92vl12951 linux_basic]# date
  3. Fri Aug 6 10:16:40 CST 2021
  4. # 查看文件的更新时间
  5. [root@cnsz92vl12951 linux_basic]# ll --full-time test-william.py
  6. -rw-r--r-- 1 root root 26 2021-08-05 20:47:16.309165985 +0800 test-william.py
  7. # 更新文件的时间为2天前
  8. [root@cnsz92vl12951 linux_basic]# touch -d "2 days ago" test-william.py
  9. [root@cnsz92vl12951 linux_basic]# ll --full-time test-william.py
  10. -rw-r--r-- 1 root root 26 2021-08-04 10:16:04.601836299 +0800 test-william.py
  11. # 更新时间为202108011200
  12. [root@cnsz92vl12951 linux_basic]# touch -t 202108011200 test-william.py
  13. [root@cnsz92vl12951 linux_basic]# ll --full-time test-william.py
  14. -rw-r--r-- 1 root root 26 2021-08-01 12:00:00.000000000 +0800 test-william.py
  1. [root@cnsz92vl12951 linux_basic]# touch william.py
  2. [root@cnsz92vl12951 linux_basic]# ll
  3. total 12
  4. -rw-r--r-- 1 root root 26 Aug 1 12:00 test-william.py
  5. -rw-r--r-- 1 root root 0 Aug 6 10:26 william.py

文件与目录的默认权限与隐藏权限

默认权限

  • 用户创建为文件则默认没有可执行权限,即只有rw这两项,即最大为666,-rw-rw-rw-
  • 用户创建为目录,由于x与是否可进入目录有关,因此所有权限均开放,即最大777,drwxrwxrwx

    文件默认权限:umask

    问题:新增一个文件或目录,默认权限是什么?
    查看umask ```shell [root@cnsz92vl12951 linux_basic]# umask 0022

[root@cnsz92vl12951 linux_basic]# umask -S u=rwx,g=rx,o=rx

  1. **说明:**022表示需**要移除**的权限,**文件移除后为:-rw-r--r--;目录移除后为:rwxr-xr-x**<br />**新建文件&目录的默认权限**
  2. ```shell
  3. [root@cnsz92vl12951 linux_basic]# mkdir dir
  4. [root@cnsz92vl12951 linux_basic]# touch dir.py
  5. [root@cnsz92vl12951 linux_basic]# ll
  6. total 12
  7. drwxr-xr-x 2 root root 6 Aug 6 10:33 dir
  8. -rw-r--r-- 1 root root 0 Aug 6 10:33 dir.py

设置umask

  1. [root@cnsz92vl12951 linux_basic]# umask
  2. 0022
  3. # 设置umask值
  4. [root@cnsz92vl12951 linux_basic]# umask 002
  5. [root@cnsz92vl12951 linux_basic]# umask
  6. 0002
  7. # 此时用户组也具有w权限了
  8. [root@cnsz92vl12951 linux_basic]# touch test-dir.py
  9. [root@cnsz92vl12951 linux_basic]# mkdir test-dir
  10. [root@cnsz92vl12951 linux_basic]# ll
  11. drwxrwxr-x 2 root root 6 Aug 6 10:47 test-dir
  12. -rw-rw-r-- 1 root root 0 Aug 6 10:47 test-dir.py

文件隐藏属性

文件的隐藏属性对系统有很大的帮助,尤其在系统安全上面,需要强调的是,chattr命令只能在ext2、ext3、ext4的linux传统文件系统上面完整生效,其他文件系统可能无法完整生效。

  • chattr(配置文件隐藏属性)
    chattr [+-=] [ASacdistu] 文件/目录名称
    选项与参数:
    注意:设置属性常见的a与i的设置值,而且很多设置值必须要root才能设置 ```shell +:增加某一特殊参数,其他原本存在参数则不动 -:删除某一特殊参数,其他原本存在参数则不动 =:直接设置参数,且仅有后面接的参数 A:当设置A属性后,若存取文件(目录)时,它的存取时间atime将不会被修改,可避免I/O较慢的机器过度的读写磁盘 S:一般文件是非同步写入磁盘的,增加S属性时,当进行任何文件的修改,该修改会同步写入磁盘中

a:当设置a后,这个文件只能增加数据,不能删除也不能修改数据,只有root才能设置这个属性

c:当设置c后,会自动的将文件[压缩],在读取的时候将会自动解压缩,但是在存储的时候,将会先进行压缩后再存储(对大文件似乎挺有用) d:当dump程序被执行时,设置d属性将可使该文件(目录)不会被dump备份 i:可以让一个文件[不能被删除、改名、设置链接也无法写入或新增数据],对系统安全性有相当大的助益,只有root才能设置这个属性 s:当文件设置s属性后,如果文件被删除,它将完全的从硬盘删除,所以如果误删,完全无法恢复 u:与s相反,如果该文件删除了,则数据内容其实还存在磁盘中,可以用来恢复该文件

  1. 重点:如对文件安全要求高,ia参数比较常用
  2. ```shell
  3. [root@cnsz92vl12951 linux_basic]# cd dir/
  4. [root@cnsz92vl12951 dir]# ll
  5. total 0
  6. [root@cnsz92vl12951 dir]# touch attr_test.py
  7. [root@cnsz92vl12951 dir]# chattr +i attr_test.py
  8. [root@cnsz92vl12951 dir]# rm -rf attr_test.py
  9. rm: cannot remove ‘attr_test.py’: Operation not permitted
  10. # 移除文件隐藏属性
  11. [root@cnsz92vl12951 dir]# chattr -i attr_test.py
  12. [root@cnsz92vl12951 dir]# lsattr attr_test.py
  13. ---------------- attr_test.py
  14. [root@cnsz92vl12951 dir]# chattr +a attr_test.py
  15. [root@cnsz92vl12951 dir]# vi attr_test.py
  16. "attr_test.py"
  17. "attr_test.py" E212: Can't open file for writing
  18. Press ENTER or type command to continue
  • lsattr(显示文件隐藏属性)
    lsattr [-adR] 文件或目录
    参数与选项:
    基本使用

    1. -a:将隐藏文件的属性也显示出来
    2. -d:如果接的是目录,仅列出目录本身的属性而非目录内的文件名
    3. -R:连同子目录的数据也一并列出来
    1. [root@cnsz92vl12951 dir]# lsattr -adR attr_test.py
    2. -----a---------- attr_test.py

    文件特殊权限:SUID、SGID、SBIT

  • Set UID (SUID)
    文件权限除了r、w、x三个,看看下面这两个文件
    s与t这两个权限的意义与系统的账号及系统的进程管理较为有关,后面会详细介绍。
    s这个标志会出现在拥有者x权限上时,此时就被称为Set UID,简称为SUID的特殊权限。SUID对文件的限制于功能

    1. [root@cnsz92vl12951 dir]# ls -l /tmp/ ; ls -l /usr/bin/passwd
    2. total 4
    3. -rw-r--r-- 1 root root 26 Aug 9 20:44 osw.hb
    4. drwx------ 3 root root 16 Mar 11 09:35 systemd-private-1828000e965d45a7801bef37bbbee106-chronyd.service-q0dVrX
    5. drwx------ 3 root root 16 Jun 26 11:54 systemd-private-1828000e965d45a7801bef37bbbee106-mariadb.service-eaIxIg
    6. -rwsr-xr-x 1 root root 27856 Apr 1 2020 /usr/bin/passwd
    1. SUID权限仅对二进制程序有效
    2. 执行者对该程序需要有x的可执行权限
    3. 本权限仅在执行该程序过程中有效(run-time)
    4. 执行者将具有该程序拥有者的权限
  • Set GID (SGID)
    当s标志在文件拥有者的x权限上时,称为SGID。如下:
    SGID有如下功能:

    1. [root@cnsz92vl12951 dir]# ls -l /usr/bin/locate
    2. -rwx--s--x 1 root slocate 40520 Apr 11 2018 /usr/bin/locate
    1. SGID对二进制程序有用
    2. 执行者对该程序需要有x的可执行权限
    3. 执行者将具有该程序用户组的权限
  • Sticky Bit (SBIT)
    当t标志在文件其他组的x权限上时,称为SBIT,SBIT目前只针对目录有效,对文件已经没有效果。SBIT对目录的作用是:
    1. 当用户对于此目录具有w、x权限,即具有写入的权限
    2. 当用户在目录下建立文件或目录时,仅有自己与root才有权力删除该文件


举例:用户甲对目录A具有用户组或其他人权限,并拥有该目录w的权限,这表示甲用户对该目录内任何人建立的目录或文件均可进行删除、更名、移动等操作。不过,如果将A目录加上SBIT的权限选项后,则甲只能针对自己建立的文件或目录进行删除、更名、移动等操作,而无法删除他人的文件

  • SUID/SGID/SBIT权限设置
    4:SUID
    2:SGID
    1:SBIT
    chmod [num]755 filename

    1. [root@cnsz92vl12951 dir]# lsattr attr_test.py
    2. ---------------- attr_test.py
    3. [root@cnsz92vl12951 dir]# chmod 4755 attr_test.py
    4. [root@cnsz92vl12951 dir]# ls -l attr_test.py
    5. -rwsr-xr-x 1 root root 0 Aug 9 20:35 attr_test.py

    观察文件类型file

    file命令可以用来查询文件的基本信息,如属于ASCII或者是数据文件或二进制文件,有没有用到动态链接库等

    1. [root@cnsz92vl12951 dir]# touch file_test.py
    2. [root@cnsz92vl12951 dir]# vi file_test.py
    3. [root@cnsz92vl12951 dir]# file file_test.py
    4. file_test.py: ASCII text

    命令与文件的查找

    脚本文件的查找

  • which(查找【执行文件】)
    which [-a] command
    选项或参数:

    1. -a:将所有由PATH目录中可以找到的命令均列出,而不止第一个被找到的命令

    基本使用 ``` [root@cnsz92vl12951 dir]# which ifconfig /sbin/ifconfig

[root@cnsz92vl12951 dir]# which which alias which=’alias | /usr/bin/which —tty-only —read-alias —show-dot —show-tilde’ /bin/alias /usr/bin/which

[root@cnsz92vl12951 dir]# which history /usr/bin/which: no history in (/sbin:/bin:/usr/sbin:/usr/bin)

[root@cnsz92vl12951 dir]# history —help bash: history: —: invalid option history: usage: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg…]

  1. 注意:which命令是根据【PATH】环境变量所规范的路径,查找执行文件的文件名,重点是找出执行文件而已,且which后面接的是完整的文件名。最后的history例子中,因为historybash内置的命令,whichPATH内的目录找不到,所有出现这种情况。后面将介绍type命令(bash相关)
  2. <a name="66a25d72"></a>
  3. #### 文件的查找
  4. - whereis(特定目录中查找文件)<br />选项和参数: <br />基本使用

-l:可以列出whereis会查询的几个主要目录 -b:只找binary格式文件 -m:只找说明文件manual路径下的文件 -s:只找source源文件 -u:查找不在上述三个项目当中的其他特殊文件

  1. ```
  2. ipconfig:[root@cnsz92vl12951 dir]# whereis ifconfig
  3. ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
  • locate(输入文件名称)
    locate [-ir] keypword
    选项和参数:

    1. -i:忽略大小写的差异
    2. -c:不输出文件名,仅计算找到的文件数量
    3. -l:仅输出几行的意思,如:-l 5 输出5
    4. -S:输出locate所使用的数据文件的相关信息,包括该数据库记录的文件/目录数量等
    5. -r:后面可接正则表达式
  • 基本使用 ```shell [root@cnsz92vl12951 dir]# locate passwd -l 5 /etc/passwd /etc/passwd- /etc/pam.d/passwd /etc/security/opasswd /usr/bin/gpasswd

[root@cnsz92vl12951 dir]# locate -S Database /var/lib/mlocate/mlocate.db: 37,657 directories 264,760 files 19,600,837 bytes in file names 7,847,931 bytes used to store database

  1. locate命令的限制:因为是由数据库查找,数据库的建立默认是每天执行一次,如果新建的文件,数据库还没来得及更新,则会找不到。<br />手动更新locate数据库(updatedb命令,一般需要数分钟)
  2. ```shell
  3. [root@cnsz92vl12951 linux_basic]# ll
  4. total 16
  5. drwxr-xr-x 2 root root 6 Aug 6 10:33 dir
  6. -rw-r--r-- 1 root root 0 Aug 6 10:33 dir.py
  7. -rw-r--r-- 1 root root 6720 Aug 6 09:08 factory.py
  8. d--x--x--x 2 william william 54 Aug 5 19:58 test
  9. drwxrwxr-x 2 root root 6 Aug 6 10:47 test-dir
  10. -rw-rw-r-- 1 root root 0 Aug 6 10:47 test-dir.py
  11. -rw-r--r-- 1 root root 26 Aug 1 12:00 test-william.py
  12. -rw-r--r-- 1 root root 15 Aug 9 19:40 william.py
  13. [root@cnsz92vl12951 linux_basic]# touch shigl.py
  14. [root@cnsz92vl12951 linux_basic]# locate shigl.py
  15. [root@cnsz92vl12951 linux_basic]# updatedb
  16. [root@cnsz92vl12951 linux_basic]# locate shigl.py
  17. /app/linux_basic/shigl.py
  • find
    find [PATH] [option] [option]

选项与参数:
与时间有关的选项参数:-atime、-ctime与-mtime,以-mtime为例:

  1. -mtime n:n为证书,意为在n天之前的[一天内]被修改过内容的文件
  2. -mtime +n:列出在n天之前(不含n本身)被修改过内容的文件
  3. -mtime -n:列出n天之内(含n本身)被修改过内容的文件
  4. -newer file: file为一个存在的文件,列出此file还要新的文件
  5. n值详细说明:
  6. +4:代表大于等于5天前的文件
  7. -4:代表小于等于4天内的文件
  8. 4:代表4-5那一天的文件

基本使用

  1. # 列出过去24小时指定目录下修改过内容的文件
  2. [root@cnsz92vl12951 linux_basic]# find ./ -mtime 0
  3. ./
  4. ./dir
  5. ./dir/attr_test.py
  6. ./dir/file_test.py
  7. ./william.py
  8. ./shigl.py

使用者或用户组名称有关的参数:

  1. -uid n:n为数字,这个参数使用者的账号ID,也是UID
  2. -gid n:n为数字,这个参数用户组的名称ID,也是GID
  3. -user name:name为使用者账号名称
  4. -group name:name为用户组名称
  5. -nouser:查找文件的拥有者不在/etc/passwd
  6. -nogroup:查找文件的拥有用户组不存在/etc/group

基本使用

  1. [root@cnsz92vl12951 linux_basic]# find ./ -user william
  2. ./test

与文件权限和名称有关的参数:

  1. -name filename:查找名称为filename的文件
  2. -size [+-] SIZE:查找比SIZE大/小的文件,例如:-size +50k
  3. -type TYPE:查找文件的类型为TYPE的,类型一般有:正规文件(f)、设备文件(bc)、 目录(d)、链接文件(l)、socket(s)、FIFO(p)等属性
  4. -perm mode:查找文件权限[刚好等于]mode权限的文件 如:-perm 0755
  5. -perm -mode:查找文件权限[全部囊括]mode权限的文件 如:-perm -0755
  6. -perm /mode:查找文件权限[包含任一]mode权限的文件 如:-perm /0755

基本使用

  1. # find 查找目录 -name 文件名
  2. [root@cnsz92vl12951 shigl]# find / -name passwd
  3. /etc/passwd
  4. /etc/pam.d/passwd
  5. /usr/bin/passwd
  6. /usr/share/bash-completion/completions/passwd
  7. [root@cnsz92vl12951 linux_basic]# find -type d
  8. .
  9. ./test
  10. ./dir
  11. ./test-dir
  12. [root@cnsz92vl12951 linux_basic]# find -perm -0755
  13. .
  14. ./dir
  15. ./test-dir
  16. [root@cnsz92vl12951 linux_basic]# find -perm -755
  17. .
  18. ./dir
  19. ./test-dir
  20. [root@cnsz92vl12951 linux_basic]# find -perm /755
  21. .
  22. ./test
  23. ./test/test_01.py
  24. ./test/test_02.py
  25. ./test/test.py
  26. ./test-william.py
  27. ./factory.py
  28. ./dir
  29. ./dir.py
  30. ./test-dir.py
  31. ./test-dir
  32. ./william.py
  33. ./shigl.py

find配合-exec参数执行

  1. [root@cnsz92vl12951 linux_basic]# find /usr/bin/ /usr/sbin -perm /6000
  2. /usr/bin/wall
  3. /usr/bin/chage
  4. /usr/bin/gpasswd
  5. [root@cnsz92vl12951 linux_basic]# find /usr/bin/ /usr/sbin -perm /6000 -exec ls -l {} \;
  6. -r-xr-sr-x. 1 root tty 15344 Jun 10 2014 /usr/bin/wall
  7. -rwsr-xr-x 1 root root 73888 Aug 9 2019 /usr/bin/chage
  8. -rwsr-xr-x 1 root root 78408 Aug 9 2019 /usr/bin/gpasswd