四劍客第九關

liu4356發表於2024-03-16

四劍客面試真題-3

1、建立目錄 /data/it , 並且在該目錄下建立目錄檔案 it.txt , 然後在檔案 it.txt 裡寫入內容
inet addr:10.0.0.8 Bcast:10.0.0.25 Mask:255.255.255.255

mkdir -p /data/it
echo "inet addr:10.0.0.8 Bcast:10.0.0.25 Mask:255.225.225.255" > /data/it/it.txt







實際效果演示

[root@master ~]# mkdir /data/it
[root@master ~]# echo "inet addr:10.0.0.8 Bcast:10.0.0.25 Mask:255.225.225.255" > /data/it/it.txt
[root@master ~]# cd /data/it
[root@master it]# ls
it.txt
[root@master it]# cat it.txt 
inet addr:10.0.0.8 Bcast:10.0.0.25 Mask:255.225.225.255

2、將題1中的 it.txt 檔案內容透過命令過濾只輸出如下內容

10.0.0.8 10.0.0.255 255.255.255.0

at it.txt | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | while read -r line; do echo $line; done


cat it.txt | grep -oE 'inet addr:([0-9]{1,3}\.){3}[0-9]{1,3}|Bcast:([0-9]{1,3}\.){3}[0-9]{1,3}|Mask:([0-9]{1,3}\.){3}[0-9]{1,3}' | cut -d: -f2 | tr '\n' ' '

grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' it.txt






實際效果演示

root@master it]# cat it.txt | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | while read -r line; do echo $line; done
10.0.0.8
10.0.0.25
255.225.225.255
[root@master it]# cat it.txt | grep -oE 'inet addr:([0-9]{1,3}\.){3}[0-9]{1,3}|Bcast:([0-9]{1,3}\.){3}[0-9]{1,3}|Mask:([0-9]{1,3}\.){3}[0-9]{1,3}' | cut -d: -f2 | tr '\n' ' '
10.0.0.8 10.0.0.25 255.225.225.255

[root@master it]# grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' it.txt
10.0.0.8
10.0.0.25
255.225.225.255


3 將題1 中的 it 目錄移動到 /tmp 目錄下,並將 /etc/passwd 檔案複製到 /tmp/passwd

mv /data/it /tmp
cp /etc/passwd /tmp/passwd





實際效果演示
[root@master ~]# mv /data/it /tmp
[root@master ~]# cp /etc/passwd /tmp/passwd


4、在題目3的基礎上使用awk 取 passwd 檔案的第6行到20行的第三列重定向列到 /tmp/test.txt 檔案裡?

awk 'NR>=6 && NR<=20 {print $3}' /tmp/passwd > /tmp/test.txt







實際效果演示


[root@master ~]# awk 'NR>=6 && NR<=20 {print $3}' /tmp/passwd > /tmp/test.txt
[root@master ~]# cat /tmp/test
test01.txt  test.txt    
[root@master ~]# cat /tmp/test.txt 








Stack:/var/lib/avahi-autoipd:/sbin/nologin
bus:/:/sbin/nologin
polkitd:/:/sbin/nologin
by






5 在題3 的基礎上要求用命令 rm 刪除檔案時提示如下禁止使用 rm 的提示,並使該效果永久生效

echo 'alias rm="echo \"警告:禁止使用rm命令直接刪除檔案。請使用更安全的方法刪除檔案。\" 1>&2; false"' >> ~/.bashrc  
source ~/.bashrc






實際效果演示

[root@master ~]# echo 'alias rm="echo \"警告:禁止使用rm命令直接刪除檔案。請使用更安全的方法刪除檔案。\" 1>&2; false"' >> ~/.bashrc  
[root@master ~]# source ~/.bashrc
[root@master ~]# rm
警告:禁止使用rm命令直接刪除檔案。請使用更安全的方法刪除檔案。
[root@master ~]# reboot
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(centos7.4) at 21:29:25.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

Connecting to 10.0.1.134:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sun Mar 17 05:24:34 2024 from 10.0.1.1
**************
** 生產伺服器,請慎重操作並慎用rm命令! **
**************
sh: /root/sh/1.sh: No such file or directory
[root@master ~]# rm
警告:禁止使用rm命令直接刪除檔案。請使用更安全的方法刪除檔案。



6、在題目3的基礎上,刪除/tmp/ 下passwd 以外的其他檔案

find /tmp -maxdepth 1 -type f ! -name passwd -delete

find /tmp -type f ! -name passwd |xargs rm







實際效果演示


[root@master ~]# find /tmp -maxdepth 1 -type f ! -name passwd -delete
[root@master ~]# cat /tmp/
.font-unix/
.ICE-unix/
inode_fill_test/
it/
passwd
systemd-private-26f048676a1b4e55a466d3fa2e72e8b2-httpd.service-TDwMjT/
test.txt
.Test-unix/
.X11-unix/
.XIM-unix/

[root@master ~]# find /tmp -type f ! -name passwd |xargs rm
[root@master ~]# cat /tmp/
.font-unix/
.ICE-unix/
inode_fill_test/
it/
passwd
systemd-private-26f048676a1b4e55a466d3fa2e72e8b2-httpd.service-TDwMjT/
test.txt


7 在題目3的基礎上,請列印 /etc/passwd 檔案中的第2~5 行

sed -n '2,5p' /etc/passwd





實際效果演示

[root@master ~]# sed -n '2,5p' /etc/passwd
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

8、在題目3的基礎上,使用命令調換 passwd 檔案裡 root 位置和 /bin/bash 位置?即將所有的第一列和最後一列位置調換

題目意思 把/tmp/passwd 這個檔案的第一行和最後一行位置互換

awk -F: '{OFS=":"; print $NF, $1, $2, $3, $4, $5, %6}' /tmp/passwd > /tmp/new_passwd







命令註解


awk: 這是文字處理工具的名稱,它允許你基於模式掃描和處理文字。

-F:: 這個選項設定輸入欄位分隔符為冒號(:)。這意味著awk會將每一行分割成多個欄位,每個欄位由冒號分隔。

'{t=$1; $1=$NF; $NF=t; print}': 這是awk要執行的命令序列,放在大括號{}內。

t=$1: 將第一個欄位(即使用者名稱,如root)的值賦給變數t。
$1=$NF: 將最後一個欄位(即登入shell,如/bin/bash)的值賦給第一個欄位。
$NF=t: 將變數t(原先第一個欄位的值)賦給最後一個欄位。
print: 列印修改後的整行。
OFS=:: 這個選項設定輸出欄位分隔符為冒號(:)。這確保了當awk列印行時,欄位之間由冒號分隔,從而保持與原始檔案格式的一致性。

/tmp/passwd: 這是awk命令要處理的輸入檔案。在這個例子中,它被假定為/tmp/passwd,一個包含/etc/passwd檔案內容的臨時副本。

> /tmp/passwd.new: 這將awk命令的輸出重定向到一個新的檔案/tmp/passwd.new。

&&: 這是一個shell運算子,它表示只有當左邊的命令(即awk命令)成功執行時,才會執行右邊的命令。

mv /tmp/passwd.new /tmp/passwd: 如果awk命令成功執行並建立了/tmp/passwd.new檔案,這個mv命令將把新檔案重新命名為/tmp/passwd,從而替換原來的檔案。

整體來說,這個命令的作用是將/tmp/passwd檔案中的每一行的第一個欄位(通常是使用者名稱)和最後一個欄位(通常是登入shell)互換位置,並將結果儲存到同一個檔案(透過先寫入新檔案然後重新命名的方式)。

注意:這個命令會覆蓋/tmp/passwd檔案的內容,因此在執行之前請確保你已經備份了原始檔案。此外,這個命令並沒有處理特殊情況,比如檔案中只有一行或者行的欄位數量不等於七個,因此在應用到生產環境之前需要更多的錯誤處理和驗證。









實際效果演示

[root@master ~]# awk -F: '{t=$1; $1=$NF; $NF=t; print}' OFS=: /tmp/passwd > /tmp/passwd.new && mv /tmp/passwd.new /tmp/passwd
mv: overwrite ‘/tmp/passwd’? y
[root@master ~]# cat /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
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
wang:x:1000:1000:wang:/home/wang:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
it101:x:1001:1001::/home/it101:/bin/bash
it1:x:1002:1002::/home/it1:/bin/bash
it2:x:1003:1003::/home/it2:/bin/bash
it3:x:1004:1004::/home/it3:/bin/bash
it4:x:1005:1005::/home/it4:/bin/bash
it5:x:1006:1006::/home/it5:/bin/bash
it6:x:1007:1007::/home/it6:/bin/bash
it7:x:1008:1008::/home/it7:/bin/bash
it8:x:1009:1009::/home/it8:/bin/bash
it9:x:1010:1010::/home/it9:/bin/bash
it10:x:1011:1011::/home/it10:/bin/bash
it11:x:1012:1012::/home/it11:/bin/bash
it12:x:1013:1013::/home/it12:/bin/bash
it13:x:1014:1014::/home/it13:/bin/bash
it14:x:1015:1015::/home/it14:/bin/bash
it15:x:1016:1016::/home/it15:/bin/bash
it16:x:1017:1017::/home/it16:/bin/bash
it17:x:1018:1018::/home/it17:/bin/bash
it18:x:1019:1019::/home/it18:/bin/bash
it19:x:1020:1020::/home/it19:/bin/bash
it20:x:1021:1021::/home/it20:/bin/bash
it21:x:1022:1022::/home/it21:/bin/bash
it22:x:1023:1023::/home/it22:/bin/bash
it23:x:1024:1024::/home/it23:/bin/bash
it24:x:1025:1025::/home/it24:/bin/bash
it25:x:1026:1026::/home/it25:/bin/bash
it26:x:1027:1027::/home/it26:/bin/bash
it27:x:1028:1028::/home/it27:/bin/bash
it28:x:1029:1029::/home/it28:/bin/bash
it29:x:1030:1030::/home/it29:/bin/bash
it30:x:1031:1031::/home/it30:/bin/bash
it31:x:1032:1032::/home/it31:/bin/bash
it32:x:1033:1033::/home/it32:/bin/bash
it33:x:1034:1034::/home/it33:/bin/bash
it34:x:1035:1035::/home/it34:/bin/bash
it35:x:1036:1036::/home/it35:/bin/bash
it36:x:1037:1037::/home/it36:/bin/bash
it37:x:1038:1038::/home/it37:/bin/bash
it38:x:1039:1039::/home/it38:/bin/bash
it39:x:1040:1040::/home/it39:/bin/bash
it40:x:1041:1041::/home/it40:/bin/bash
it41:x:1042:1042::/home/it41:/bin/bash
it42:x:1043:1043::/home/it42:/bin/bash
it43:x:1044:1044::/home/it43:/bin/bash
it44:x:1045:1045::/home/it44:/bin/bash
it45:x:1046:1046::/home/it45:/bin/bash
it46:x:1047:1047::/home/it46:/bin/bash
it47:x:1048:1048::/home/it47:/bin/bash
it48:x:1049:1049::/home/it48:/bin/bash
it49:x:1050:1050::/home/it49:/bin/bash
it50:x:1051:1051::/home/it50:/bin/bash
it51:x:1052:1052::/home/it51:/bin/bash
it52:x:1053:1053::/home/it52:/bin/bash
it53:x:1054:1054::/home/it53:/bin/bash
it54:x:1055:1055::/home/it54:/bin/bash
it55:x:1056:1056::/home/it55:/bin/bash
it56:x:1057:1057::/home/it56:/bin/bash
it57:x:1058:1058::/home/it57:/bin/bash
it58:x:1059:1059::/home/it58:/bin/bash
it59:x:1060:1060::/home/it59:/bin/bash
it60:x:1061:1061::/home/it60:/bin/bash
it61:x:1062:1062::/home/it61:/bin/bash
it62:x:1063:1063::/home/it62:/bin/bash
it63:x:1064:1064::/home/it63:/bin/bash
it64:x:1065:1065::/home/it64:/bin/bash
it65:x:1066:1066::/home/it65:/bin/bash
it66:x:1067:1067::/home/it66:/bin/bash
it67:x:1068:1068::/home/it67:/bin/bash
it68:x:1069:1069::/home/it68:/bin/bash
it69:x:1070:1070::/home/it69:/bin/bash
it70:x:1071:1071::/home/it70:/bin/bash
it71:x:1072:1072::/home/it71:/bin/bash
it72:x:1073:1073::/home/it72:/bin/bash
it73:x:1074:1074::/home/it73:/bin/bash
it74:x:1075:1075::/home/it74:/bin/bash
it75:x:1076:1076::/home/it75:/bin/bash
it76:x:1077:1077::/home/it76:/bin/bash
it77:x:1078:1078::/home/it77:/bin/bash
it78:x:1079:1079::/home/it78:/bin/bash
it79:x:1080:1080::/home/it79:/bin/bash
it80:x:1081:1081::/home/it80:/bin/bash
it81:x:1082:1082::/home/it81:/bin/bash
it82:x:1083:1083::/home/it82:/bin/bash
it83:x:1084:1084::/home/it83:/bin/bash
it84:x:1085:1085::/home/it84:/bin/bash
it85:x:1086:1086::/home/it85:/bin/bash
it86:x:1087:1087::/home/it86:/bin/bash
it87:x:1088:1088::/home/it87:/bin/bash
it88:x:1089:1089::/home/it88:/bin/bash
it89:x:1090:1090::/home/it89:/bin/bash
it90:x:1091:1091::/home/it90:/bin/bash
it91:x:1092:1092::/home/it91:/bin/bash
it92:x:1093:1093::/home/it92:/bin/bash
it93:x:1094:1094::/home/it93:/bin/bash
it94:x:1095:1095::/home/it94:/bin/bash
it95:x:1096:1096::/home/it95:/bin/bash
it96:x:1097:1097::/home/it96:/bin/bash
it97:x:1098:1098::/home/it97:/bin/bash
it98:x:1099:1099::/home/it98:/bin/bash
it99:x:1100:1100::/home/it99:/bin/bash
it100:x:1101:1101::/home/it100:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@master ~]# cat /tmp/passwd 
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
/sbin/nologin:x:12:100:games:/usr/games:games
/sbin/nologin:x:14:50:FTP User:/var/ftp:ftp
/sbin/nologin:x:99:99:Nobody:/:nobody
/sbin/nologin:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:avahi-autoipd
/sbin/nologin:x:81:81:System message bus:/:dbus
/sbin/nologin:x:999:998:User for polkitd:/:polkitd
/sbin/nologin:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:tss
/sbin/nologin:x:89:89::/var/spool/postfix:postfix
/sbin/nologin:x:74:74:Privilege-separated SSH:/var/empty/sshd:sshd
/bin/bash:x:1000:1000:wang:/home/wang:wang
/sbin/nologin:x:192:192:systemd Network Management:/:systemd-network
/bin/bash:x:1001:1001::/home/it101:it101
/bin/bash:x:1002:1002::/home/it1:it1
/bin/bash:x:1003:1003::/home/it2:it2
/bin/bash:x:1004:1004::/home/it3:it3
/bin/bash:x:1005:1005::/home/it4:it4
/bin/bash:x:1006:1006::/home/it5:it5
/bin/bash:x:1007:1007::/home/it6:it6
/bin/bash:x:1008:1008::/home/it7:it7
/bin/bash:x:1009:1009::/home/it8:it8
/bin/bash:x:1010:1010::/home/it9:it9
/bin/bash:x:1011:1011::/home/it10:it10
/bin/bash:x:1012:1012::/home/it11:it11
/bin/bash:x:1013:1013::/home/it12:it12
/bin/bash:x:1014:1014::/home/it13:it13
/bin/bash:x:1015:1015::/home/it14:it14
/bin/bash:x:1016:1016::/home/it15:it15
/bin/bash:x:1017:1017::/home/it16:it16
/bin/bash:x:1018:1018::/home/it17:it17
/bin/bash:x:1019:1019::/home/it18:it18
/bin/bash:x:1020:1020::/home/it19:it19
/bin/bash:x:1021:1021::/home/it20:it20
/bin/bash:x:1022:1022::/home/it21:it21
/bin/bash:x:1023:1023::/home/it22:it22
/bin/bash:x:1024:1024::/home/it23:it23
/bin/bash:x:1025:1025::/home/it24:it24
/bin/bash:x:1026:1026::/home/it25:it25
/bin/bash:x:1027:1027::/home/it26:it26
/bin/bash:x:1028:1028::/home/it27:it27
/bin/bash:x:1029:1029::/home/it28:it28
/bin/bash:x:1030:1030::/home/it29:it29
/bin/bash:x:1031:1031::/home/it30:it30
/bin/bash:x:1032:1032::/home/it31:it31
/bin/bash:x:1033:1033::/home/it32:it32
/bin/bash:x:1034:1034::/home/it33:it33
/bin/bash:x:1035:1035::/home/it34:it34
/bin/bash:x:1036:1036::/home/it35:it35
/bin/bash:x:1037:1037::/home/it36:it36
/bin/bash:x:1038:1038::/home/it37:it37
/bin/bash:x:1039:1039::/home/it38:it38
/bin/bash:x:1040:1040::/home/it39:it39
/bin/bash:x:1041:1041::/home/it40:it40
/bin/bash:x:1042:1042::/home/it41:it41
/bin/bash:x:1043:1043::/home/it42:it42
/bin/bash:x:1044:1044::/home/it43:it43
/bin/bash:x:1045:1045::/home/it44:it44
/bin/bash:x:1046:1046::/home/it45:it45
/bin/bash:x:1047:1047::/home/it46:it46
/bin/bash:x:1048:1048::/home/it47:it47
/bin/bash:x:1049:1049::/home/it48:it48
/bin/bash:x:1050:1050::/home/it49:it49
/bin/bash:x:1051:1051::/home/it50:it50
/bin/bash:x:1052:1052::/home/it51:it51
/bin/bash:x:1053:1053::/home/it52:it52
/bin/bash:x:1054:1054::/home/it53:it53
/bin/bash:x:1055:1055::/home/it54:it54
/bin/bash:x:1056:1056::/home/it55:it55
/bin/bash:x:1057:1057::/home/it56:it56
/bin/bash:x:1058:1058::/home/it57:it57
/bin/bash:x:1059:1059::/home/it58:it58
/bin/bash:x:1060:1060::/home/it59:it59
/bin/bash:x:1061:1061::/home/it60:it60
/bin/bash:x:1062:1062::/home/it61:it61
/bin/bash:x:1063:1063::/home/it62:it62
/bin/bash:x:1064:1064::/home/it63:it63
/bin/bash:x:1065:1065::/home/it64:it64
/bin/bash:x:1066:1066::/home/it65:it65
/bin/bash:x:1067:1067::/home/it66:it66
/bin/bash:x:1068:1068::/home/it67:it67
/bin/bash:x:1069:1069::/home/it68:it68
/bin/bash:x:1070:1070::/home/it69:it69
/bin/bash:x:1071:1071::/home/it70:it70
/bin/bash:x:1072:1072::/home/it71:it71
/bin/bash:x:1073:1073::/home/it72:it72
/bin/bash:x:1074:1074::/home/it73:it73
/bin/bash:x:1075:1075::/home/it74:it74
/bin/bash:x:1076:1076::/home/it75:it75
/bin/bash:x:1077:1077::/home/it76:it76
/bin/bash:x:1078:1078::/home/it77:it77
/bin/bash:x:1079:1079::/home/it78:it78
/bin/bash:x:1080:1080::/home/it79:it79
/bin/bash:x:1081:1081::/home/it80:it80
/bin/bash:x:1082:1082::/home/it81:it81
/bin/bash:x:1083:1083::/home/it82:it82
/bin/bash:x:1084:1084::/home/it83:it83
/bin/bash:x:1085:1085::/home/it84:it84
/bin/bash:x:1086:1086::/home/it85:it85
/bin/bash:x:1087:1087::/home/it86:it86
/bin/bash:x:1088:1088::/home/it87:it87
/bin/bash:x:1089:1089::/home/it88:it88
/bin/bash:x:1090:1090::/home/it89:it89
/bin/bash:x:1091:1091::/home/it90:it90
/bin/bash:x:1092:1092::/home/it91:it91
/bin/bash:x:1093:1093::/home/it92:it92
/bin/bash:x:1094:1094::/home/it93:it93
/bin/bash:x:1095:1095::/home/it94:it94
/bin/bash:x:1096:1096::/home/it95:it95
/bin/bash:x:1097:1097::/home/it96:it96
/bin/bash:x:1098:1098::/home/it97:it97
/bin/bash:x:1099:1099::/home/it98:it98
/bin/bash:x:1100:1100::/home/it99:it99
/bin/bash:x:1101:1101::/home/it100:it100
/sbin/nologin:x:48:48:Apache:/usr/share/httpd:apache

9、把/data 目錄及其子目錄下所有以副檔名.txt 結尾的檔案中包含oldgirl 的字串全部替換 oldboy

find /data -type f -name "*.txt" -exec sed -i 's/oldgirl/oldboy/g' {} +

find /data -type f -name "*.txt" -exec sed -i 's/oldgirl/oldboy/g' {} \;








find /data -type f -name "*.txt" -exec sed -i 's/oldgirl/oldboy/g' {} \;
{}:這是一個佔位符,代表find命令找到的每個檔案的路徑。
\;:表示-exec選項的結束




find /data -type f -name "*.txt" -exec sed -i 's/oldgirl/oldboy/g' {} +


{}:這是一個佔位符,它會被 find 命令替換為當前找到的檔名。
+:這個符號告訴 find 命令,當收集到足夠的檔名時,應該將它們一次性傳遞給 sed 命令執行,而不是每次只傳遞一個檔名




實際效果演示


[root@master data]# cat 2.txt 
oldgirl

dkfhs
fgsf
sf

old
doldf
hsd

oldgir
oldgirl
sjkdhfs
sdjfhs
fshjf
s'shfs
oldgirl
fdshjs
sbfs
fsjh
oldgirl
dfjkhas
sdbf
oldgirl
1

[root@master data]# find /data -type f -name "*.txt" -exec sed -i 's/oldgirl/oldboy/g' {} +
[root@master data]# cat 2.txt
oldboy

dkfhs
fgsf
sf

old
doldf
hsd

oldgir
oldboy
sjkdhfs
sdjfhs
fshjf
s'shfs
oldboy
fdshjs
sbfs
fsjh
oldboy
dfjkhas
sdbf
oldboy
1







[root@master data]# cat 1.tx
cat: 1.tx: No such file or directory
[root@master data]# cat 1.txt 
oldgirl

dkfhs
fgsf
sf

old
doldf
hsd

oldgir
oldgirl
sjkdhfs
sdjfhs
fshjf
s'shfs
oldgirl
fdshjs
sbfs
fsjh
oldgirl
dfjkhas
sdbf
oldgirl
1
[root@master data]# find /data -type f -name "*.txt" -exec sed -i 's/oldgirl/oldboy/g' {} \;
[root@master data]# cat 1.txt 
oldboy

dkfhs
fgsf
sf

old
doldf
hsd

oldgir
oldboy
sjkdhfs
sdjfhs
fshjf
s'shfs
oldboy
fdshjs
sbfs
fsjh
oldboy
dfjkhas
sdbf
oldboy
1

10 查詢 /oldboy 下所有 7 天以前 以 log 結尾的大於 1M的檔案移動 /tmp 下

find /oldboy -type f \( -name "*.log" -size +1M \) -mtime +7 -exec mv {} /tmp/ \;



find /oldboy -type f -name "*.log" -mtime +7 -size +1M -exec mv -t /tmp/ {} +















find /oldboy -type f \( -name "*.log" -size +1M \) -mtime +7 -exec mv {} /tmp/ \;

-exec mv {} /tmp/ \;:對找到的每個檔案執行 mv 命令,將它們移動到 /tmp 目錄下。{} 是 find 命令找到的每個檔案的佔位符,\; 表示 -exec 選項的結束





find /oldboy -type f -name "*.log" -mtime +7 -size +1M -exec mv -t /tmp/ {} +
-exec mv -t /tmp/ {} +:對找到的每個檔案執行 mv 命令,將它們移動到 /tmp 目錄下。這裡使用 -t 選項指定目標目錄,然後 {} 是 find 命令找到的每個檔案的佔位符,+ 表示將盡可能多的檔名一次性傳遞給 mv 命令







實際效果演示


find /oldboy -type f -name "*.log" -mtime +7 -size +1M -exec mv -t /tmp/ {} +



11、什麼是Linux 的執行級別,請描述 Linux 的執行級別不同數字的含義

執行級就是作業系統當前正在執行的功能級別
?
第0級:關閉系統(千萬不要把initdefault設定為0,否則將開不了機)
第1級:單使用者模式
第2級:沒有網路多使用者模式
第3級:有網路多使用者模式
第4級:系統保留
第5級:有網路和圖形的多使用者模式
第6級:重啟系統

12、請描述 buffer 和 cache 的區別

buffer(即緩衝區),將寫入磁碟的IO先寫入到記憶體中,當達到了一定的時間或者是一定的大小的時候,再一次性地寫入到磁碟中,這是一個取的過程!
資料流向:CPU ==》記憶體==》磁碟
?
cache(快取區),為了避免程式大量的對磁碟進行讀寫,我們先將磁碟中的資料寫入到記憶體中,然後程式直接在記憶體中去讀取資料,是一個取的過程!
資料流向:磁碟==》記憶體==》CPU

相關文章