查看命令自己linux
查看文件内容shell
过滤匹配数据库
查找文件vim
用于查找命令的位置centos
[root@meditation ~]# which cd /usr/bin/cd
输出命令相关的目录和配置文件bash
[root@meditation ~]# whereis cd cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz
[root@VM_0_15_centos ~]# ls /etc/ > /root/123.txt
从第一行开始显示全部内容输出到屏幕上less
[root@VM_0_15_centos ~]# cat -n /root/123.txt ... 187 trusted-key.key 188 tuned 189 udev 190 updatedb.conf 191 usb_modeswitch.conf 192 uuid 193 vconsole.conf 194 vimrc 195 virc 196 wgetrc 197 wpa_supplicant 198 X11 199 xdg 200 xinetd.d 201 yum 202 yum.conf 203 yum.repos.d
从第一行开始,输出文件内容,当一页没法所有输出时,回车翻行,空格翻页,q退出 常配合管道符使用ssh
[root@VM_0_15_centos ~]# ll /etc/ | more drwxr-xr-x. 2 root root 4096 Jun 10 2014 cron.monthly -rw-r--r--. 1 root root 451 Jun 10 2014 crontab drwxr-xr-x. 2 root root 4096 Jun 10 2014 cron.weekly -rw-------. 1 root root 0 Mar 7 14:38 crypttab -rw-r--r--. 1 root root 1620 Oct 31 2018 csh.cshrc -rw-r--r--. 1 root root 866 Oct 31 2018 csh.login drwxr-xr-x. 4 root root 4096 Jul 2 15:03 dbus-1 drwxr-xr-x. 2 root root 4096 Jul 2 15:03 default drwxr-xr-x. 2 root root 4096 Mar 7 14:39 depmod.d drwxr-x---. 4 root root 4096 Mar 7 14:39 dhcp --More--
和more命令类似,可是支持向前查看,还能够经过/passwd来查找passwd字符串ui
[root@VM_0_15_centos ~]# ll /etc/ | less total 1456 drwxr-xr-x. 3 root root 4096 Mar 7 14:39 abrt drwxr-xr-x. 4 root root 4096 Mar 7 14:39 acpi -rw-r--r--. 1 root root 16 Mar 7 14:42 adjtime -rw-r--r--. 1 root root 1518 Jun 7 2013 aliases -rw-r--r-- 1 root root 12288 Mar 7 14:44 aliases.db drwxr-xr-x. 2 root root 4096 Jul 2 15:03 alternatives -rw------- 1 root root 541 Nov 20 2018 anacrontab ...
显示文件的开始部分 -n 指定显示文件开始部分的行数,默认10行spa
[root@VM_0_15_centos ~]# head -n 2 /root/123.txt abrt acpi
显示文件的结束部分 -n 指定显示文件结束部分的行数,默认10行 -f 监视文件是否有增长变化
[root@VM_0_15_centos ~]# tail -n 2 /root/123.txt yum.conf yum.repos.d
根据文件名查找 优势速度快,从数据库中查找
[root@meditation ~]# touch /home/sunlizhao/abc123.txt [root@meditation ~]# locate abc123.txt [root@meditation ~]#
[root@meditation ~]# updatedb [root@meditation ~]# locate abc123.txt /home/sunlizhao/abc123.txt
用于查找文件中的内容 在文件当中匹配符合条件的字符串
语法
grep [选项] 字符串 文件名
选项
-i ---- 忽略大小写
-v ---- 排除指定字符串(取反)
-r ---- 递归
1.1匹配一个词
[root@meditation ~]# grep root /home/sunlizhao/abc123.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
1.2将文件中没有nologin的那行取出来,并显示行号
[root@meditation ~]# grep -nv nologin /home/sunlizhao/abc123.txt 1:root:x:0:0:root:/root:/bin/bash 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt 25:syslog:x:996:994::/home/syslog:/bin/false 27:jfedu1:x:1000:1000::/home/jfedu1:/bin/bash 28:jfedu2:x:1001:1001::/home/jfedu2:/bin/bash 29:sunlizhao:x:1002:1002::/home/sunlizhao:/bin/bash 30:sunlizhao31:x:1003:1003::/home/sunlizhao31:/bin/bash
1.3从多个文件中匹配,并显示行号
[root@meditation ~]# grep -n root /home/sunlizhao/abc123.txt /etc/passwd /home/sunlizhao/abc123.txt:1:root:x:0:0:root:/root:/bin/bash /home/sunlizhao/abc123.txt:10:operator:x:11:0:operator:/root:/sbin/nologin /etc/passwd:1:root:x:0:0:root:/root:/bin/bash /etc/passwd:10:operator:x:11:0:operator:/root:/sbin/nologin
1.4搜索运行中的sshd进程,并过滤掉grep命令自己
[root@VM_0_15_centos ~]# ps aux | grep sshd | grep -v grep root 1209 0.0 0.5 156744 5440 ? Ss 22:43 0:00 sshd: root@pts/1 root 1323 0.0 0.5 156744 5436 ? Ss 22:44 0:00 sshd: root@pts/2 root 3081 0.0 0.4 112864 4304 ? Ss Aug13 0:52 /usr/sbin/sshd -D root 7396 0.0 0.4 112864 4244 ? Ss 23:35 0:00 sshd: [accepted] root 7969 0.0 0.5 156744 5436 ? Ss 14:12 0:00 sshd: root@pts/0
1.5统计检索出的指定内容有多少行
[root@VM_0_15_centos ~]# egrep "nologin|root" /etc/passwd | wc -l 21
2.1比较两个文件中共同存在的行,并显示行号
[root@meditation ~]# grep -nxf /home/sunlizhao/abc123.txt /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 2:bin:x:1:1:bin:/bin:/sbin/nologin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
2.2比较abc123.txt文件比passwd文件多的部分
添加数据 [root@meditation ~]# echo "hhhhh" >> /home/sunlizhao/abc123.txt [root@meditation ~]# grep -vxf /etc/passwd /home/sunlizhao/abc123.txt hhhhh
查找abc123.txt文件中以root开头的行
[root@meditation ~]# grep ^root /home/sunlizhao/abc123.txt root:x:0:0:root:/root:/bin/bash
查找abc123.txt文件中以bash结尾的行
[root@meditation ~]# grep bash$ /home/sunlizhao/abc123.txt root:x:0:0:root:/root:/bin/bash jfedu1:x:1000:1000::/home/jfedu1:/bin/bash jfedu2:x:1001:1001::/home/jfedu2:/bin/bash sunlizhao:x:1002:1002::/home/sunlizhao:/bin/bash
**查找/sunlizhao/目录下包含"root"的文件
[root@meditation ~]# echo "root" > /home/sunlizhao/abc.txt [root@meditation ~]# grep -r root /home/sunlizhao/ /home/sunlizhao/abc.txt:root /home/sunlizhao/abc123.txt:root:x:0:0:root:/root:/bin/bash /home/sunlizhao/abc123.txt:operator:x:11:0:operator:/root:/sbin/nologin
注意,使用find时要避免大范围搜索,会很是耗费系统资源
选项
[root@VM_0_15_centos ~]# find / -name "passwd" /etc/passwd /etc/pam.d/passwd /usr/share/bash-completion/completions/passwd /usr/bin/passwd
若是须要在搜索时匹配某个文件或目录的完整路径,而不单单是匹配文件名 可使用-path或-ipath
查找/目录下的以passwd开头的文件,父目录必须是/etc
发现有两个文件,passwd-是passwd的自动备份文件
[root@VM_0_15_centos /]# find / -path "/etc/passwd*" /etc/passwd /etc/passwd-
查看root目录下后缀名是.sh的文件
[root@VM_0_15_centos ~]# find /root -type f -name "*.sh" /root/eof.sh /root/ping.sh /root/useradd1.sh /root/packups.sh
检索用户主目录下全部的空目录
[root@VM_0_15_centos ~]# find ~ -type d -empty /root/DES_DIR /root/.config/abrt
对当前匹配条件进行“反义”相似于逻辑非操做
检索出/root下全部文件, 可是后缀不能是.sh
[root@VM_0_15_centos ~]# find /root -type f ! -name "*.sh" /root/cpu.txt /root/.lesshst /root/.cshrc /root/.bashrc /root/.viminfo
检索出/root下全部不为空的文件
[root@VM_0_15_centos ~]# find /root/ -type f ! -empty /root/eof.sh /root/cpu.txt /root/.lesshst /root/ping.sh /root/.cshrc /root/useradd1.sh
查看/root目录下,是否有不属于root的文件
[root@VM_0_15_centos ~]# find /root/ -type f ! -user root [root@VM_0_15_centos ~]#
**检索/root目录下
须要根据文件被修改的时间进行检索
选项/天 | 选项/分钟 | 描述 |
---|---|---|
-mtime | -mmin | 修改时间:最后一次文件内容有过更改的时间点 |
-atime | -amin | 访问时间:最后一次文件有被读取过的时间点 |
-ctime | -cmin | 变动时间: 最后一次文件有被变动过的时间点(如内容被修改,权限被修改) |
常见写法 | 描述 |
---|---|
-mtime 2 | 该文件2天前被修改过 |
-mtime -2 | 该文件2天之内被修改过 |
-mtime +2 | 该文件距离上次修改已经超过2天时间 |
检索/root目录下,五天内被修改的文件,并去掉隐藏文件
[root@VM_0_15_centos ~]# find /root/ -type f -mtime -5 ! -name ".*" /root/.cache/abrt/lastnotification /root/123.txt
表示大小的单位
另外,还可使用+或者-表示大于或小于当前条件
[root@VM_0_15_centos ~]# find /root -type f -size +1k /root/.viminfo /root/.bash_history /root/.packup.swp /root/123.txt /root/c.txt
find /usr -perm u=rwx,g=rx,o=x 若是要匹配的文件的权限为- r-xr-xr-x,即u,g,o的权限是相同的 可使用
[root@VM_0_15_centos ~]# find /root/ -perm u=rw,g=r,o=r /root/eof.sh /root/cpu.txt /root/ping.sh
[root@VM_0_15_centos ~]# find /root/ -perm 644 /root/eof.sh /root/cpu.txt /root/ping.sh
find命令默认是以递归方式检索项目的,但这种方式会致使获得的结果数量很是大
使用-maxdepth限制find命令递归的层数
[root@VM_0_15_centos ~]# find /etc/ -name "passwd" /etc/passwd /etc/pam.d/passwd [root@VM_0_15_centos ~]# find /etc/ -maxdepth 1 -name "passwd" /etc/passwd
10,逻辑组合
在以前的例子中有出现多个搜索天剑的组合以及对某个搜索条件的反转
实际上find命令支持and和or两种逻辑运算,对应的命令选项分别为-a和-o
此外还能够经过小括号对搜索条件进行分组
可是要注意,命令钟的小括号常须要用单引号包裹起来,由于小括号在shell中有特殊含义
检索/etc下名为ssh的目录
[root@VM_0_15_centos ~]# find /etc/ -type d -name "ssh" /etc/ssh /etc/selinux/targeted/active/modules/100/ssh
等同于
[root@VM_0_15_centos ~]# find /etc/ -type d -a -name "ssh" /etc/ssh /etc/selinux/targeted/active/modules/100/ssh