四剑客第九关

四剑客面试真题-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
posted @ 2024-03-16 23:02  三思博客  阅读(3)  评论(0编辑  收藏  举报