老男孩linux運維第一次測試題

1.1 我想在/data/oldboyedu目錄下面創建一個oldboy.txt文件

[root@oldboyedu~]# cd /data/oldboyedu
-bash: cd:/data/oldboyedu: No such file or directory

1.爲何出現這樣的錯誤

答:沒有/data目錄或者沒有/data/oldboyedu/目錄

2.如何解決這個錯誤呢?

[root@oldboyedu-36-02~]# mkdir -p /data/oldboyedu
[root@oldboyedu-36-02~]# cd /data/oldboyedu
[root@oldboyedu-36-02oldboyedu]# touch oldboy.txt

1.2 向oldboy.txt加入內容"I love studyingLinux." (不少於2種方法)

法一:
[root@oldboyedu-36-02oldboyedu]# echo 'I love studying Linux.' >oldboy.txt
[root@oldboyedu-36-02oldboyedu]# cat oldboy.txt
I lovestudying Linux.
法二:
[root@oldboyedu-36-02oldboyedu]# cat >oldboy.txt
I lovestudying linux.
^C
[root@oldboyedu-36-02oldboyedu]# cat oldboy.txt
I lovestudying linux.
法三:
[root@oldboyedu-36-02oldboyedu]# vi oldboy.txt--a/i--esc--:wq
法四:
[root@oldboyedu-36-02~]# cat >>oldboy.txt<<EOF
> I lovestudying linux.
> EOF

1.3 把/data 目錄複製到/tmp目錄下

[root@oldboyedu-36-02oldboyedu]# cp -r /data /tmp/
[root@oldboyedu-36-02oldboyedu]# ls /tmp
9.txt  b.txt data  yum.log  z.txt
[root@oldboyedu-36-02oldboyedu]# cp -a /data /tmp/
cp:overwrite `/tmp/data/oldboyedu/oldboy.txt'? y
[root@oldboyedu-36-02oldboyedu]# ls /tmp/
9.txt  b.txt data  yum.log  z.txt

1.4 說說這些特殊符號含義:  >> >  2> 2>>   #(井號) .(點) ..(兩個點)

>>:1>>,追加標準輸出重定向,不清除內容,新加內容到最後一行;
>:1>,標準輸出重定向,清除舊內容,加入新內容;
2>:錯誤輸出重定向;
2>>:錯誤追加輸出重定向;
#:1)註釋;2)代表root用戶;
.:./,當前目錄;
..:../ ,當前目錄的上級目錄;


1.5 test.txt內容爲:

trainning
fanbingbing
lidao

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

創建環境:
法一:
[root@oldboyedu-36-02oldboyedu]# cat >>test.txt<<eof
>training
>fanbingbing
> lidao
> eof
法二:
[root@oldboyedu-36-02oldboyedu]# echo "training
fanbingbing
lidao">test.txt
[root@oldboyedu-36-02oldboyedu]# cat test.txt
training
fanbingbing
lidao
不包含trainning字符串的命令
法一:
[root@oldboyedu-36-02oldboyedu]# sed -n '2,3p' test.txt
fanbingbing
lidao
法二:
[root@oldboyedu-36-02oldboyedu]# head -3 test.txt|tail -2
fanbingbing
lidao
法三:
[root@oldboyedu-36-02oldboyedu]# tail -3 test.txt|grep -v training
fanbingbing
lidao
法四:
[root@oldboyedu-36-02oldboyedu]# grep -v 'training' test.txt
fanbingbing
lidao
法五:
[root@oldboyedu-36-02oldboyedu]# awk 'NR==2,NR==3' test.txt
fanbingbing
lidao
[root@oldboyedu-36-02oldboyedu]# awk '{if(NR>=2&&NR<=3)print $0"\n"}'test.txt
fanbingbing
 
lidao
法六:
[root@oldboyedu-36-02~]# awk '{if(NR==2||NR==3) print $0"\n"}' test.txt
fanbingbing
 
lidao
注意:
[root@oldboyedu-36-02 ~]# awk'{if(NR>=2||NR<=3) print $0"\n"}' test.txt
training
 
fanbingbing
 
lidao

1.6 入職新公司,老大讓你在服務器上限制rm命令,當用戶輸入rm 命令時候提示”rm command is not allowed touse.” 請問實現的步驟是?。

臨時修改:
[root@oldboyedu-36-02~]# alias rm='echo "alias rm=rm command is not allowed to use"'
[root@oldboyedu-36-02~]# alias rm
aliasrm='echo "alias rm=rm command is not allowed to use"'
[root@oldboyedu-36-02~]# rm -f 1.txt
alias rm=rmcommand is not allowed to use -f 1.txt
永久生效:
1)alias rm='echo rm command is not allowed to use'(臨時生效)
2)用vim將1)的內容寫入到/etc/profile文件的最後一行
3)用source /etc/profile命令使文件永久生效
4)去/root/.bashrc下將alias rm='rm -i'註釋掉即#‘alias rm='rm -i'’

1.7 取出文件ett.txt 的第30到40行的內容。

注:ett.txt由seq 20 120 >ett.txt創建

創建模擬環境:
[root@oldboyedu-36-02~]# seq 20 120 >ett.txt
法一:
[root@oldboyedu-36-02~]# head -40 ett.txt|tail -11
法二:
tail -72ett.txt|head -11
法三:
sed -n'30,40p' ett.txt
法四:
awk'NR==30,NR==40' ett.txt
awk'NR>=30&&NR<=40' ett.txt
法五:
awk'{if(NR>=30&&NR<=40) print $0"\n"}' ett.txt
注意:(取不出來)
[root@oldboyedu-36-02 ~]# awk'{if(NR==30||NR==40) print $0"\n"}' ett.txt
49
 
59

1.8 把test.txt文件中的trainning修改爲oldboy.

法一:
[root@oldboyedu-36-02~]# sed -i 's#training#oldboy#g' test.txt
[root@oldboyedu-36-02~]# cat test.txt
oldboy
fanbingbing
lidao
法二:vi/vim test.txt --a/i--insert--esc--:wq

1.9 查找出/data目錄下所有以.txt結尾的文件,並且把文件中的trainning修改爲oldboy.

[root@oldboyedu-36-02~]# find /data/ -type f -name "*.txt" |xargs sed -i's#training#oldboy#g'
[root@oldboyedu-36-02~]# find /data/ -type f -name "*.txt" -exec sed -i's#training#oldboy#g' {} \;
[root@oldboyedu-36-02~]# sed -i 's#training#oldboy#g' `find /data/ -type f -name "*.txt"`
[root@oldboyedu-36-02~]# sed -i 's#training#oldboy#g' $(find /data/ -type f -name "*.txt")

1.10 查找/oldboy下

所有7天以前以log結尾的大於1M的文件複製到/tmp下。

生成環境:(所用命令:dd if=/dev/zero of=1.log bs=1M count=3;touch-m ;date -s ;find cp -t ;du -sh)
[root@oldboyedu36-01 data]# date -s 20170501
Mon May  1 00:00:00 CST 2017
[root@oldboyedu36-01 data]# dd if=/dev/zero of=1.log bs=1M count=3
3+0 records in
3+0 records out
3145728 bytes (3.1 MB) copied, 0.00374874 s, 839 MB/s
[root@oldboyedu36-01 data]# du -sh 1.log
3.0M    1.log
[root@oldboyedu36-01 data]# touch -m {1..6}.log
[root@oldboyedu36-01 data]# date -s 20170509
Tue May  9 00:00:00 CST 2017

解題方法
法一:
[root@oldboyedu36-01 data]# find ./ -type f -name "?.log"-mtime +7 -size +1M |xargs cp -t /tmp/
[root@oldboyedu36-01data]# ll -h /tmp/
total 3072
-rw-r--r--. 1root root 3.1M May  9 00:02 1.log
[root@oldboyedu36-01data]# du -sh /tmp/
3.1M    /tmp/
法二:
[root@oldboyedu36-01data]# find ./ -type f -name "?.log" -mtime +7 -size +1M |xargs -i cp{}  /tmp/
法三:
[root@oldboyedu36-01data]# find ./ -type f -name "?.log" -mtime +7 -size +1M -exec cp {} /tmp/ \;
法四:
[root@oldboyedu36-01data]# cp `find ./-type f -name "?.log" -mtime +7 -size +1M` /tmp/ ===反引號
法五:
[root@oldboyedu36-01data]# cp $(find ./ -type f -name "?.log" -mtime +7 -size +1M |xargs)/tmp/

1.11 請描述buffer和cache的區別(附加題)?

 

答:
buffer:把數據寫入內存,這時寫入數據的內存空間稱爲緩衝區,簡稱緩衝;
cache:從內存中讀取數據,這時讀取數據的內存空間稱爲緩存區,簡稱緩存;
 總結:寫緩衝,讀緩存。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章