目录
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 cdcd: /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.txtlocate yangjie.txt #发现文件不存在updatedb #如果对当天的文件进行查找,需要手动更新数据库updatedblocate yangjie.txt #文件能够正常查询到
1.4 grep
作用:过滤,它能够使用正则表达式来搜索文本,并把结果打印出来;
参数 | 作用 |
---|---|
-v | 取反(或者叫过滤) |
-i | 忽略大小写 |
^# | 以#开头 |
#$ | 以#结尾 |
^$ | 空行 |
-n | 对过滤的内容加上行号 |
l | 或者的意思 |
举例:
1)在passwd中查找以P开头的行
[root@linuxprobe ~]# grep ^p /etc/passwdpegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologinpolkitd:x:998:997:User for polkitd:/:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinpulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologinpkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologinpcp:x:387:387:Performance Co-Pilot:/var/lib/pcp:/sbin/nologinpostgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
2)在passwd中查找以bash结尾的行
[root@linuxprobe ~]# grep bash$ /etc/passwdroot:x:0:0:root:/root:/bin/bashamandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bashpostgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bashyangjie:x:1000:1000:yangjie:/home/yangjie:/bin/bashoracle:x:1001:1001::/home/oracle:/bin/bash
3)在passwd中查找以p开头的行,并显示行号
[root@linuxprobe ~]# grep -n ^p /etc/passwd15:pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin18:polkitd:x:998:997:User for polkitd:/:/sbin/nologin33:postfix:x:89:89::/var/spool/postfix:/sbin/nologin35:pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin36:pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin49:pcp:x:387:387:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin55:postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
4)使用grep查找sshd进程,同时过滤掉grep进程
[root@linuxprobe ~]# ps -ef|grep sshd|grep -v greproot 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/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin……[root@linuxprobe ~]# grep "nologin\|root" /etc/passwd|wc -l54[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 |
依次如下:
a、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
b、find /home/oracle/ -atime -10 -exec mv {} /opt ;
[root@linuxprobe ~]# find /home/oracle/ -atime -10 -exec mv {} /opt/ \;[root@linuxprobe ~]# ll /opt/总用量 4drwxr-xr-x. 3 rootroot 18 4月 22 21:51 bootdrwxr-xr-x. 3 rootroot 22 9月 12 2018 ORCLfmapdrwxr-xr-x. 2 rootroot 6 9月 7 2017 rhdrwx------. 23 yangjie yangjie 4096 5月 3 20:24 yangjie-rw-r--r--. 1 rootroot 0 5月 3 20:30 yangjie.txt
c、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.cfgdrwxr-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.txtdrwxr-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.txtdrwxr-xr-x. 3 rootroot 22 9月 12 2018 ORCLfmapdrwxr-xr-x. 2 rootroot 6 9月 7 2017 rhdrwx------. 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
6)按权限查找:-perm
a、在/bin目录下搜素权限等于775且大于8M的文件或者目录
[root@linuxprobe ~]# find /bin/ -size +8M -a -perm 755 -exec ls -al {} \;-rwxr-xr-x. 1 root root 10527312 6月 10 2014 /bin/virtuoso-t-rwxr-xr-x. 1 root root 8885408 6月 10 2014 /bin/doxygen
b、在/bin目录下搜索权限至少有644的文件或者目录
[root@linuxprobe ~]# find /bin/ -perm 644|wc -l #/bin下面权限为644的文件或者目录只有2个2[root@linuxprobe ~]# find /bin/ -perm -644|wc -l #/bin下面权限至少为644的文件或者目录有3046个3046
c、在系统中查看权限至少为777的目录或文件
[root@linuxprobe ~]# mkdir ccc[root@linuxprobe ~]# chmod 777 ccc[root@linuxprobe ~]# mkdir test[root@linuxprobe ~]# chmod 1777 test[root@linuxprobe ~]# ll -d testdrwxrwxrwt. 2 root root 6 5月 6 20:14 test[root@linuxprobe ~]# touch a.sh[root@linuxprobe ~]# chmod 4777 a.sh[root@linuxprobe ~]# ll -d testdrwxrwxrwt. 2 root root 6 5月 6 20:14 test[root@linuxprobe ~]# ll -d a.sh-rwsrwxrwx. 1 root root 0 5月 6 20:14 a.sh[root@linuxprobe ~]# find /root/ -perm 777/root/ccc[root@linuxprobe ~]# find /root/ -perm -777/root/ccc/root/test/root/a.sh
7) 设置查找的目录深度
-maxdepth 1 #只能找目录第一层的文件和目录
查找/bin目录下权限等于755的可执行文件
[root@linuxprobe ~]# find /bin/ -maxdepth 1 -a -perm 755 |wc -l2717
8)根据用户ID进行查询
寻找系统中所有属于用户yangjie的目录并且复制到/root/yg/目录下
find / -user yangjie -exec cp {} /root/yg/ \;find: ‘/proc/6140/task/6140/fd/5’: 没有那个文件或目录find: ‘/proc/6140/task/6140/fdinfo/5’: 没有那个文件或目录find: ‘/proc/6140/fd/6’: 没有那个文件或目录
2. 命令的判断
2.1 三个特殊符号: ; && ||
1) ;分号
;不考虑命令的相关性,可连续执行。;不保证命令全部执行成功,只是会按顺序执行,即便前面命令错误,后面的命令也会正常执行;
[root@linuxprobe ~]# sync;ll / |wc -l;ll /root/xxxxxx27ls: 无法访问/root/xxxxxx: 没有那个文件或目录
2)&&逻辑且
&&只有当前面的命令执行成功时,后面的命令才会继续执行。
[root@linuxprobe ~]# cd /opt && touch a.txt && ll a.txt # 先进入/opt目录然后创建a.txt然后查看-rw-r--r--. 1 root root 0 5月 6 20:47 a.txt[root@linuxprobe opt]# cd /yg && touch a.txt && ll a.txt # 先进入/yg目录然后创建a.txt然后查看-bash: cd: /yg: 没有那个文件或目录
经典用法:源码编译
./configure && make -j 4 && make install
3)|| 逻辑或
||逻辑或:
a||b 如果a命令执行成功则不执行b命令,如果a命令执行失败则执行b命令。
查看/opt/目录下是否有yg这个文件夹,如果没有则创建一个
[root@linuxprobe opt]# ll /opt/yg || mkdir /opt/yg ls: 无法访问/opt/yg: 没有那个文件或目录 [root@linuxprobe opt]# ll -d /opt/yg drwxr-xr-x. 2 root root 6 5月 6 20:57 /opt/yg进入/opt/yg文件夹,如果进入则什么都不干,否则在当前路径创建a.txt文件
[root@linuxprobe opt]# cd /opt/yg || touch a.txt[root@linuxprobe yg]# pwd/opt/yg[root@linuxprobe yg]# ll总用量 0
4)总结
命令情况 | 说明 |
---|---|
命令1 && 命令2 | 如果命令1执行,且执行正确($?=0),然后执行命令2如果命令1执行完成,但是执行错误($?≠0),那么不会执行命令2 |
命令1 || 命令2 | 如果命令1执行,且执行正确($?=0),那么不会执行命令2如果命令1执行,但是执行错误($?≠0),那么命令2执行 |
linux中多条命令是从左到右、从前到后、从上到下依次执行的,如下所示:
进入/opt/yg目录,如果不存在则创建/opt/yg/文件夹,然后在/opt/yg文件夹下面穿件123.txt,并且查看
[root@linuxprobe yg]# cd /opt/yg || mkdir /opt/yg && touch /opt/yg/123.txt && ll /opt/yg/123.txt-bash: cd: /opt/yg: 没有那个文件或目录-rw-r--r--. 1 root root 0 5月 6 21:16 /opt/yg/123.txt