一、查找/etc目录下大于1M且类型为普通文件的所有文件;
[root@localhost ~]# find /etc -type f -size +1M | xargs ls -lh
-rw-------. 1 root root 3.8M Nov 3 2018 /etc/selinux/targeted/active/policy.kern
-rw-r--r--. 1 root root 1.4M Nov 3 2018 /etc/selinux/targeted/contexts/files/file_contexts.bin
-rw-r--r--. 1 root root 3.8M Nov 3 2018 /etc/selinux/targeted/policy/policy.31
-r--r--r--. 1 root root 7.6M Jun 9 20:38 /etc/udev/hwdb.bin
二、打包/etc/目录下面所有conf结尾的文件,压缩包名称为当天的时间,并拷贝到/usr/local/src目录备份;
[root@localhost ~]# find /etc -type f -name "*.conf" | xargs tar zcf `date +%F`.tar.gz && cp `date +%F`.tar.gz /usr/local/src
tar: Removing leading `/' from member names
[root@localhost ~]# ls
111 192.168.1.111 1.txt 2020-07-07.tar.gz 2.txt 3.txt aaa abc.php anaconda-ks.cfg bbb creatuser.sh test1 test.php test.txt
[root@localhost ~]# ls /usr/local/src
2020-07-07.tar.gz
[root@localhost ~]# date
Tue Jul 7 13:18:06 CST 2020
三、利用sed 取出ifconfig命令中本机的IPv4地址;
[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.103 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::de8:e705:819:cec0 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:46:a3:d9 txqueuelen 1000 (Ethernet)
RX packets 1369 bytes 131434 (128.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 746 bytes 93910 (91.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# ifconfig ens33 | sed -n '2p'
inet 192.168.1.103 netmask 255.255.255.0 broadcast 192.168.1.255
[root@localhost ~]# ifconfig ens33 | sed -n '2p' | sed -nr 's@^[^0-9]+([0-9.]+).*$@\1@p'
192.168.1.103
或者
[root@localhost ~]# ifconfig ens33 | sed -nr '2s@^[^0-9]+([0-9.]+).*$@\1@p'
192.168.1.103
四、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符;
1、备份到/tmp目录,先使用sed,将所需要匹配的行匹配出来;
[root@localhost ~]# cp /etc/fstab /tmp/
[root@localhost ~]# ll /tmp
total 4
-rw-r--r-- 1 root root 0 Jun 18 19:43 3.txt
-rw-r--r-- 1 root root 465 Jul 7 13:33 fstab
drwxr-xr-x 3 root root 17 Jun 25 10:08 mytest1
[root@localhost ~]# cat /tmp/fstab
#
# /etc/fstab
# Created by anaconda on Tue Jun 9 20:29:14 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=61bfe580-8947-41c1-91c0-0e3422e7d468 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
[root@localhost ~]# sed -nr '/^#[[:blank:]]+/s@^#[[:blank:]]+(,*)@\1@p' /tmp/fstab
/etc/fstab
Created by anaconda on Tue Jun 9 20:29:14 2020
Accessible filesystems, by reference, are maintained under '/dev/disk'
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
2、加上-i将其修改对应的文件,然后验证;
[root@localhost ~]# sed -ri '/^#[[:blank:]]+/s@^#[[:blank:]]+(,*)@\1@p' /tmp/fstab
[root@localhost ~]# cat /tmp/fstab
#
/etc/fstab
/etc/fstab
Created by anaconda on Tue Jun 9 20:29:14 2020
Created by anaconda on Tue Jun 9 20:29:14 2020
#
Accessible filesystems, by reference, are maintained under '/dev/disk'
Accessible filesystems, by reference, are maintained under '/dev/disk'
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=61bfe580-8947-41c1-91c0-0e3422e7d468 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
五、处理/etc/fstab路径,使用sed命令取出其目录名和基名;
[root@localhost ~]# echo "/etc/fstab" | sed -r 's@(.*)\/.+@\1@'
/etc
[root@localhost ~]# echo "/etc/fstab" | sed -r 's@.*\/(.+)@\1@'
fstab