1. 程式人生 > >day4、Linux基礎題目

day4、Linux基礎題目

不清楚 讀取 命令 所有 不同 追加 讀取數據 特殊符號 exe

第一題

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

[[email protected] ~]# cd /data/oldboyedu

-bash: cd: /data/oldboyedu: No such file or directory

1.為何出現這樣的錯誤

因為沒有這個目錄

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

創建這兩個目錄:mkdir -p /data/da

第二題

接上題,向 da.txt 加入內容 "I love studying Linux." (不少於 2 種方法)

方法一

命令格式:echo "I love studying Linux." >> /data/da.txt

[[email protected] ~]# echo "I love studying Linux." >> /data/da.txt

[[email protected] ~]# cat /data/da.txt

I love studying Linux.

方法二

命令格式:cat >/data/da.txt <<EOF

> I love studying Linux.

> EOF

[[email protected] ~]# cat >/data/da.txt <<EOF

> I love studying Linux.

> EOF

[[email protected] ~]# cat /data/da.txt

I love studying Linux.

第三題

把/data 目錄復制到 /tmp 目錄下

命令格式:cp -r /data/ /tmp/

[[email protected] ~]# cp -r /data/ /tmp/

第四題

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

>:標準輸出重定向,先清除文件內容,再將新內容放入文件。

>>:追加輸出重定向,不清除文件內容,將新內容放入文件末尾。

2>:錯誤輸出重定向,先清除文件內容,再將命令錯誤的提示放入文件。

2>>:錯誤追加輸出重定向,不清楚文件內容,將命令錯誤的提示追加到文件末尾。

#(號):Linux中的註釋符號,

.(點):表示當前路徑。

..(點點):表示上一層路徑。

第五題

test.txt 內容為:

trainning

fanbingbing

lidao

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

方法一

命令格式: tail -2 /test.txt

[[email protected] ~]# tail -2 /test.txt

fanbingbing

lidao

方法二

命令格式:sed ‘/trainning/d‘ /test.txt

[[email protected] ~]# sed ‘/trainning/d‘ /test.txt

fanbingbing

lidao

方法三

命令格式:grep -v " trainning " /test.txt

[[email protected] ~]# grep -v " trainning " /test.txt

fanbingbing

lidao

方法四

命令格式:awk ‘!/trainning/‘ test.txt

[[email protected] ~]# awk ‘!/trainning/‘ test.txt

fanbingbing

lidao

第六題

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

第一步:

命令格式:alias rm=‘echo rm command is not allowed to use.‘

[[email protected] ~]# alias rm=‘echo rm command is not allowed to use.‘

[[email protected] ~]# rm test.txt

rm command is not allowed to use. test.txt

第二步:寫入 /etc/profile 配置文件

[[email protected] ~]# echo "alias rm=‘echo rm command is not allowed to use.‘" >> /etc/profile

第三步:生效

[[email protected] ~]# source /etc/profile

第七題

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

方法一

命令格式:sed -n ‘30,40p‘ /ett.txt

[[email protected] ~]# sed -n ‘30,40p‘ /ett.txt

30

31

32

33

34

35

36

37

38

39

40

方法二

命令格式:awk ‘NR==30,NR==40‘ /ett.txt

[[email protected] ~]# awk ‘NR==30,NR==40‘ /ett.txt

30

31

32

33

34

35

36

37

38

39

40

方法三

命令格式:head -40 /ett.txt |tail -11

[[email protected] ~]# head -40 /ett.txt |tail -11

30

31

32

33

34

35

36

37

38

39

40

第八題

把 test.txt 文件中的 trainning 修改為 lll.

方法一

命令格式:find /data/ -type f -name "test.txt" |xargs sed -i ‘s#trainning#lll#g‘

[[email protected] ~]# find /data/ -type f -name "test.txt" |xargs sed ‘s#trainning#lll#g‘

lll

fanbingbing

lidao

[[email protected] ~]# find /data/ -type f -name "test.txt" |xargs sed -i ‘s#trainning#lll#g‘

方法二

命令格式:sed -i ‘s#trainning#lll#g‘ $(find /data/ -type f -name "test.txt")

[[email protected] ~]# sed ‘s#trainning#lll#g‘ $(find /data/ -type f -name "test.txt")

lll

fanbingbing

lidao

[[email protected] ~]# sed -i ‘s#trainning#lll#g‘ $(find /data/ -type f -name "test.txt")

方法三

命令格式:find /data/ -type f -name "test.txt" -exec sed -i ‘s#trainning#lll#g‘ {} \;

[[email protected] ~]# find /data/ -type f -name "test.txt" -exec sed -i ‘s#trainning#lll#g‘ {} \;

[[email protected] ~]# find /data/ -type f -name "test.txt" -exec sed ‘s#trainning#lll#g‘ {} \;

lll

fanbingbing

lidao

第九題

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

命令格式:

find /data/ -type f -name "*.txt" |xargs sed -i ‘s#trainning#lll#g‘

sed -i ‘s#trainning#lll#g‘ $(find /data/ -type f -name "*.txt")

find /data/ -type f -name "*.txt" -exec sed -i ‘s#trainning#lll#g‘ {} \;

第十題

查找/data 下所有以 log 結尾的大於 1M 的文件復制到/tmp 下。

命令格式:find /data -type f -name "*.log" -size +1M

方法1

cp $(find /data -type f -name "*.log" -size +1M ) /tmp

方法2

find /data -type f -name "*.log" -size +1M |xargs cp -t /tmp

方法3

find /data -type f -name "*.log" -size +1M |xargs -i cp {} /tmp

方法4

find /data -type f -name "*.log" -size +1M -exec cp {} /tmp \;

第十一題

什麽是 linux 的運行級別,請描述 linux 的運行級別不同數字的含義?(附加題)

  1. 運行級別0-6

0表示關機

1表示單用戶

2 表示多用戶但是沒有NFS

3 表示完整的多用戶狀態 命令行模式 命令模式

4 沒有使用

5 圖形化界面模式

6 重啟

  1. 如何查看運行級別

runlevel命令

  1. 如何配置運行級別

/etc/inittab文件

  1. 臨時修改init命令

init 5

第十二題

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

buffer:緩沖區,寫buffer 數據寫入到內存中的緩沖區

cache:緩存區,讀cache 從內存中的緩存區讀取數據

day4、Linux基礎題目