永久修改主机名
[root@kangxu ~]# hostnamectl set-hostname cuihua
[root@kangxu ~]# bash
[root@cuihua ~]#
重要面试题
使用shell脚本,找出/etc目录下以.conf结尾文件复制到/tmp下面
方法1.
find /etc/ -name '*.conf' -type f|xargs -i cp {} /tmp/
方法2.
find /etc/ -name '*.conf' -type f -exec cp {} /tmp \;
方法3.
cp $(find /etc/ -name '*.conf' -type f ) /tmp
方法4.
find /etc/ -name '*.conf' -type f |xargs cp -t /tmp/
打包备份/etc目录到/backup下面 保证每天备份的压缩包名字不同
[root@oldboy ~]# tar zcf /backup/etc-'date +%F'.tar.gz /etc
tar: Removing leading `/' from member names
[root@oldboy ~]# ls /backup
etc-2019-04-19.tar.gz
取出网卡ip地址:
ip a s eth0
方法1 sed 正则
[root@oldboyedu59 ~]# ip a s eth0 |sed -n '3p' |sed 's#^.*t ##g'
10.0.0.201/24 brd 10.0.0.255 scope global eth0
[root@oldboyedu59 ~]# ip a s eth0 |sed -n '3p' |sed 's#^.*t ##g'|sed 's#/.*$##g'
10.0.0.201
方法2 sed 后向引用
[root@oldboyedu59 ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:ff:79:0e brd ff:ff:ff:ff:ff:ff
inet 10.0.0.201/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feff:790e/64 scope link
valid_lft forever preferred_lft forever
[root@oldboyedu59 ~]# ip a s eth0 |sed -n 3p
inet 10.0.0.201/24 brd 10.0.0.255 scope global eth0
[root@oldboyedu59 ~]# ip a s eth0 |sed -n 3p |sed -r 's#^.*t (.*)/.*$#\1#g'
10.0.0.201
[root@oldboyedu59 ~]# ip a s eth0 |sed -n 3p |sed -r 's#(^.*t )(.*)(/.*$)#\1#g'
inet
[root@oldboyedu59 ~]# ip a s eth0 |sed -n 3p |sed -r 's#(^.*t )(.*)(/.*$)#\3#g'
/24 brd 10.0.0.255 scope global eth0
stat /etc/hosts
取出结果中的644
[root@cuihua ~]# stat /etc/hosts|sed -n '4p'|sed -r 's#^.*\(0([0-9]+)/.*$#\1#g'
644
[root@cuihua ~]#
[root@cuihua ~]# stat /etc/hosts|sed -n '4p'|sed -r 's#^.*\(0(.*)/-.*$#\1 #g'
644
Linux下面生成随机密码方法
方法1 tr + head
[root@oldboyedu59 ~]# tr -cd 'a-zA-Z0-9' </dev/urandom |head -c8
B0iV36fZ[root@oldboyedu59 ~]#
方法2 date +md5sum/sha512sum + head
[root@oldboyedu59 ~]# date +%N
211379317
date +%N |md5sum |head -c10
[root@oldboyedu59 ~]# date +%N|md5sum |head -c8
888b0ea8[root@oldboyedu59 ~]#
方法3 RANDOM环境变量
RANDOM生成随机数字
echo $RANDOM
RANDOM + md5sum
RANDOM +数字
[root@oldboyedu59 ~]# echo $((RANDOM+10000000))
10017665