1. 程式人生 > >linux常用分析文件命令

linux常用分析文件命令

[[email protected] ~]# grep [-acinv] [-color=auto] '搜尋字串' filename

選項與引數 -a 將二進位制檔案以text檔案方式搜尋資料 -c 計算找到搜尋字串的個數 -i 不區分大小寫 -n 輸出行號 -v 反向選擇 選擇不包含搜尋字串的選項 -l 列出含有搜尋字串的檔名 -r 在制定目錄中遞迴查詢 –color=auto ‘搜尋字串’ 將找到的關鍵字加上顏色 註釋:可以 vim ~/.bashrc 在檔案中加上 (1)alias grep=‘grep --color=auto’ (2) source ~/.bashrc 就可以不用每次都寫這個引數了 例子: 1.將/etc/passwd 有出現root的行取出來

    # grep root /etc/passwd 或者 cat /etc/passwd | grep root
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin

2.將/etc/passwd ,有出現root的行取出來,同時顯示這些行在/etc/passwd的行號

#grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin

3.將/etc/passwd 中沒有出現root的行取出來

#grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

4.將/etc/passswd 沒有出現root和nologin 的行取出來

#grep -v root /etc/passwd | grep -v nologin /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
syslog:x:498:498::/home/syslog:/bin/false

5.用dmesg 列出核心資訊,再以grep找出內含eth那行,要將捉到的關鍵字顯色,加上行號來表示

[[email protected] ~]#dmesg | grep -n --color=auto 'su'
1:Initializing cgroup subsys cpuset
2:Initializing cgroup subsys cpu
4:Command line: ro root=/dev/vda1 console=ttyS0 console=tty0 panic=5 rd_NO_LUKS rd_NO_LVM LANG=C rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM
5:KERNEL supported cpus:
109:Kernel command line: ro root=/dev/vda1 console=ttyS0 console=tty0 panic=5 rd_NO_LUKS rd_NO_LVM LANG=C rd_NO_MD SYSFONT=latarcyrheb-sun16 [email protected]  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM
130:Initializing cgroup subsys ns
131:Initializing cgroup subsys cpuacct
132:Initializing cgroup subsys memory
133:Initializing cgroup subsys devices
134:Initializing cgroup subsys freezer
135:Initializing cgroup subsys net_cls
136:Initializing cgroup subsys blkio
137:Initializing cgroup subsys perf_event
138:Initializing cgroup subsys net_prio
139:CPU: Unsupported number of siblings 14
140:mce: CPU supports 10 MCE banks
152:Performance Events: unsupported p6 CPU model 79 no PMU driver, software events only.
172:ACPI: (supports S0 S3 S4 S5)
208:SCSI subsystem initialized
532:[ 7446]     0  7446    36256       93   0       0             0 su
641:[ 7446]     0  7446    36256       93   0       0             0 su
740:[22131]   503 22131    36256       96   0       0             0 su
742:[22185]     0 22185    36256       93   0       0             0 su

6.用dmesg 列出核心資訊,再已grep找出內含supported那行,在關鍵字所在行的前兩行與後三行也一起取出來顯示

[[email protected] ~]#dmesg | grep -n -A3 -B2 --color=auto 'supported'
3-Linux version 2.6.32-642.6.2.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) ) #1 SMP Wed Oct 26 06:52:09 UTC 2016
4-Command line: ro root=/dev/vda1 console=ttyS0 console=tty0 panic=5 rd_NO_LUKS rd_NO_LVM LANG=C rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM
5:KERNEL supported cpus:
6-  Intel GenuineIntel
7-  AMD AuthenticAMD
8-  Centaur CentaurHauls
--
137-Initializing cgroup subsys perf_event
138-Initializing cgroup subsys net_prio
139:CPU: Unsupported number of siblings 14
140-mce: CPU supports 10 MCE banks
141-alternatives: switching to unfair spinlock
142-SMP alternatives: switching to UP code
--
150-..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1
151-CPU0: Intel(R) Xeon(R) CPU E5-26xx v4 stepping 01
152:Performance Events: unsupported p6 CPU model 79 no PMU driver, software events only.
153-NMI watchdog disabled (cpu0): hardware events not enabled
154-Brought up 1 CPUs
155-Total of 1 processors activated (4788.89 BogoMIPS).

7.根據檔案內容遞迴查詢目錄

#grep 'supported' *         #在當前目錄搜尋帶’supported‘行的檔案
#grep -r 'supported' *      #在當前目錄及子目錄下搜尋'supported' 行的檔案
#grep -l -r 'supported' *  #在當前目錄及其子目錄下搜尋'supported'行檔案,但是不顯示匹配的行,只顯示匹配的檔案