1. 程式人生 > >linux經典 題庫(一)

linux經典 題庫(一)

awk   grep   sed

1.1創建一個目錄/data

1) 解答: mkdir /data


擴展:mkdir後面可以加參數-p,表示遞歸創建,也可以加參數-v,表示可以顯示出創建的過程。



1.2oldboy.txt增加內容為“I am studying linux.”

解答:為文件增加內容目前有三種方法:

1) 用echo的方式追加內容:echo "I am studyinglinux"> /data/oldboy.txt


2) 用vi編輯器追加內容:vi /data/oldboy.txt ##進入vi後輸入i,進入編輯模式,輸入要追加的內容,然後按esc鍵退出。


3) 用cat和eof配合輸出

cat >>/data/oldboy.txt <<eof

> I am studying enhlish

> eof



1.3已知文件test.txt內容為:

test

liyao

oldboy

請給出輸出test.txt文件內容時,不包含oldboy字符串的命令。

解答:此題有幾種解法

1) 利用head取文件的前兩行即可


2) 用grep過濾排除 ,-v參數表示反轉過濾。

grep -v"oldboy" test.txt



1.4只查看ett.txt文件(共100行)內第20到第30行的內容

解答:

1) 利用head和tail結合取

head -30 ett.txt |tail -11


2) 利用sed命令

sed -n"20,30p" ett.txt


3) 利用awk命令

awk"NR>=20&&NR<=30" ett.txt


1.5/oldboy目錄及其子目錄下所有以擴展名.sh結尾的文件中包含./hostlists.txt的字符串全部替換為../idctest_iplist

解答:

方法一find /root -type f -name "*.sh"|xargs sed -i ‘s#./hostlists.txt#../idctest_iplist #g‘

find用於查找文件,後面加參數-type 後面加文件類型,後面加要查詢的文件名,-name;sed和xargs聯合,加上sed is#替換前的內容#替換後的內容#g


方法二:利用變量函數

第一步:創建測試環境

[[email protected] oldboy]# cat 5.sh

I am studying Linux

I am a goog boy

[[email protected] oldboy]# cd.

-bash: cd.: command not found

[[email protected] oldboy]# cd ..

[[email protected] ~]# pwd

/root

第二步:先用find找到文件find /root/oldboy -type f-name ‘*.sh‘

[[email protected] ~]# sed ‘s#am#123456#g‘ $(find /root/oldboy -type f -name ‘*.sh‘)

466

I 123456 studying Linux

I 123456 a goog boy

466

第三步:利用sed‘s#替換前#替換後#g’先查看一下要別替換後的結果,$()的意思引用find的執行結果

[[email protected] ~]# sed ‘s#am#123456#g‘ $(find /root/oldboy -name ‘*.sh‘ -type f )

466

I 123456 studying Linux

I 123456 a goog boy

466

第四步:sed後面加上參數 -i ,實現最後替換。

[[email protected] ~]# sed ‘s#am#123456#g‘ -i $(find/root/oldboy -name ‘*.sh‘ -type f )

[[email protected] ~]# cat /root/oldboy/5.sh

I 123456 studying Linux

I 123456 a goog boy

[[email protected] ~]#

1.6如何查看/etc/services文件內容有多少行?

解答:

第一種:

[[email protected] oldboy]# cat -n/etc/services |tail -1

10774 iqobject 48619/udp # iqobject

[[email protected] oldboy]# ##所有可以查看行號的命令 + tail -1

第二種:

[[email protected] oldboy]# wc -l/etc/services

10774 /etc/services

生產場景中經常會通過wc -l計算服務進程個數等。然後通過腳本判斷進程個數來檢測服務是否正常,如進程數大於一個,說明進程還存在,服務一般就正常。

1.7如何過濾出已知當前目錄下oldboy中的所有一級目錄(提示:不包含oldboy目錄下面目錄的子目錄及隱藏目錄,即只能是一級目錄)

解答:

1.7.1 第一種方法(tree -Ld 1

[[email protected] data]# tree -Ld 1

.

├── niu

├── old

└── yu

3 directories

[[email protected] data]#

註:tree命令是以樹的結構顯示目錄結構,參數‘-L’ ,主要用於顯示目錄結構的層數;參數‘-d’ 用於顯示目錄的。

1.7.2第二種方法

[[email protected] data]# ls -l |grep ‘^d‘

drwxr-xr-x 2 root root 4096 Mar 16 03:08 niu

drwxr-xr-x 3 root root 4096 Mar 16 03:07 old

drwxr-xr-x 3 root root 4096 Mar 16 03:07 yu

[[email protected] data]#

註:此題用了grep與管道過濾查詢,先用ls -l查出所有的文件,其中包括文件和目錄,然後用到了通配符^,它後面加名字,表示以名字開頭的文件。

1.7.3第三種方法

[[email protected] data]#ll

total 16

-rw-r--r-- 1 root root 0 Mar 16 03:06 1

-rw-r--r-- 1 root root 0 Mar 16 03:06 2

-rw-r--r-- 1 root root 0 Mar 16 03:06 3

-rw-r--r-- 1 root root 0 Mar 16 03:06 44

-rw-r--r--. 1 root root 29 Mar 15 13:34 ett.txt

drwxr-xr-x 2 root root 4096 Mar 16 03:08 niu

drwxr-xr-x 3 root root 4096 Mar 16 03:07 old

drwxr-xr-x 3 root root 4096 Mar 16 03:07 yu

[[email protected] data]# ls-l |awk ‘$2>1‘

total 16

drwxr-xr-x 2 root root 4096 Mar 16 03:08 niu

drwxr-xr-x 3 root root 4096 Mar 16 03:07 old

drwxr-xr-x 3 root root 4096 Mar 16 03:07 yu

[[email protected] data]#

註:awk可以取列,利用變量$後面加列數可以實現,列如$2是取第二列,$6是取第六列。


1.7.4第四種方法

[[email protected] data]#find -maxdepth 1 -type d

.

./yu

./niu

./old

[[email protected] data]#

註:find是查詢命令中很重要的,它和maxdepth可以查詢目錄的最大深度數,

-type 後面跟文件的類型,一般有普通文件,用f表示,目錄文件用d表示。

-name 匹配某個名字

-name 不匹配某個名字

find後面的參數是有順序的,一般maxdepth放前面

[[email protected] data]# find -maxdepth 1 -type d !-name ‘.‘

-bash: !-name: event not found

[[email protected] data]# find -maxdepth 1 -type d !-name ‘.‘ ##註意!和後面的空格

-bash: !-name: event not found

[[email protected] data]# find-maxdepth 1 -type d ! -name ‘.‘

./yu

./niu

./old

[[email protected] data]#


1.7.5第五種方法

[[email protected] data]#ls -F|find -type d -name ‘$/‘

[[email protected] data]# ls -F

1 2 3 44 ett.txt niu/ old/ yu/

[[email protected] data]# ls -F|grep ‘$/‘

[[email protected] data]# ls-F|grep ‘/$‘

niu/

old/

yu/

[[email protected] data]#

註:find是搜索文件的,而grep一般用於搜索字母或文字的。ls -F 用來將文件末尾加上‘/‘

1.8一個目錄中有很多文件(ls -l查看時好多屏),想用一條命令最快速度查看到最近更新的文件。如何看?

解答:

[[email protected]~]# date

Thu Mar 16 05:28:51 CST 2017

[[email protected] ~]# mkdir oldboy2

[[email protected] ~]#ls -lrt

total 64

-rw-r--r--. 1 root root 5890 Mar 10 09:39 install.log.syslog

-rw-r--r--. 1 root root 21764 Mar 10 09:42install.log

-rw-------. 1 root root 1073 Mar 10 09:42 anaconda-ks.cfg

-rw-r--r--. 1 root root 201 Mar 12 20:45 oldboy.txt.bak

-rw-r--r--. 1 root root 0 Mar 12 21:07 file

-rw-r--r--. 1 root root 18 Mar 13 19:32 test.txt

drwxr-xr-x 2 root root 4096 Mar 16 00:31oldboy.txt

drwxr-xr-x 2 root root 4096 Mar 16 05:30oldboy2

drwxr-xr-x. 7 root root 4096 Mar 16 2017 oldboy

-rw-r--r--. 1 root root 10 Mar 16 2017 ett.txt

[[email protected] ~]#

1.9已知apache服務的訪問日誌按天記錄在服務器本地目錄/app/logs下,由於磁盤空間緊張,現在要求只能保留最近7天的訪問日誌!請問如何解決? 請給出解決辦法或配置或處理命令。(提示:可以從apache服務配置上著手,也可以從生成出來的日誌上著手。)

解答:第一步:

首先創建運行環境:

[[email protected] logs]#mkdir -p /app/logs2

[[email protected] app]# cd logs2/

[[email protected] logs2]# ll

total 0

[[email protected] logs2]#for time in {01..16};do date -s "201703$time"; touch access_www_$(date +%F).log ;done

Wed Mar 100:00:00 CST 2017

Thu Mar 200:00:00 CST 2017

Fri Mar 300:00:00 CST 2017

Sat Mar 400:00:00 CST 2017

Sun Mar 500:00:00 CST 2017

Mon Mar 600:00:00 CST 2017

Tue Mar 700:00:00 CST 2017

Wed Mar 800:00:00 CST 2017

Thu Mar 900:00:00 CST 2017

Fri Mar 10 00:00:00 CST 2017

Sat Mar 11 00:00:00 CST 2017

Sun Mar 12 00:00:00 CST 2017

Mon Mar 13 00:00:00 CST 2017

Tue Mar 14 00:00:00 CST 2017

Wed Mar 15 00:00:00 CST 2017

Thu Mar 16 00:00:00 CST 2017

[[email protected] logs2]# date-s "20170316"

Thu Mar 16 00:00:00 CST 2017

第二步:先找到日誌文件

[[email protected] logs2]# find-type f -name ‘*.log‘

./access_www_2017-03-04.log

./access_www_2017-03-02.log

./access_www_2017-03-03.log

./access_www_2017-03-01.log

./access_www_2017-03-09.log

./access_www_2017-03-07.log

./access_www_2017-03-16.log

./access_www_2017-03-10.log

./access_www_2017-03-05.log

./access_www_2017-03-15.log

./access_www_2017-03-11.log

./access_www_2017-03-08.log

./access_www_2017-03-12.log

./access_www_2017-03-13.log

./access_www_2017-03-06.log

./access_www_2017-03-14.log

第三步:使用find的參數-mtime ,查找某段時間前,然後利用管道查看出來。

[[email protected] logs2]# find-type f -name ‘*.log‘ -mtime +7 |xargs ls -l

-rw-r--r-- 1 root root 0 Mar 1 00:00 ./access_www_2017-03-01.log

-rw-r--r-- 1 root root 0 Mar 2 00:00 ./access_www_2017-03-02.log

-rw-r--r-- 1 root root 0 Mar 3 00:00 ./access_www_2017-03-03.log

-rw-r--r-- 1 root root 0 Mar 4 00:00 ./access_www_2017-03-04.log

-rw-r--r-- 1 root root 0 Mar 5 00:00 ./access_www_2017-03-05.log

-rw-r--r-- 1 root root 0 Mar 6 00:00 ./access_www_2017-03-06.log

-rw-r--r-- 1 root root 0 Mar 7 00:00 ./access_www_2017-03-07.log

-rw-r--r-- 1 root root 0 Mar 8 00:00 ./access_www_2017-03-08.log

[[email protected] logs2]#

第四步:最後一步刪掉篩選的7天前的日誌文件。

[[email protected] logs2]# find-type f -name ‘*.log‘ -mtime +7 |xargs rm -fr

[[email protected] logs2]# ll

total 0

-rw-r--r-- 1 root root 0 Mar 9 00:00 access_www_2017-03-09.log

-rw-r--r-- 1 root root 0 Mar 10 00:00access_www_2017-03-10.log

-rw-r--r-- 1 root root 0 Mar 11 00:00access_www_2017-03-11.log

-rw-r--r-- 1 root root 0 Mar 12 00:00access_www_2017-03-12.log

-rw-r--r-- 1 root root 0 Mar 13 00:00access_www_2017-03-13.log

-rw-r--r-- 1 root root 0 Mar 14 00:00access_www_2017-03-14.log

-rw-r--r-- 1 root root 0 Mar 15 00:00access_www_2017-03-15.log

-rw-r--r-- 1 root root 0 Mar 16 00:00access_www_2017-03-16.log

[[email protected] logs2]#

1.10打印輕量級web服務的配置文件nginx.conf內容的行號及內容,該如何做?

解答:

1.10.1第一種方法:nl nginx.conf

[[email protected] /]# nlnginx.conf

1 stu1

2 stu2

3 stu3

4 stu4

5 stu5

[[email protected] /]#

1.10.2第二種方法:vi nginx.conf 後使用set nu來呈現行號,set nonu來取消行號

[[email protected] /]# vi nginx.conf

1 stu1

2 stu2

3 stu3

4 stu4

5 stu5

~

:set nu

1.10.3第三種方法:cat -n nginx.conf

[[email protected] /]# cat-n nginx.conf

1 stu1

2 stu2

3 stu3

4 stu4

5 stu5

[[email protected] /]#

註:

1.10.4第四種方法:awk ‘{print NR,$0}‘ nginx.conf

[[email protected] /]#awk ‘{print NR,$0}‘ nginx.conf

1 stu1

2 stu2

3 stu3

4 stu4

5 stu5

[[email protected] /]#

註:print 表示打印,NR表示行,$0表示整行

1.10.5第五種方法

[[email protected] /]# grep-n ‘.‘ nginx.conf

1:stu1

2:stu2

3:stu3

4:stu4

5:stu5

[[email protected] /]#

註:-n表示將每一行前面加行號,‘.’表示匹配所有

1.10.6第六種方法

[[email protected] /]# sed= nginx.conf |xargs -n2

1 stu1

2 stu2

3 stu3

4 stu4

5 stu5

[[email protected] /]#

註:sed後面的等號前後有空格,否則搜不出,

1.11/etc/目錄為linux系統的默認的配置文件及服務啟動命令的目錄

a.請用tar打包/etc整個目錄(打包及壓縮)

b.請用tar打包/etc整個目錄(打包及壓縮,但需要排除/etc/services文件)

c.請把a點命令的壓縮包,解壓到/tmp指定目錄下(最好只用tar命令實現)

解答:

A 解答

第一步:打包先進入要打包的目錄下。

[[email protected] etc]#cd /etc/

[[email protected] etc]# tarzcf /mnt/etc.tar.gz /etc/

tar: Removing leading `/‘ from member names

tar: Removing leading `/‘ from hard link targets

[[email protected] etc]# ls /mnt/

8 data etc.tar.gz ett.txt oldboy test.txt

[[email protected] etc]#

參數解析:tar後面的參數z表示以軟件gzip壓縮文件,c(create)創建壓縮包,f(file)指定壓縮包名字。

B

解答:

第一種方法

[[email protected] etc]#tar zcf /mnt/paichu.tar.gz /etc/ --exclude=etc/services

tar: Removing leading `/‘ from member names

tar: Removing leading `/‘ from hard link targets

[[email protected] etc]# ls /mnt/

8 data etc.tar.gz ett.txt oldboy paichu.tar.gz test.txt

[[email protected] etc]#

創建成功後,查看是否排除成功。

[[email protected] etc]# ls /mnt/

8 data etc.tar.gz ett.txt oldboy paichu.tar.gz test.txt

[[email protected] etc]#tar tf /mnt/paichu.tar.gz |grep services

etc/init/readahead-disable-services.conf

etc/etc/init/readahead-disable-services.conf

[[email protected] etc]#

查看打包的包內容,可以用tart(list)f(file) 包名字。從上面查看可以看出來查出來的文件沒有/etc/services的文件。

--exclude=後面加上要排除打包的路徑,最好是相對路徑。


第二種方法此方法用於排除一個或多個文件。

首先可以先將要排除的文件寫入一個文件中,然後再排除。

[[email protected] etc]# vi/mnt/paichu.txt

etc/services

etc/ssh

[[email protected] etc]# tar zcf /mnt/paichu1.tar.gz /etc/--exclude-from=/mnt/paichu.txt

tar: Removing leading `/‘from member names

tar: Removing leading `/‘from hard link targets

[[email protected] etc]# ls /mnt/

8 etc.tar.gz oldboy paichu.tar.gz test.txt

data ett.txt paichu1.tar.gz paichu.txt

[[email protected] etc]# tar tf /mnt/paichu1.tar.gz |grep services

etc/init/readahead-disable-services.conf

etc/etc/init/readahead-disable-services.conf


第三種方法:利用花括號{}裏面寫上要排除打包的文件名。

[[email protected] etc]#tar zcf /mnt/paichu3.tar.gz /etc/ --exclude={ntp,pm}

tar: Removing leading `/‘ from member names

tar: Removing leading `/‘ from hard link targets

[[email protected] etc]# tar tf /mnt/paichu3.tar.gz |grepntp

etc/dhcp/dhclient.d/ntp.sh

etc/ntp.conf

etc/X11/fontpath.d/

etc/rc.d/rc1.d/K74ntpd

1.12已知如下命令及結果:

[[email protected] ~]$echo "I am oldboy,myqq is 31333741">>oldboy.txt

[[email protected] ~]$ catoldboy.txt

I am oldboy,myqq is31333741

現在需要從文件中過濾出oldboy31333741字符串,請給出命令.

解答:

第一種方法:先創建測試環境

此方法是先用cut命令指定空格為分隔符,將內容分為5份,然後用sed將‘,myqq‘ 這個多余的內容取出掉。

[[email protected] data]# cat oldboy.txt

[[email protected] data]# echo "I am oldboy,myqq is31333741">>oldboy.txt

[[email protected] data]# cat oldboy.txt

I am oldboy,myqq is 31333741

[[email protected] data]#cut -d ‘ ‘ -f3,5 oldboy.txt

oldboy,myqq 31333741

[[email protected] data]# cut-d ‘ ‘ -f3,5 oldboy.txt |sed ‘s#,myqq##g‘

oldboy 31333741

[[email protected] data]#

註意:cut是截取列,

-d, --delimiter=DELIM 加上參數d表示指定分割符,後面跟上單引號分隔符,-f 表示fields表示指定列名。


第二種方法:此方法是先用sed命令將內容中唯一的逗號替換掉(只是不顯示出來),然後只剩下空格,這時候再用cut 命令分割。

[[email protected] data]# cat oldboy.txt

I am oldboy,myqq is 31333741

[[email protected] data]# sed ‘s#,# #g‘ oldboy.txt

I am oldboy myqq is 31333741

[[email protected] data]# sed‘s#,# #g‘ oldboy.txt |cut -d ‘ ‘ -f3,6

oldboy 31333741

[[email protected] data]#


第三種方法:此方法是用awk先搜索出第三列和第五列,然後用sed將“.myqq”替換成空。

[[email protected] data]# cat oldboy.txt

I am oldboy,myqq is 31333741

[[email protected] data]# awk ‘{print $3,$5}‘ oldboy.txt

oldboy,myqq 31333741

[[email protected] data]# awk‘{print $3,$5}‘ oldboy.txt |sed ‘s#,myqq##g‘

oldboy 31333741

[[email protected] data]#

awk 默認分隔符是空格,如果想指定某個分割符,可以加上F,後面加上指定的分隔符。


第四種方法:利用awk的F參數指定分隔符

[[email protected] data]# awk -F ‘[ ,]‘ -f3,6 oldboy.txt

awk: fatal: can‘t open source file `3,6‘ forreading (No such file or directory)

[[email protected] data]# awk-F ‘[ ,]‘ ‘{print $3,$6}‘ oldboy.txt

oldboy 31333741

[[email protected] data]#

第五種方法:使用cut的參數c來去字符,空格與逗號都算一個字符。取得列前面不用加f

[[email protected] data]# cut -c 6-11,20- oldboy.txt

oldboy 31333741

[[email protected] data]#


第六種方法:此種方法是用先將這兩句話換行成兩行,然後取最後一行。

[[email protected] data]# awk-F ‘[,]‘ ‘{print $1"\n"$2}‘ oldboy.txt 此處的F後面的‘[,]‘ 可以用,代替。

I am oldboy

myqq is 31333741

[[email protected] data]# awk-F, ‘{print $1"\n"$2}‘ oldboy.txt |awk ‘{print $NF}‘

oldboy

31333741

[[email protected] data]#

第七種方法:利用tr命令,它是精簡版的sed。

[[email protected] data]# tr ‘,‘ "\n" <oldboy.txt

I am oldboy

myqq is 31333741

[[email protected] data]# tr ‘,‘ "\n" <oldboy.txt |awk ‘{printNF}‘

3

3

[[email protected] data]# tr ‘,‘ "\n" <oldboy.txt |awk ‘{print$NF}‘

oldboy

31333741

[[email protected] data]#

註:tr 後面的,表示指定逗號為分隔符,後面的\n表示搜索到以後從分隔符後換行。

1.13過濾出/etc/services 文件包含33061521兩數據庫端口的行的內容。

##使用grep過濾

[[email protected] etc]# grep -E ‘1521|3306‘ services |在引號中是或者的意思,不加單引號就是管道

mysql 3306/tcp # MySQL

mysql 3306/udp # MySQL

ncube-lm 1521/tcp # nCube License Manager

ncube-lm 1521/udp # nCube License Manager

[[email protected] etc]#

##使用AWK過濾

[[email protected] tmp]# awk ‘/1521|3306/‘ /etc/services

mysql 3306/tcp # MySQL

mysql 3306/udp # MySQL

ncube-lm 1521/tcp # nCube License Manager

ncube-lm 1521/udp # nCubeLicense Manager

##使用sed過濾

[[email protected] tmp]# sed -nr ‘/1521|3306/‘ /etc/services

sed: -e expression #1, char 11: missing command

[[email protected] tmp]# sed -nr ‘/1521|3306/p‘ /etc/services

mysql 3306/tcp # MySQL

mysql 3306/udp # MySQL

ncube-lm 1521/tcp # nCube License Manager

ncube-lm 1521/udp # nCube License Manager

[[email protected] tmp]#


linux經典 題庫(一)