学习笔记:CentOS 7学习之十二:查找命令

1.which-whereis-locate-grep-find查找命令

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

1.1 which

[root@linuxprobe ~]# which cd
/bin/cd

1.2 whereis

[yangjie@linuxprobe ~]$ whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz /usr/share/man/mann/cd.n.gz

1.3 locate

locate命令相当于find -name,是它的另外一种写法,但是这个要比find搜索快的多,因为find命令查找的是具体的目录文件,而locate它搜索的是数据库 /var/lib/mlocate/mlocate.db,这个数据库中存有本地所有的文件信息,这个数据库是linux自动创建并每天更新维护的。相关的配置信息在/etc/update.conf,查看定时任务在/etc/cron.daily/mlocate

touch /opt/yangjie.txt
locate yangjie.txt #发现文件不存在
updatedb #如果对当天的文件进行查找,需要手动更新数据库updatedb
locate yangjie.txt #文件能够正常查询到

1.4 grep

作用:过滤,它能够使用正则表达式来搜索文本,并把结果打印出来;

参数 作用
-v 取反(或者叫过滤)
-i 忽略大小写
^# 以#开头
#$ 以#结尾
^$ 空行
-n 对过滤的内容加上行号
l 或者的意思

举例:

1)在passwd中查找以P开头的行

[root@linuxprobe ~]# grep ^p /etc/passwd
pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
polkitd:x:998:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
pcp:x:387:387:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

2)在passwd中查找以bash结尾的行

[root@linuxprobe ~]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
yangjie:x:1000:1000:yangjie:/home/yangjie:/bin/bash
oracle:x:1001:1001::/home/oracle:/bin/bash

3)在passwd中查找以p开头的行,并显示行号

[root@linuxprobe ~]# grep -n ^p /etc/passwd
15:pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
18:polkitd:x:998:997:User for polkitd:/:/sbin/nologin
33:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
35:pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
36:pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin
49:pcp:x:387:387:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
55:postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

4)使用grep查找sshd进程,同时过滤掉grep进程

[root@linuxprobe ~]# ps -ef|grep sshd|grep -v grep
root   1515  1  0 20:24 ?00:00:00 /usr/sbin/sshd -D

5)在passwd文件中搜索有nologin或者root字符的行

[root@linuxprobe ~]#grep "nologin\|root" /etc/passwd    #“\|”为转义字符既|
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
……

[root@linuxprobe ~]# grep "nologin\|root" /etc/passwd|wc -l
54

[root@linuxprobe ~]# egrep "nologin|root" /etc/passwd |wc -l  #使用egrep命令时可以不适用转义字符
54

1.5 find命令

格式:find pathname -option 【-print】

  • 参数:
参数 说明
pathname find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
  • 选项:
选项 说明
-name 按照文件名查找文件。 “名称”
-perm 按照文件权限来查找文件。 666、777等
-prune 使用这一选项可以使find命令不在当前指定的目录中查找(即排除),如果同时使用-depth选项,那么-prune选项将被find命令忽略
-depth 在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找
-user 按照文件属主来查找文件
-group 按照文件所属的组来查找文件
-mtime -n/+n 按照文件的更改日期来查找文件:
1、-n表示文件的更改时间距现在n天以内;
2、+n表示文件更改时间距现在n天之前
-type 查找某一类型的文件:
b-块设备文件;
d-目录:
c-字符设备文件;
p-管道文件;
l-符号链接文件;
f-不同文件;
-size 查找符合指定大小的文件
-exec 对匹配的文件执行改参数所给出的其他linux命令,相应命令的形式为' 命令 {} ;,注意{}和;之间的空格,{}代表查到的内容;

举例:

1)查找当前目录下的所有.txt文件

[root@linuxprobe ~]# find ./ -name "*.txt"
./.targetcli/history.txt
./.targetcli/log.txt
./2.txt
./1.txt
./file.txt

2)按照文件的更改时间或者访问时间等查找文件

如果希望按照更改时间来查找文件,可以使用atime,mtime或者ctime选项:

mtime:文件最后一次修改时间;

atime:文件最后一次访问时间;

ctime:文件的最后一次变化时间,也就是修改时间

a. 查看/root/目录下5天以内修改的文件:

[root@linuxprobe ~]# find /root/ -mtime -5
/root/
/root/.cache/abrt
/root/.cache/abrt/lastnotification
/root/.bash_history
/root/.viminfo
/root/2.txt
/root/.xauthqRxrFo
/root/p.sh
/root/1.txt
/root/file.txt

3)-exec对查找内容执行相应的命令

命令格式:

find 空格 path 空格 -option 空格 -exec 空格 要执行的命令 空格 {} 空格 说明
find 空格 ./ 空格 -name "*.txt" 空格 -exec 空格 ls -l 空格 {} 空格 ; 查找当前目录下的.txt文件,并用ls -l显示详细信息
find 空额 /home/oracle/ 空格 -atime -10 空格 -exec 空格 mv 空格 {} /opt/ 空格 ; 查找/home/yangjie/目录下的所有在10天内访问过的文件,然后将他们移动到/opt/目录下
find 空格 ./ 空格 -name "*.txt" 空格 -exec 空格 tar -zcvf data.tar.gz 空格 {} 空格 ; 查找当前目录下的所有.txt文件,然后打包成data.tar.gz

依次如下:

1、find ./ -name "*.txt" -exec ls -l {} /;

[root@linuxprobe ~]# find ./ -name "*.txt" -exec ls -l {} \; 
-rw-r--r--. 1 root root 0 9月  20 2018 ./.targetcli/history.txt
-rw-r--r--. 1 root root 3978 9月  20 2018 ./.targetcli/log.txt
-rw-r--r--. 1 root root 0 5月   3 23:32 ./1.txt
-rw-r--r--. 1 root root 0 5月   3 23:32 ./3.txt
-rw-r--r--. 1 root root 0 5月   3 23:33 ./2.txt

2、find /home/oracle/ -atime -10 -exec mv {} /opt ;

[root@linuxprobe ~]# find /home/oracle/ -atime -10 -exec mv {} /opt/ \;
[root@linuxprobe ~]# ll /opt/
总用量 4
drwxr-xr-x.  3 rootroot  18 4月  22 21:51 boot
drwxr-xr-x.  3 rootroot  22 9月  12 2018 ORCLfmap
drwxr-xr-x.  2 rootroot   6 9月   7 2017 rh
drwx------. 23 yangjie yangjie 4096 5月   3 20:24 yangjie
-rw-r--r--.  1 rootroot   0 5月   3 20:30 yangjie.txt

3、find ./ -name "*.txt" -exec tar -zcvf data.tar.gz {} ;

[root@linuxprobe ~]# find ./ -name "*.txt" -exec tar -zcvf data.tar.gz {} \;
./.targetcli/history.txt
./.targetcli/log.txt
./1.txt
./3.txt
./2.txt
[root@linuxprobe ~]# ll
总用量 16
-rw-r--r--. 1 root root0 5月   3 23:32 1.txt
-rw-r--r--. 1 root root0 5月   3 23:33 2.txt
-rw-r--r--. 1 root root0 5月   3 23:32 3.txt
-rw-------. 1 root root 2350 9月   6 2018 anaconda-ks.cfg
-rw-r--r--. 1 root root  111 5月   3 23:55 data.tar.gz
-rw-r--r--. 1 root root 2398 9月   6 2018 initial-setup-ks.cfg
drwxr-xr-x. 2 root root6 9月   6 2018 perl5
-rwxr-xr-x. 1 root root  105 5月   3 15:32 p.sh

4) 使用xargs -i命令来代替-exec

举例: find ./ -name "*.txt" | xargs -i cp {} /opt #将当前目录下所有的.txt文件复制到/opt/下

find ./ -name "*.txt" |xargs -i cp {} /opt/

[root@linuxprobe ~]# ll /opt
总用量 8
-rw-r--r--.  1 rootroot   0 5月   4 00:04 1.txt
-rw-r--r--.  1 rootroot   0 5月   4 00:04 2.txt
-rw-r--r--.  1 rootroot   0 5月   4 00:04 3.txt
drwxr-xr-x.  3 rootroot  18 4月  22 21:51 boot
-rw-r--r--.  1 rootroot   0 5月   4 00:04 history.txt
-rw-r--r--.  1 rootroot3978 5月   4 00:04 log.txt
drwxr-xr-x.  3 rootroot  22 9月  12 2018 ORCLfmap
drwxr-xr-x.  2 rootroot   6 9月   7 2017 rh
drwx------. 23 yangjie yangjie 4096 5月   3 20:24 yangjie
-rw-r--r--.  1 rootroot   0 5月   3 20:30 yangjie.txt

5)查找多个类型文件

比较符使用:

-a: and 并且

查找/etc/下面大于40k且小于50k的文件

[root@linuxprobe ~]# find /etc/ -size +40k -a -size -50k 
/etc/selinux/targeted/contexts/files/file_contexts.homedirs.bin
/etc/selinux/targeted/active/modules/100/sysadm/hll
/etc/gconf/gconf.xml.defaults/%gconf-tree-ml.xml
/etc/brltty/fr-abrege.ctb
[root@linuxprobe ~]# find /etc/ -size +40k -a -size -50k -exec ls -al {} \;
-rw-r--r--. 1 root root 44725 9月   6 2018 /etc/selinux/targeted/contexts/files/file_contexts.homedirs.bin
-rw-------. 1 root root 47464 9月   6 2018 /etc/selinux/targeted/active/modules/100/sysadm/hll
-rw-r--r--. 1 root root 43112 9月   6 2018 /etc/gconf/gconf.xml.defaults/%gconf-tree-ml.xml
-rw-r--r--. 1 root root 49286 4月  11 2018 /etc/brltty/fr-abrege.ctb

-o: or 或者

在当前目录下寻找所有的.sh文件或者.pdf文件

[root@linuxprobe ~]# touch a.pdf
[root@linuxprobe ~]# find ./ -name "*.sh" -o -name "*.pdf"
./p.sh
./a.pdf

+:大于

-: 小于

查找/etc/下大于20k的文件

[root@linuxprobe ~]# find /etc/ -size +200k
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
/etc/udev/hwdb.bin
/etc/services
/etc/ssh/moduli
/etc/selinux/targeted/contexts/files/file_contexts
/etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/policy/policy.31
/etc/selinux/targeted/active/file_contexts
/etc/selinux/targeted/active/policy.kern
/etc/selinux/targeted/active/policy.linked
/etc/gconf/schemas/ekiga.schemas
/etc/brltty/ko.ctb
/etc/brltty/zh-tw-ucb.ctb
/etc/brltty/zh-tw.ctb
/etc/mstflint/ca-bundle.crt

-----



---END---


---2019-5-4 0:25:45---


猜你喜欢

转载自www.cnblogs.com/yj411511/p/10807210.html