一、權(quán)限
權(quán)限分類:
owner:屬主,u
group:屬組,g
other:其他,o
3個bit為一個權(quán)限
目錄權(quán)限:
r:列出目錄文件列表
x:cd、目錄文件內(nèi)容(基礎(chǔ)權(quán)限)
w:能否創(chuàng)建修改目錄下的文件。能否刪除某個文件是看父目錄時候有w權(quán)限(需要x權(quán)限)
文件權(quán)限:
r 讀取文件內(nèi)容
w 更改文件內(nèi)容
x 運行(文件默認不會有執(zhí)行權(quán)限)
umask:
umask可以用于保留在創(chuàng)建文件權(quán)限
666-umask=新建文件權(quán)限(如果新建權(quán)限文件為奇數(shù)則加一)
777-umask=新建目錄權(quán)限
二、文本處理工具grep
grep(egrp、fgrep fregp不支持正則表達式)(可以讀取標準輸入)
功能:文本過濾(模式:pattern)工具,根據(jù)用戶指定的“模式”對目標文件逐行進行匹配檢查,打印匹配到的行
模式:由正則表達式字符及文本字符所編寫的過濾條件
語法:grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
選項:alias grep='grep --color=auto' 定義grep別名
grep -v 顯示不被pattern匹配的字符
grep -i 忽略字符大小寫
grep -n 顯示出匹配字符串的行號
grep -c 統(tǒng)計匹配行的數(shù)量
grep -q 靜默模式,不輸出任何信息
grep -o 僅顯示匹配到字符串,每匹配一次顯示一行
grep -A#(#是數(shù)字的意思) 包含字符串的后面幾行
grep -B#(#是數(shù)字的意思) 包含字符串的前面幾行
grep -C#(#是數(shù)字的意思) 包含字符串的前后幾行
grep -e 實現(xiàn)多個選項間的或關(guān)系
grep -w 匹配整個單詞
grep -E 使用ERE
grep -F 相當于fgrep,不支持正則表達式
三、正則表達式
正則表達式(匹配字符串,不是字符名):
REGEXP:由一類特殊字符及文本字符所編寫的模式,其中有 些字符(元字符)不表示字符字面意義,而表示控制或通配的功能
程序支持:grep,sed,awk,vim, less,nginx,varnish等
分兩類: 基本正則表達式:BRE
擴展正則表達式:ERE grep -E, egrep
正則表達式引擎:
采用不同算法,檢查處理正則表達式的軟件模塊
PCRE(Perl Compatible Regular Expressions)
元字符分類:字符匹配、匹配次數(shù)、位置錨定、分組
用途1、字符匹配:
. 匹配任意單個字符
[] 匹配指定范圍內(nèi)的任意單個字符
[^] 匹配指定范圍外的任意單個字符
[:alnum:] 字母和數(shù)字
[:alpha:] 代表任何英文大小寫字符,亦即A-Z, a-z
[:lower:] 小寫字母[:upper:] 大寫字母
[:blank:] 空白字符(空格和制表符)
[:space:]水平和垂直的空白字符(比[:blank:]包含的范圍廣)
[:cntrl:] 不可打印的控制字符(退格、刪除、警鈴...)
[:digit:] 十進制數(shù)字
[:xdigit:]十六進制數(shù)字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 標點符號
匹配次數(shù):用在要指定次數(shù)的字符后面,用于指定前面的字符要出現(xiàn)的次數(shù)
* 匹配前面的字符任意次,包括0次 (貪婪模式:盡可能長的匹配)
.* 任意長度的任意字符
\? 匹配其前面的字符0或1次
\+ 匹配其前面的字符至少1次
\{n\} 匹配前面的字符n次
\{m,n\} 匹配前面的字符至少m次,至多n次
\{,n\} 匹配前面的字符至多n次
\{n,\} 匹配前面的字符至少n次
位置錨定:定位出現(xiàn)的位置
^ 行首錨定,用于模式的最左側(cè)
$ 行尾錨定,用于模式的最右側(cè)
^PATTERN$ 用于模式匹配整行
^$ 空行
^[[:space:]]*$ 空白行
\< 或\b詞首錨定,用于單詞模式的左側(cè)
\> 或\b詞尾錨定;用于單詞模式的右側(cè)
\<PATTERN\>匹配整個單詞 = grep -w
分組:
\(\) 將一個或多個字符捆綁在一起,當作一個整體進行處理,如:\(root\)\+
分組括號中的模式匹配到的內(nèi)容會被正則表達式引擎記錄于內(nèi)部的變量中,這些變量的命名方式為: \1, \2, \3, ...
\1表示從左側(cè)起第一個左括號以及與之匹配右括號之間的模式所匹配到的字符(后向引用是引用的字符,而不是正則表達式)
示例:\(string1\+\(string2\)*\)
\1 :string1\+\(string2\)*
\2 :string2
后向引用:引用前面的分組括號中的模式所匹配字符,而非模式本身
或者:\|
示例:a\|b: a或b C\|cat: C或cat \(C\|c\)at:Cat或cat
egrep 擴展的正則表達式 = grep -E
語法:egrep [OPTIONS] PATTERN [FILE...]
和正則表達式的區(qū)別在于 少了\。
實例:
1、復(fù)制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內(nèi)部文件的屬組和其它用戶均沒有任何訪問權(quán)限。
cp -a /etc/skel /home/tuser1
chmod -R 700 /home/tuser1
ll -d /home/tuser1
drwx------. 2 root root 4096 Jan 9 2018 /home/tuser1
2、編輯/etc/group文件,添加組hadoop。
vim /etc/group
hadoop:x:1010
cat /etc/group| grep "^hadoop"
hadoop:x:1010
3、手動編輯/etc/passwd文件新增一行,添加用戶hadoop,其基本組ID為hadoop組的id號;其家目錄為/home/hadoop。
vim /etc/passwd
hadoop:x:1010:1010::/home/hadoop:/bin/bash
cat /etc/passwd| grep "^hadoop"
hadoop:x:1010:1010::/home/hadoop:/bin/bash
4、復(fù)制/etc/skel目錄為/home/hadoop,要求修改hadoop目錄的屬組和其它用戶沒有任何訪問權(quán)限。
cp -r /etc/skel /home/hadoop
chmod -R 700 /home/hadoop
ll -d /home/hadoop
drwx------ 2 root root 4096 Sep 9 15:43 /home/hadoop
5、修改/home/hadoop目錄及其內(nèi)部所有文件的屬主為hadoop,屬組為hadoop。
chown -R hadoop:hadoop /home/hadoop
ll -a /home/hadoop
total 20
drwx------ 2 hadoop hadoop 4096 Sep 9 15:43 .
drwxr-xr-x. 10 root root 4096 Sep 9 15:43 ..
-rwx------ 1 hadoop hadoop 18 Sep 9 15:43 .bash_logout
-rwx------ 1 hadoop hadoop 193 Sep 9 15:43 .bash_profile
-rwx------ 1 hadoop hadoop 231 Sep 9 15:43 .bashrc
6、顯示/proc/meminfo文件中以大寫或小寫S開頭的行;用兩種方式;
cat /proc/meminfo | grep "^s\|^S"
cat /proc/meminfo | grep "^[sS]"
SwapCached: 0 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Shmem: 712 kB
Slab: 87464 kB
SReclaimable: 75748 kB
SUnreclaim: 11716 kB
7、顯示/etc/passwd文件中其默認shell為非/sbin/nologin的用戶;
grep -v "sbin/nologin$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
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:996:994::/home/syslog:/bin/false
admin:x:1000:1000::/home/admin:/bin/bash
xdm:x:1001:1001::/home/xdm:/bin/bash
bash:x:1003:1006::/home/bash:/bin/bash
testbash:x:1004:1007::/home/testbash:/bin/bash
sh:x:1005:1008::/home/sh:/bin/bash
hadoop:x:1010:1010::/home/hadoop:/bin/bash
8、顯示/etc/passwd文件中其默認shell為/bin/bash的用戶;
grep "/bin/bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
admin:x:1000:1000::/home/admin:/bin/bash
xdm:x:1001:1001::/home/xdm:/bin/bash
bash:x:1003:1006::/home/bash:/bin/bash
testbash:x:1004:1007::/home/testbash:/bin/bash
sh:x:1005:1008::/home/sh:/bin/bash
hadoop:x:1010:1010::/home/hadoop:/bin/bash
9、找出/etc/passwd文件中的一位數(shù)或兩位數(shù);
grep -wo "[0-9]\{1,2\}" /etc/passwd
10、顯示/etc/grub2.cfg中以至少一個空白字符開頭的行;
grep "^[[:blank:]]\+.*" /etc/grub2.cfg
11、顯示/etc/grub2.cfg文件中以#開頭,后面跟至少一個空白字符,而后又有至少一個非空白字符的行;
grep "^#.*[[:blank:]]\+[^[:blank:]]\+" /etc/grub2.cfg
# DO NOT EDIT THIS FILE
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
### BEGIN /etc/grub.d/00_header ###
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
### END /etc/grub.d/00_header ###
### BEGIN /etc/grub.d/00_tuned ###
### END /etc/grub.d/00_tuned ###
### BEGIN /etc/grub.d/01_users ###
### END /etc/grub.d/01_users ###
### BEGIN /etc/grub.d/10_linux ###
### END /etc/grub.d/10_linux ###
### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###
### BEGIN /etc/grub.d/20_ppc_terminfo ###
### END /etc/grub.d/20_ppc_terminfo ###
### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###
### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###
### BEGIN /etc/grub.d/41_custom ###
### END /etc/grub.d/41_custom ###
12、打出netstat -tan命令執(zhí)行結(jié)果中以‘LISTEN’,后或跟空白字符結(jié)尾的行;
netstat -tan | grep "LISTEN[[:blank:]]\+$"
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
13、添加用戶bash, testbash, basher, nologin (此一個用戶的shell為/sbin/nologin),而后找出當前系統(tǒng)上其用戶名和默認shell相同的用戶的信息;
useradd bash
useradd testbash
useradd bahser
useradd -s /sbin/nologin nologin
grep "^\(.*\):.*\b\1$" /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
bash:x:1003:1006::/home/bash:/bin/bash
nologin:x:1006:1009::/home/nologin:/sbin/nologin