美文网首页CentOS 7
centos7文件的查找命令

centos7文件的查找命令

作者: 亦德 | 来源:发表于2018-01-12 17:40 被阅读0次

    文件的查找目前使用的主要有以下几个命令:

    命令 说明
    which 查看可执行文件的位置
    whereis 查看可执行文件的位置及相关文件
    locate 配合数据库缓存,快速查看文件位置
    grep 过滤匹配,它是一个文件搜索工具
    find 查找相关文件

    1 which

    which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

    [root@virtue ~]# which cd
    /usr/bin/cd
    [root@virtue ~]# type cd
    cd 是 shell 内嵌
    [root@virtue ~]# which squid
    /usr/bin/which: no squid in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
    

    *centos7中并没有出现内嵌命令无法查找的情况

    2 whereis

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。
    whereis命令会查找一个记录系统内文件信息的数据库文件,所以速度会很快。缺点是数据库文件并不是实时更新,默认情况一星期更新一次,因此如果最近删除的文件或者创建的文件是有可能错误的显示的。为了防止这种情况,可以手动的强制更新数据库文件。

    命令参数
    -b   定位可执行文件。
    -m   定位帮助文件。
    -s   定位源代码文件。
    -u   搜索默认路径下除可执行文件、源代码文件、帮助文件以外的其它文件。
    -B   指定搜索可执行文件的路径。
    -M   指定搜索帮助文件的路径。
    -S   指定搜索源代码文件的路径。
    
    普通查找
    [root@virtue ~]# whereis svn
    svn:
    [root@virtue ~]# whereis git
    git: /usr/bin/git /usr/share/man/man1/git.1.gz
    
    查找二进制文件
    [root@virtue ~]# whereis -b git
    git: /usr/bin/git
    
    查找帮助文件
    [root@virtue ~]# whereis -m git
    git: /usr/share/man/man1/git.1.gz
    

    3 locate

    locate可以快速的找到文件的位置,因为locate查找的是数据库的文件来确定文件的位置,而并不是深入各个文件系统查找。一般情况下,数据库文件通过corntab自动更新,通常每天更新一次。

    命令参数
    命令 说明
    -e 将排除在寻找的范围之外。
    -1 如果是 1 则启动安全模式。无法查看权限不足的文件。该模式会降低速度,因为需要实际的去档案系统中获得权限资料。
    -f 将特定的档案系统排除在外。
    -q 安静模式,不会显示任何错误讯息。
    -n 至多显示 n个输出。
    -r 使用正规运算式做寻找的条件。
    -o 指定资料库存的名称。
    -d 指定资料库的路径
    普通查找

    查找内容: /etc下说有以sh开头的文件

    [root@virtue ~]# locate /etc/sh
    /etc/shadow
    /etc/shadow-
    /etc/shells
    
    手动更新数据库
    [root@virtue ~]# updatedb 
    

    4 grep

    grep命令是用来过滤文件内容的命令,它能够使用正则表达式来搜索文本,并把结果打印出来

    命令参数
    命令 说明
    v 取反
    -i 忽略大小写
    ^# 以#开头
    #$ 以#结尾
    ^$ 空行
    -n 对过滤的内容加上行号
    | 或者的意思
    普通过滤
    [root@virtue ~]# ps aux | grep sshd | grep -v grep
    root      3175  0.0  0.0 145720  5364 ?        Ss   11:03   0:03 sshd: root@pts/0
    root     24841  0.0  0.0 105872  4080 ?        Ss    2017   0:09 /usr/sbin/sshd -D
    
    过滤内容添加行号
    [root@virtue ~]# grep -n root /etc/passwd
    1:root:x:0:0:root:/root:/bin/bash
    10:operator:x:11:0:operator:/root:/sbin/nologin
    
    多重条件过滤内容
    [root@virtue ~]# grep "nologin\|root" /etc/passwd | wc -l
    18
    [root@virtue ~]# egrep "nologin|root" /etc/passwd | wc -l
    18
    

    5 find

    Linux下find命令在目录结构中搜索文件,并执行指定的操作。即使系统中含有网络文件系统( NFS),只你具有相应的权限,find命令在该文件系统中同样有效。 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。

    命令参数
    命令参数 说明
    pathname find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
    -print find命令将匹配的文件输出到标准输出。
    -print0 与xargs配套使用,以“\0”做为定界符。
    -exec find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。
    -ok 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
    命令选项
    命令选项 说明
    -name 按照文件名查找文件。
    -perm 按照文件权限来查找文件。
    -prune 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。
    -depth 在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
    -user 按照文件属主来查找文件。
    -nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
    -group 按照文件所属的组来查找文件。
    -nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
    -mtime -n/+n 按照文件的更改时间来查找文件,-n表示文件更改时间距现在n天以内,+n表示文件更改时间距现在n天以前。
    -type 查找某一类型的文件,诸如:b - 块设备文件/d - 目录/c - 字符设备文件/p - 管道文件/l - 符号链接文件/f - 普通文件
    -newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。
    -size n [c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
    -fstype 查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
    -mount 在查找文件时不跨越文件系统mount点。
    -follow 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
    -cpio 对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
    关于访问时间的参数具体如下所示:
    命令选项 说明
    -amin n 查找系统中最后N分钟访问的文件
    -atime n 查找系统中最后n*24小时访问的文件
    -cmin n 查找系统中最后N分钟被改变文件状态的文件
    -ctime n 查找系统中最后n*24小时被改变文件状态的文件
    -mmin n 查找系统中最后N分钟被改变文件数据的文件
    -mtime n 查找系统中最后n*24小时被改变文件数据的文件
    find命令应用实例
    # 按名称查找
    [root@virtue ~]# find . -name "*.asm"
    ./Python-3.6.1/Modules/_decimal/libmpdec/vcdiv64.asm
    ./Python-3.6.1/Modules/_ctypes/libffi_msvc/win64.asm
    ./hello.asm
    # 按时间进行查找
    [root@virtue log]# find . -mtime +100
    ./ppp
    ./audit
    [root@virtue log]# find . -mtime -2
    ./lastlog
    ./wtmp
    ./audit/audit.log
    ./yum.log
    ./cron
    ./messages
    ./secure
    # 按照权限查找
    [root@virtue log]# find /root -perm 777
    /root/Python-3.6.1/build/temp.linux-x86_64-3.6/libffi/include/ffitarget.h
    /root/Python-3.6.1/build/temp.linux-x86_64-3.6/libffi/include/ffi_common.h
    /root/.vim/bundle/sparkup/TextMate/Sparkup.tmbundle/Support/sparkup.py
    /root/.vim/bundle/sparkup/ftplugin
    /root/.vim/bundle/sparkup/vim/ftplugin/html/sparkup.py
    /root/.vim/bundle/sparkup/vim/ftplugin/htmldjango
    /root/.vim/bundle/sparkup/vim/ftplugin/smarty
    /root/.vim/bundle/sparkup/vim/ftplugin/xml
    # 按照用户/属组查找
    [root@virtue tsa]# find . -user peter
    ./12.txt
    ./1.txt
    ./backup.sh
    ./sendmail.py
    [root@virtue tsa]# find . -group peter
    ./12.txt
    ./1.txt
    ./backup.sh
    ./sendmail.py
    [root@virtue tsa]# userdel -r peter 
    [root@virtue tsa]# find . -nouser
    ./12.txt
    ./1.txt
    ./backup.sh
    ./sendmail.py
    [root@virtue tsa]# find . -nogroup
    ./12.txt
    ./1.txt
    ./backup.sh
    ./sendmail.py
    # 按照文件类型查找
    [root@virtue tsa]# find . -type d 
    .
    ./a12
    ./a39
    [root@virtue tsa]# find . -type f
    ./12.txt
    ./1.txt
    ./backup.sh
    ./sendmail.py
    ./a12/12.txt
    ./a12/summary.md
    ./a39/detail.html
    ./a39/style.css
    ./a39/script.js
    # 查找后指定命令
    [root@virtue tsa]# find . -nogroup -ls
    36783493    4 -rw-rw-r--   1 1001     1001           26 1月 11 16:27 ./12.txt
    36783494    4 -rw-rw-r--   1 1001     1001            9 1月 11 16:26 ./1.txt
    36783495    4 -rw-rw-r--   1 1001     1001           26 1月 11 16:26 ./backup.sh
    36780031    0 -rw-rw-r--   1 1001     1001            0 1月 11 16:24 ./sendmail.py
    [root@virtue tsa]# find . -nogroup -ls
    36783493    4 -rw-rw-r--   1 1001     1001           26 1月 11 16:27 ./12.txt
    36783494    4 -rw-rw-r--   1 1001     1001            9 1月 11 16:26 ./1.txt
    36783495    4 -rw-rw-r--   1 1001     1001           26 1月 11 16:26 ./backup.sh
    36780031    0 -rw-rw-r--   1 1001     1001            0 1月 11 16:24 ./sendmail.py
    [root@virtue tsa]# find . -name "*.py" -delete
    [root@virtue tsa]# ll
    总用量 12
    -rw-rw-r--. 1 1001 1001 26 1月  11 16:27 12.txt
    -rw-rw-r--. 1 1001 1001  9 1月  11 16:26 1.txt
    drwxr-xr-x. 2 root root 36 1月  11 16:34 a12
    drwxr-xr-x. 2 root root 56 1月  11 16:35 a39
    -rw-rw-r--. 1 1001 1001 26 1月  11 16:26 backup.sh
    # 查找后exec完成指定操作
    [root@virtue tsa]# find . -type f -exec ls -l {} \;
    -rw-rw-r--. 1 1001 1001 26 1月  11 16:27 ./12.txt
    -rw-rw-r--. 1 1001 1001 9 1月  11 16:26 ./1.txt
    -rw-rw-r--. 1 1001 1001 26 1月  11 16:26 ./backup.sh
    -rw-r--r--. 1 root root 0 1月  11 16:34 ./a12/12.txt
    -rw-r--r--. 1 root root 0 1月  11 16:34 ./a12/summary.md
    -rw-r--r--. 1 root root 0 1月  11 16:35 ./a39/detail.html
    -rw-r--r--. 1 root root 0 1月  11 16:35 ./a39/style.css
    -rw-r--r--. 1 root root 0 1月  11 16:35 ./a39/script.js
    # 使用exec删除查找到的文件(慎重:删除时并不会进行确认操作)
    [root@virtue tsa]# find . -name "*.txt"  -exec rm {} \;
    [root@virtue tsa]# find . -type f -exec ls -l {} \;
    -rw-rw-r--. 1 1001 1001 26 1月  11 16:26 ./backup.sh
    -rw-r--r--. 1 root root 0 1月  11 16:34 ./a12/summary.md
    -rw-r--r--. 1 root root 0 1月  11 16:35 ./a39/detail.html
    -rw-r--r--. 1 root root 0 1月  11 16:35 ./a39/style.css
    -rw-r--r--. 1 root root 0 1月  11 16:35 ./a39/script.js
    # 安全的删除方式
    [root@virtue tsa]# find . -name "s*" 
    ./a12/summary.md
    ./a39/style.css
    ./a39/script.js
    [root@virtue tsa]# find . -name "s*" -ok rm {} \; 
    < rm ... ./a12/summary.md > ? y
    < rm ... ./a39/style.css > ? y
    < rm ... ./a39/script.js > ? 
    [root@virtue tsa]# find . -name "s*" 
    ./a39/script.js
    # 多次执行exec
    [root@virtue tsa]# find /etc/ -name passwd -exec grep root {} \; -exec ls -l {} \;
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    -rw-r--r--. 1 root root 1085 1月  11 16:33 /etc/passwd
    
    find和xargs配合使用应用实例
    # 普通的配合使用
    [root@virtue tsa]# find . -name "*.txt" | xargs ls -l
    -rw-r--r--. 1 root root 0 1月  12 16:01 ./a.txt
    -rw-r--r--. 1 root root 0 1月  12 16:01 ./b.txt
    -rw-r--r--. 1 root root 0 1月  12 16:01 ./c.txt
    -rw-r--r--. 1 root root 0 1月  12 16:01 ./d.txt
    -rw-r--r--. 1 root root 0 1月  12 16:01 ./e.txt
    -rw-r--r--. 1 root root 0 1月  12 16:01 ./f.txt
    # 配合使用打包
    [root@virtue tsa]# find . -name "*.txt" -print0 | xargs -0 tar cf find.tar 
    [root@virtue tsa]# tar tf find.tar 
    ./a.txt
    ./b.txt
    ./c.txt
    ./d.txt
    ./e.txt
    ./f.txt
    
    find逻辑运算符
    命令 说明
    -a 并且
    -o 或者
    + 超过
    - 低于
    # find运算符应用实例
    [root@virtue tsa]# find . -name "a*" -a -type d 
    ./a12
    ./a39
    [root@virtue tsa]# find /etc/ -size +20k -a -size -50k | wc -l
    27
    

    相关文章

      网友评论

        本文标题:centos7文件的查找命令

        本文链接:https://www.haomeiwen.com/subject/vmjznxtx.html