W4 find & sed

Posted kevin1111

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了W4 find & sed相关的知识,希望对你有一定的参考价值。

1、打包/etc/目录下面所有conf结尾的文件,压缩包名称为当天的时间,并拷贝到/usr/local/src目录备份。

find /etc/ -regex ".*\\.conf$" | xargs tar -zcvf `date +%F`-confs.tar.gz && cp `date +%F`-confs.tar.gz /usr/local/src

2、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件或目录

find / -nouser -o -nogroup -mtime -7 -ls

3、查找/etc目录下至少有一类用户没有执行权限的文件

find /etc/ ! -perm /111 -ls

4、自建网络yum源(通过httpd实现)

自建网络yum源需要2台机器,一台做server,一台做client

server端操作

以huawei mirror为例

# 配置华为云
wget -O /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-8-reg.repo

sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/CentOS-Base.repo
sed -i "s/mirrorlist=http/#mirrorlist=http/g" /etc/yum.repos.d/CentOS-Base.repo
sed -i "s@http://mirror.centos.org@https://repo.huaweicloud.com@g" /etc/yum.repos.d/CentOS-Base.repo
yum repolist all

## 使用httpd发布到局域网
dnf -y install httpd
systemctl enable --now httpd

# 同步需要的repos BaseOS AppStream extras 
dnf reposync  --repoid=BaseOS  --download-metadata -p /var/www/html/centos/8
dnf reposync  --repoid=AppStream  --download-metadata -p /var/www/html/centos/8
dnf reposync  --repoid=extras  --download-metadata -p /var/www/html/centos

# 配置EPEL指定华为源
touch /etc/yum.repo.d/epel.repo
[epel]
name=EPEL
baseurl=https://repo.huaweicloud.com/epel/8/Everything/x86_64/
gpgcheck=0

# 同步 epel
dnf reposync --repoid=epel --download-metadata -p /var/www/html
# 下载 epel key
wget -P /var/www/html/epel/ https://repo.huaweicloud.com/epel/RPM-GPG-KEY-EPEL-8

# 关闭防火墙
systemctl stop firewalld.service

client配置使用

vim /etc/yum.repos.d/test.repo
[BaseOS]
name=BaseOS
baseurl=http://192.168.0.121/centos/8/BaseOS
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[AppStream]
name=Appstream
baseurl=http://192.168.0.121/centos/8/AppStream/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[extras]
name=extras
baseurl=http://192.168.0.121/centos/extras/
[epel]
name=epel
baseurl=http://192.168.0.121/epel/
gpgkey=http://192.168.0.121/epel/RPM-GPG-KEY-EPEL-8

yum repolist all

5、利用sed 取出ifconfig命令中本机的IPv4地址

ifconfig  | sed -rn \'2s/^[^0-9]+([0-9.]+) .*$/\\1/p\'

6、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符

sed -Ei.bak "s/^#[[:space:]]+(.*)/\\1/" /etc/fstab

7、处理/etc/fstab路径,使用sed命令取出其目录名和基名

[root@localhost ~]# echo "/etc/fstab" |sed -r \'s#^/(.*)/(.*)#\\1#\'
etc
[root@localhost ~]# echo "/etc/fstab" |sed -r \'s#^/(.*)/(.*)#\\2#\'
fstab

以上是关于W4 find & sed的主要内容,如果未能解决你的问题,请参考以下文章

文件重命名示例

使用sunOS中的find命令比较两个目录中的所有文件

find && sed

find && sed

使用 find、wc 和 sed 计算行数

有关重命名,sed/rename/basename