查看命令自己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