bash shell特性
- 什么是bash shell
bash shell 是命令解釋器,存在于linux操作系統(tǒng)的最外層哮兰,負責將用戶輸入的命令翻譯給操作系統(tǒng)內(nèi)核進行執(zhí)行的接口。 - bash shell的執(zhí)行流程
1)判斷命令是否通過絕對路徑進行執(zhí)行
2)判斷命令是否有alias別名
3)判斷命令是內(nèi)部命令還是外部命令
4)如果是內(nèi)部命令直接執(zhí)行,外部命令查找是否有hash緩存
5)如果沒有hash緩存霎终,通過PATH路徑進行查找 - 什么是內(nèi)部命令,什么是外部命令
1)內(nèi)部命令:shell程序自帶的命令
2)外部命令:在系統(tǒng)PATH變量中某個路徑下的可執(zhí)行程序 - 如何檢查輸入的命令是外部命令還是內(nèi)部命令
使用type -a 命令來查看
#cd命令屬于shell內(nèi)部命令
[root@localhost ~]$ type -a cd
cd is a shell builtin
cd is /usr/bin/cd
#ping屬于外部命令, 同時會打印當前命令路徑
[root@localhost ~]$ type -a ping
ping is /bin/ping
- PATH環(huán)境變量
1)PATH由多個路徑組成升薯,每個路徑值之間用冒號間隔莱褒,對這些路徑的增加和刪除操作都將影響到Bash解釋器對Linux命令的查找
2)使用echo $PATH查看系統(tǒng)的環(huán)境變量
[root@localhost.localdomain /]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
- 如果是外部命令,執(zhí)行完命令將緩存在hash中涎劈,下次執(zhí)行的時候將通過緩存調(diào)取執(zhí)行广凸,不會查找PATH變量,如果hash中存在命令緩存蛛枚,此時將命令移動到其他PATH變量中存在的目錄中谅海,命令執(zhí)行將會報找不到這個命令的錯誤,使用hash -r清除緩存記錄蹦浦,系統(tǒng)將會通過PATH變量找到該命令扭吁。
[root@localhost.localdomain ~]$ hash # hash中存在free命令路徑
hits command
1 /usr/bin/ls
1 /usr/bin/free
[root@localhost.localdomain ~]$ free #執(zhí)行free成功,從hash中查找free命令
total used free shared buff/cache available
Mem: 995748 195612 386500 7688 413636 645016
Swap: 1048572 0 1048572
[root@localhost.localdomain ~]$ mv /usr/bin/free /sbin #將free移動位置
[root@localhost.localdomain ~]$ free
-bash: /usr/bin/free: No such file or directory #報錯盲镶,hash緩存中/usr/bin/找不到free
[root@localhost.localdomain ~]$ hash -r #清除hash表
[root@localhost.localdomain ~]$ free #執(zhí)行free成功侥袜,從PATH變量中查找free
total used free shared buff/cache available
Mem: 995748 195588 386524 7688 413636 645040
Swap: 1048572 0 1048572
- alias命令
查看系統(tǒng)別名以及創(chuàng)建別名
[root@localhost.localdomain ~]$ alias # alias查看系統(tǒng)中存在的別名
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias grep='grep --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
[root@localhost.localdomain ~]$ alias free="free -h" #臨時創(chuàng)建別名
[root@localhost.localdomain ~]$ alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias free='free -h'
alias grep='grep --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
[root@localhost.localdomain ~]$ free
total used free shared buff/cache available
Mem: 972M 190M 377M 7.5M 403M 630M
Swap: 1.0G 0B 1.0G
- ls 命令
查看目錄以及文件信息
[root@localhost.localdomain /]$ ls #查看當前目錄下的內(nèi)容
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost.localdomain /]$ ls -a # -a查看當前目錄下的所有內(nèi)容(隱藏文件)
. .. bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost.localdomain /]$ ls -l /tmp # -l查看/tmp目錄下內(nèi)容的詳細信息
total 4
-rwx------. 1 root root 836 Oct 19 10:24 ks-script-LKdnj5
drwx------. 3 root root 17 Oct 19 10:29 systemd-private-73db8943d3c34b968d054a5527d3f835-chronyd.service-Ybtxup
drwx------. 2 root root 6 Oct 19 10:29 vmware-root_572-2999067484
-rw-------. 1 root root 0 Oct 19 10:20 yum.log
[root@localhost.localdomain /]$ ls -lh # -h將文件大小使用KB、GB溉贿、MB的方式展示
total 16K
lrwxrwxrwx. 1 root root 7 Oct 19 10:20 bin -> usr/bin
dr-xr-xr-x. 5 root root 4.0K Oct 19 10:24 boot
drwxr-xr-x. 18 root root 3.0K Oct 19 10:29 dev
drwxr-xr-x. 75 root root 8.0K Oct 20 11:13 etc
[root@localhost.localdomain /]$ ls -dl / # -d展示目錄本身的信息枫吧,不展示目錄里面的內(nèi)容
dr-xr-xr-x. 17 root root 224 Oct 19 10:24 /
文件管理
- 系統(tǒng)的目錄結構
[root@localhost.localdomain /]$ ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
1、/bin: 是/usr/bin的軟連接宇色,主要存放一些普通用戶可以執(zhí)行的命令文件
2九杂、/boot: 主要存放系統(tǒng)啟動時所需要的文件(內(nèi)核闽寡、Grup引導文件等等)
3、/dev: 主要存放一些硬件文件(磁盤尼酿,鍵盤等)
4爷狈、/etc: 主要存放的是系統(tǒng)的配置文件
5、/home: 普通用戶的家目錄
6裳擎、/lib和/lib64: 是/usr/lib和/usr/lib64的軟鏈接涎永,主要存在的是命令執(zhí)行時所需要的庫文件
7、/media和/mnt: 掛載目錄
8鹿响、/opt: 額外軟件包的安裝目錄
9羡微、/proc: 反應系統(tǒng)進程的實時狀態(tài)的目錄(存放關于正在執(zhí)行的進程狀態(tài)文件、內(nèi)存狀態(tài)文件等)
10惶我、/root: root用戶的家目錄
11妈倔、/sbin: 是/usr/sbin的軟鏈接,主要存放一些root用戶才能執(zhí)行的命令文件
12绸贡、/tmp: 臨時文件夾
13盯蝴、/usr: 相當于windows中的C:\Windows文件,主要是一些系統(tǒng)文件
14听怕、/var: 主要存放的是一些日志文件
- cd命令
切換目錄
[root@localhost.localdomain /]$ cd /etc #切換到指定的目錄
[root@localhost.localdomain /etc]$ cd - #切換到上一次所在的目錄
/
[root@localhost.localdomain /]$ cd #切換到當前用戶的家目錄
[root@localhost.localdomain ~]$ cd . #切換到當前目錄
[root@localhost.localdomain ~]$ cd .. #切換到上一級目錄
[root@localhost.localdomain /]$
- touch命令
如果文件存在捧挺,則修改文件的時間,如果文件不存在尿瞭,則創(chuàng)建文件
[root@localhost.localdomain ~]$ touch file1.txt #創(chuàng)建文件file1.txt
[root@localhost.localdomain ~]$ ls
anaconda-ks.cfg file1.txt
[root@localhost.localdomain ~]$ touch file{a,b}.txt #創(chuàng)建文件filea.txt fileb.txt
[root@localhost.localdomain ~]$ ls
anaconda-ks.cfg file1.txt filea.txt fileb.txt
[root@localhost.localdomain ~]$ touch file{2..5}.txt #創(chuàng)建文件 file2.txt file3.txt file4.txt file5.txt
[root@localhost.localdomain ~]$ ls
anaconda-ks.cfg file1.txt file2.txt file3.txt file4.txt file5.txt filea.txt fileb.txt
[root@localhost.localdomain ~]$ touch filec.txt filed.txt #創(chuàng)建文件filec.txt filed.txt
[root@localhost.localdomain ~]$ ls
anaconda-ks.cfg file1.txt file2.txt file3.txt file4.txt file5.txt filea.txt fileb.txt filec.txt filed.txt
- mkdir命令
創(chuàng)建目錄闽烙,選項:-v 顯示詳情 -p遞歸創(chuàng)建
[root@localhost.localdomain ~]$ mkdir test1 #創(chuàng)建目錄test1
[root@localhost.localdomain ~]$ ls
anaconda-ks.cfg test1
[root@localhost.localdomain ~]$ mkdir -p test2/test3 #遞歸創(chuàng)建目錄./test2/test3
[root@localhost.localdomain ~]$ ls
anaconda-ks.cfg test1 test2
[root@localhost.localdomain ~]$ mkdir -v test4 #創(chuàng)建test4目錄并顯示詳細信息
mkdir: created directory ‘test4’
[root@localhost.localdomain ~]$ mkdir -pv test2/test{4..6} #遞歸創(chuàng)建./test2/test{4..6} 并顯示詳細信息
mkdir: created directory ‘test2/test4’
mkdir: created directory ‘test2/test5’
mkdir: created directory ‘test2/test6’
- tree命令
以樹狀結構顯示目錄,選項:-L 1 顯示一層声搁,-d 只顯示目錄
[root@localhost.localdomain ~]$ tree #以樹狀結構顯示當前目錄里的所有內(nèi)容
.
├── anaconda-ks.cfg
├── test1
├── test2
│ ├── test3
│ ├── test4
│ ├── test5
│ └── test6
└── test4
7 directories, 1 file
[root@localhost.localdomain ~]$ tree -d #以樹狀結構顯示當前目錄里的目錄內(nèi)容
.
├── test1
├── test2
│ ├── test3
│ ├── test4
│ ├── test5
│ └── test6
└── test4
7 directories
[root@localhost.localdomain ~]$ tree -L 1 #以樹狀結構顯示當前目錄里的第一層內(nèi)容
.
├── anaconda-ks.cfg
├── test1
├── test2
└── test4
3 directories, 1 file
- cp命令
復制文件黑竞,選項:-v 顯示詳情 -r 遞歸復制
[root@localhost.localdomain ~]$ cp -vr ./test1 /tmp #將./test1目錄復制到/tmp中并顯示詳細信息
‘./test1’ -> ‘/tmp/test1’
[root@localhost.localdomain ~]$ cp -vr ./test1 /tmp/test2 #將./test1目錄復制到/tmp中并改名為test2。
‘./test1’ -> ‘/tmp/test2’
- mv命令
移動文件疏旨,注意:遞歸移動不需要加參數(shù)
[root@localhost.localdomain ~]$ mv test1 test3 #原地移動文件相當于改名
[root@localhost.localdomain ~]$ mv ./test4 /tmp #將./test4文件移動到./tmp下
[root@localhost.localdomain ~]$ mv ./test3 /tmp/test5 #將./test3移動到/tmp下并改名為test5
- rm命令
刪除文件很魂,選項:-r 遞歸刪除 -f 強制刪除 -v 顯示詳情
[root@localhost.localdomain ~]$ rm file1.txt #刪除文件, 默認rm存在alias別名,rm -i所以會提醒是否刪除文件
rm: remove regular empty file ‘file1.txt’? y
[root@localhost.localdomain ~]$ rm -f file2.txt #刪除文件, 不提醒
[root@localhost.localdomain ~]$ rm -f test2 #不加-r充石,無法刪除目錄
rm: cannot remove ‘test2’: Is a directory
[root@localhost.localdomain ~]$ rm -rf test2 #強制刪除目錄,不提醒(慎用)
- cat命令
查看文件內(nèi)容 選項:-n 顯示行數(shù) -A 顯示文件中的(tab莫换、結尾$)
[root@localhost.localdomain ~]$ cat >>file3.txt<<eof #交互式追加文件內(nèi)容
> asdfasfas fasdfafas
> sfsfsddf
> sdfsdfgsd sfdfsd
> eof
[root@localhost.localdomain ~]$ cat file3.txt #查看file3.txt文件
asdfasfas fasdfafas
sfsfsddf
sdfsdfgsd sfdfsd
[root@localhost.localdomain ~]$ cat -n file3.txt #-n查看file3.txt文件并顯示行號
1 asdfasfas fasdfafas
2 sfsfsddf
3 sdfsdfgsd sfdfsd
[root@localhost.localdomain ~]$ cat -A file3.txt #查看file3.txt文件并顯示制表符以及結尾$
asdfasfas fasdfafas$
sfsfsddf$
sdfsdfgsd sfdfsd$
- more/less命令
以翻頁的形式顯示文件內(nèi)容 - head命令
默認顯示文件前十行的內(nèi)容 選項: -n 5 顯示前五行
[root@localhost.localdomain ~]$ head /etc/passwd # 默認顯示前十行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost.localdomain ~]$ head -n 5 /etc/passwd # 顯示前五行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
- tail 命令
默認顯示文件后十行的內(nèi)容
[root@localhost.localdomain ~]$ tail /etc/passwd #默認顯示文件后10行的內(nèi)容
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
hax:x:1000:1000::/home/hax:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost.localdomain ~]$ tail -5 /etc/passwd #顯示文件后五行
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
hax:x:1000:1000::/home/hax:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@localhost.localdomain ~]$ tail -f /etc/passwd #-f查看文件尾部的變化
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
hax:x:1000:1000::/home/hax:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
- grep命令
文本搜索命令
[root@localhost.localdomain ~]$ grep root /etc/passwd # 篩選包含root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost.localdomain ~]$ grep ^root /etc/passwd #篩選以root開頭的行
root:x:0:0:root:/root:/bin/bash
[root@localhost.localdomain ~]$ grep bash$ /etc/passwd #篩選以bash結尾的行
root:x:0:0:root:/root:/bin/bash
hax:x:1000:1000::/home/hax:/bin/bash
[root@localhost.localdomain ~]$ grep -i ftp /etc/passwd # -i 忽略ftp大小寫
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@localhost.localdomain ~]$ grep -Ei "sync$|ftp" /etc/passwd # -E匹配時可以使用元字符匹配,相當于egrep
sync:x:5:0:sync:/sbin:/bin/sync
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@localhost.localdomain ~]$ grep -n -A 2 ftp /etc/passwd # -n 顯示行號 -A 2 匹配ftp行內(nèi)容并打印后兩行
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:99:99:Nobody:/:/sbin/nologin
14-systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
[root@localhost.localdomain ~]$ grep -n -B 2 ftp /etc/passwd # -n 顯示行號 -B 2 匹配ftp行內(nèi)容并打印前兩行
10-operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@localhost.localdomain ~]$ grep -n -C 2 ftp /etc/passwd # -n 顯示行號 -C 2 匹配ftp行內(nèi)容并打印前后各兩行
10-operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13-nobody:x:99:99:Nobody:/:/sbin/nologin
14-systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
[root@localhost.localdomain ~]$ grep -v sfsfsdd file3.txt #除了含sfsfsdd內(nèi)容的都顯示出來
asdfasfas fasdfafas
sdfsdfgsd sfdfsd
- wget/curl命令
聯(lián)網(wǎng)下載文件骤铃, wget:選項 -O 指定下載位置 curl 選項: -o 指定下載位置
#使用wget命令要安裝wget
yum install -y wget
[root@localhost.localdomain ~]$ wget http://mirrors.aliyun.com/repo/Centos-7.repo #將文件下載到本地
[root@localhost.localdomain ~]$ wget -O ./centostest http://mirrors.aliyun.com/repo/Centos-7.repo #將文件下載到指定目錄并改名為centostest
[root@localhost.localdomain ~]$ curl http://mirrors.aliyun.com/repo/Centos-7.repo #顯示網(wǎng)絡文件的內(nèi)容(不下載)
[root@localhost.localdomain ~]$ curl -o ./centostest1 http://mirrors.aliyun.com/repo/Centos-7.repo #下載網(wǎng)絡文件到指定的目錄并改名
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2523 100 2523 0 0 9759 0 --:--:-- --:--:-- --:--:-- 9741
- 修改yum源地址
進入到/etc/yum.repos.d目錄中拉岁,備份CentOS-Base.repo文件,執(zhí)行wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo命令修改yum源地址為阿里云yum地址 - rz/sz 命令
rz : 上傳文件到linux中
sz: 從linux中下載文件
使用rz/sz命令需要安裝lrzsz軟件惰爬,使用yum install -y lrzsz命令下載軟件
rz #上傳文件到linux當前目錄中
sz /path/file #下載/path/file文件到Windows中
- which/whereis/type -a 命令
命令查找命令
[root@localhost.localdomain ~]$ which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost.localdomain ~]$ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
[root@localhost.localdomain ~]$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
- sort命令
內(nèi)容排序积蜻,選項:-n 按照數(shù)字排序熄攘,-t 指定分隔符 -k指定第幾列(1,2)指定第幾列的第幾個字符(1.1,1.3)疙咸, -r 倒序
# 先按照第三個字段開始到第三個字段結束排序,再按照第四個字段的第一個字符開始到第三個字符結束進行排序
[root@localhost.localdomain ~]$ sort -t "." -k3,3 -k4.1,4.3 -n ip.txt
192.168.0.151 00:0F:AF:85:6C:F6
192.168.0.151 00:0F:AF:85:6C:F6
192.168.0.152 00:0F:AF:83:1F:65
192.168.0.153 00:0F:AF:85:70:03
192.168.0.153 00:0F:AF:85:70:03
192.168.1.1 00:0F:AF:81:19:1F
192.168.1.10 00:30:15:A2:3B:B6
192.168.1.11 00:30:15:A3:23:B7
192.168.1.11 00:30:15:A3:23:B7
192.168.1.12 00:30:15:A2:3A:A1
192.168.1.21 00:0F:AF:85:6C:09
192.168.1.152 00:0F:AF:83:1F:65
192.168.2.2 00:0F:AF:85:6C:25
192.168.2.20 00:0F:AF:85:55:DE
192.168.2.20 00:0F:AF:85:55:DE
192.168.2.21 00:0F:AF:85:6C:09
192.168.2.22 00:0F:AF:85:5C:41
192.168.2.22 00:0F:AF:85:5C:41
192.168.3.1 00:0F:AF:81:19:1F
192.168.3.2 00:0F:AF:85:6C:25
192.168.3.3 00:0F:AF:85:70:42
192.168.3.3 00:0F:AF:85:70:42
192.168.3.10 00:30:15:A2:3B:B6
192.168.3.12 00:30:15:A2:3A:A1
- uniq 命令
去重統(tǒng)計命令 選項:-c 計算重復的的行的數(shù)量
# 統(tǒng)計出ip出現(xiàn)的次數(shù)并排序
[root@localhost.localdomain ~]$ sort -t "." -k3,3 -k4.1,4.3 -n ip.txt|uniq -c|sort -nr
2 192.168.3.3 00:0F:AF:85:70:42
2 192.168.2.22 00:0F:AF:85:5C:41
2 192.168.2.20 00:0F:AF:85:55:DE
2 192.168.1.11 00:30:15:A3:23:B7
2 192.168.0.153 00:0F:AF:85:70:03
2 192.168.0.151 00:0F:AF:85:6C:F6
1 192.168.3.2 00:0F:AF:85:6C:25
1 192.168.3.12 00:30:15:A2:3A:A1
1 192.168.3.1 00:0F:AF:81:19:1F
1 192.168.3.10 00:30:15:A2:3B:B6
1 192.168.2.21 00:0F:AF:85:6C:09
1 192.168.2.2 00:0F:AF:85:6C:25
1 192.168.1.21 00:0F:AF:85:6C:09
1 192.168.1.152 00:0F:AF:83:1F:65
1 192.168.1.12 00:30:15:A2:3A:A1
1 192.168.1.1 00:0F:AF:81:19:1F
1 192.168.1.10 00:30:15:A2:3B:B6
1 192.168.0.152 00:0F:AF:83:1F:65
- cut命令
截取字段至標準輸出狞尔, 選項: -b 以字節(jié)為單位進行截取 -c 以字符為單位進行截取 -d 指定分割符(默認tab) -f 與-d一起使用,指定顯示那個區(qū)域 -n 取消分割多字節(jié)字符巩掺,與-b一起使用
截取ip.txt文件中以.為分隔符的第二個字段
[root@localhost.localdomain ~]$ cut -d "." -f 2 ip.txt
168
168
168
168
168
168
168
168
168
[root@localhost.localdomain ~]$ awk -F "." '{print $2}' ip.txt # awk -F 指定分割符 {print $2}打印第二個字段
168
168
168
168
168
168
168
168
168
- wc命令
統(tǒng)計行號 選項: -l 統(tǒng)計文件行數(shù)
[root@localhost.localdomain ~]$ wc -l ip.txt
24 ip.txt
- sed命令
主要功能是對行內(nèi)容進行替換偏序,次要功能取某一行的行內(nèi)容,選項:-n 取消默認輸出; '2p'取第二行胖替, -i 不輸出內(nèi)容研儒,直接修改文件; 's###g'替換內(nèi)容独令;-r 使用高級正則表達式
[root@localhost.localdomain ~]$ ifconfig ens32
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.30 netmask 255.255.255.0 broadcast 192.168.200.255
inet6 fe80::47d:77d6:ae20:9620 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:a2:43:18 txqueuelen 1000 (Ethernet)
RX packets 124182 bytes 127905228 (121.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 43096 bytes 9312317 (8.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost.localdomain ~]$ ifconfig ens32|sed -n '2p' # 取出ifconfig ens32的第二行
inet 192.168.200.30 netmask 255.255.255.0 broadcast 192.168.200.255
[root@localhost.localdomain ~]$ ifconfig ens32|sed -n '2,3p' # 取出ifconfig ens32的第二行和第三行
inet 192.168.200.30 netmask 255.255.255.0 broadcast 192.168.200.255
inet6 fe80::47d:77d6:ae20:9620 prefixlen 64 scopeid 0x20<link>
[root@localhost.localdomain ~]$ sed '2s#www#hhh#g' web.log # 將第二行的www替換為hhh(沒有真正的替換端朵,只是輸出到屏幕)
http://www.xuliangwei.com/index.html
http://hhh.xuliangwei.com/1.html
http://post.xuliangwei.com/index.html
http://mp3.xuliangwei.com/index.html
http://www.xuliangwei.com/3.html
http://post.xuliangwei.com/2.html
[root@localhost.localdomain ~]$ sed -i '2s#www#hhh#g' web.log # -i 將替換的內(nèi)容保存到文件中
[root@localhost.localdomain ~]$ ifconfig ens32|sed -n 2p|sed -r 's#^.*net (.*) net.*$#\1#g'
192.168.200.30
- 取出web.log文件中的域名并統(tǒng)計排序
[root@localhost.localdomain ~]$ cat web.log
http://www.xuliangwei.com/index.html
http://www.xuliangwei.com/1.html
http://post.xuliangwei.com/index.html
http://mp3.xuliangwei.com/index.html
http://www.xuliangwei.com/3.html
http://post.xuliangwei.com/2.html
[root@localhost.localdomain ~]$ awk -F '/' '{print $3}' web.log |sort|uniq -c| sort -nr
3 www.xuliangwei.com
2 post.xuliangwei.com
1 mp3.xuliangwei.com
- 取出iifconfig中的ip地址
[root@localhost.localdomain ~]$ ifconfig ens32
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.30 netmask 255.255.255.0 broadcast 192.168.200.255
inet6 fe80::47d:77d6:ae20:9620 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:a2:43:18 txqueuelen 1000 (Ethernet)
RX packets 123598 bytes 127853467 (121.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 42743 bytes 9271887 (8.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost.localdomain ~]$ ifconfig ens32|grep 'inet '|awk '{print $2}'
192.168.200.30
[root@localhost.localdomain ~]$ ifconfig ens32|sed -n '2p'|awk '{print $2}'
192.168.200.30
[root@localhost.localdomain ~]$ ifconfig ens32|sed -nr '2s#^.*net (.*) net.*$#\1#gp'
192.168.200.30
[root@localhost.localdomain ~]$ ifconfig ens32|awk 'NR==2 {print $2}'
192.168.200.30
[root@localhost.localdomain ~]$ ifconfig ens32|awk '/broadcast/ {print $2}'
192.168.200.30
- 修改/etc/selinux/config 中SELINUX=enforcing為SELINUX=disable
[root@localhost.localdomain /etc/selinux]$ cat config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost.localdomain /etc/selinux]$ sed -i '/^SELINUX=/s#enforcing#disable#g' config
- 系統(tǒng)文件類型
- 普通文件(文本, 二進制, 壓縮, 圖片, 日志等)
d 目錄文件
b 設備文件(塊設備)存儲設備硬盤 /dev/sda1, /dev/sda2
c 設備文件(字符設備),終端 /dev/tty1, /dev/zero
s 套接字文件, 進程間通信(socket)
p 管道文件
l 鏈接文件
- 查看普通文件的詳細類型(file命令)
[root@localhost.localdomain ~]$ file /etc/hosts
/etc/hosts: ASCII text
[root@localhost.localdomain ~]$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=aa7ff68f13de25936a098016243ce57c3c982e06, stripped
[root@localhost.localdomain ~]$ file /dev/sda
/dev/sda: block special
[root@localhost.localdomain ~]$ file /dev/tty1
/dev/tty1: character special
- 系統(tǒng)鏈接文件
文件有文件名與數(shù)據(jù)燃箭,在Linux上被分成兩個部分:用戶數(shù)據(jù) (user data) 與元數(shù)據(jù) (metadata)冲呢。用戶數(shù)據(jù),即文件數(shù)據(jù)塊 (data block)招狸,數(shù)據(jù)塊是記錄文件真實內(nèi)容的地方敬拓,我們將其稱為Block元數(shù)據(jù),即文件的附加屬性瓢颅,如文件大小恩尾、創(chuàng)建時間、所有者等信息挽懦。我們稱其為Inode在Linux中,inode是文件元數(shù)據(jù)的一部分但其并不包含文件名木人,inode號即索引節(jié)點號)文件名僅是為了方便人們的記憶和使用信柿,系統(tǒng)或程序通過 inode 號尋找正確的文件數(shù)據(jù)塊。 - 什么是軟鏈接
軟鏈接相當于Windows的快捷方式醒第,軟鏈接文件會將inode指向源文件的block渔嚷,當我們訪問這個軟鏈接文件時,其實訪問的是源文件本身稠曼。那么當我們對一個文件創(chuàng)建多個軟鏈接形病,其實就是多個inode指向同一個block。當我們刪除軟鏈接文件時霞幅,其實只是刪除了一個inode指向漠吻,并不會對源文件源文件造成影響,但如果刪除的是源文件則會造成所有軟鏈接文件失效司恳。
[root@localhost.localdomain ~]# ln -s test.txt test # 給test.txt創(chuàng)建一個軟鏈接test
[root@localhost.localdomain ~]# ll
lrwxrwxrwx. 1 root root 8 Oct 22 18:09 test -> test.txt
-rw-r--r--. 1 root root 0 Oct 22 18:09 test.txt
- 什么是硬鏈接
若一個inode號對應多個文件名途乃,則稱這些文件為硬鏈接。換言之扔傅,硬鏈接就是同一個文件使用了多個別名耍共,刪除其中一個文件只是相當于刪除了文件的別名烫饼,并沒有刪除文件inode和block本身
[root@localhost.localdomain ~]# ln test.txt test_hard.txt # 給test.txt創(chuàng)建硬鏈接test_hard.txt
[root@localhost.localdomain ~]# ll
lrwxrwxrwx. 1 root root 8 Oct 22 18:09 test -> test.txt
-rw-r--r--. 2 root root 0 Oct 22 18:09 test_hard.txt # 在詳情中看到硬鏈接數(shù)為2
-rw-r--r--. 2 root root 0 Oct 22 18:09 test.txt
- 為什么創(chuàng)建的空目錄有2個硬鏈接
[root@localhost.localdomain ~]# ll -di 2
34346745 drwxr-xr-x. 2 root root 6 Oct 22 18:27 2
[root@localhost.localdomain ~]# cd 2
[root@localhost.localdomain ~/2]# ll -ai
total 4
34346745 drwxr-xr-x. 2 root root 6 Oct 22 18:27 . # . 這個目錄是這個目錄的硬鏈接
67147841 dr-xr-x---. 4 root root 4096 Oct 22 18:27 .. # .. 這個目錄是上一級目錄的硬鏈接
- 硬鏈接與軟鏈接區(qū)別
1)ln命令創(chuàng)建硬鏈接,ln -s命令創(chuàng)建軟鏈接试读。
2)目錄不能創(chuàng)建硬鏈接杠纵,并且硬鏈接不可以跨越分區(qū)系統(tǒng)。
3)目錄軟鏈接特別常用,并且軟鏈接支持跨越分區(qū)系統(tǒng)钩骇。
4)硬鏈接文件與源文件的inode相同比藻,軟鏈接文件與源文件inode不同。
5)刪除軟鏈接文件伊履,對源文件及硬鏈接文件無任何影響韩容。
6)刪除文件的硬鏈接文件,對源文件及鏈接文件無任何影響唐瀑。
7)刪除鏈接文件的源文件群凶,對硬鏈接無影響,會導致軟鏈接失效哄辣。
8)刪除源文件及其硬鏈接文件请梢,整個文件會被真正的刪除。
32.vim編輯器
普通模式:
1.命令光標跳轉
G #光標跳轉至末端
gg #光標跳轉至頂端
Ngg #光標跳轉至當前文件內(nèi)的N行
$ #光標跳轉至當前光標所在行的尾部
^|0 #光標跳轉至當前光標所在行的首部
-------------------------------------------
2.文件內(nèi)容較多
ctrl+f #往下翻頁(行比較多)
ctrl+b #往上翻頁
-------------------------------------------
3.復制與粘貼
yy #復制當前光標所在的行
5yy #復制當前光標以及光標向下4行
p #粘貼至當前光標下一行
-------------------------------------------
4.刪除力穗、剪貼毅弧、撤銷
dd #刪除當前光標所在的行
4dd #刪除當前光標所在的行以及往下的3行
dG #刪除當前光標以后的所有行
u #撤銷上一次的操作
-------------------------------------------
編輯模式:
i #進入編輯模式,光標不做任何操作
a #進入編輯模式当窗,將當前光標往后一位
o #進入編輯模式够坐,并在當前光標下添加一行空白內(nèi)容
-------------------------------------------
命令行模式:
1.文件保存與退出
:w 保存當前狀態(tài)
:w! 強制保存當前狀態(tài)
:q 退出當前文檔(文檔必須保存才能退出)
:q! 強制退出文檔不會修改當前內(nèi)容
:wq 先保存,在退出
:wq! 強制保存并退出
ZZ 保存退出, shfit+zz
:number 跳轉至對應的行號
-------------------------------------------
2.文件內(nèi)容查找
/string #需要搜索的內(nèi)容(查找)
n #按搜索到的內(nèi)容依次往下進行查找
N #按搜索到的內(nèi)容依次往上進行查找
-------------------------------------------
3.文件內(nèi)容替換
:1,5s#sbin#test#g #替換1-5行中包含sbin的內(nèi)容為test
:%s#sbin#test#g #替換整個文本文件中包含sbin的替換為test
:%s#sbin#test#gc #替換內(nèi)容時時提示是否需要替換
-------------------------------------------
4.文件內(nèi)容另存
:w /root/test.txt #將所有內(nèi)容另存為/root/test.txt文件中
-------------------------------------------
視圖模式:
ctrl+v 進入可視塊模式崖面,選中需要注釋的行
1.插入:按shift+i進入編輯模式,輸入#,結束按ESC鍵
2.刪除:選中內(nèi)容后元咙,按x或者d鍵刪除
3.替換:選中需要替換的內(nèi)容, 按下r鍵,然后輸入替換后的內(nèi)容
-------------------------------------------
shift+v 進入可視行模式,選中整行內(nèi)容
1.復制:選中行內(nèi)容后按y鍵及可復制巫员。
2.刪除:選中行內(nèi)容后按d鍵刪除
-------------------------------------------
VIM擴展
:set nu #顯示行號
:set ic #忽略大小寫, 在搜索的時候有用
:set list #顯示制表符(空行庶香、tab鍵)
-------------------------------------------
- vim環(huán)境變量設置
~/.vimrc 個人環(huán)境變量 /etc/vimrc 全局環(huán)境變量
# vim ~/.vimrc #當下次再打開文件自動顯示行號并忽略大小寫
set nu
set ic
#如果個人vim環(huán)境沒有配置, 則使用全局vim環(huán)境變量配置。
#如果個人vim環(huán)境和全局環(huán)境變量產(chǎn)生沖突, 優(yōu)先使用個人vim環(huán)境變量简识。
- 相同文件之間差異對比赶掖,通常用于對比修改前后差異
vimdiff #以vim方式打開兩個文件對比,高亮顯示不同的內(nèi)容
- 如果VIM非正常退出 (ctrl+z)掛起或強制退出終端沒關閉VIM后
# 假設打開filename文件被以外關閉七扰,需要刪除同文件名的.swp文件即可解決
# rm -f .filename.swp