1、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如:hello();
[root@localhost~]# ls /etc/rc.d/init.d/ functions netconsole network README [root@localhost ~]# grep -o "\<.*\>()"/etc/rc.d/init.d/functions checkpid() __pids_var_run() __pids_pidof() daemon() killproc() pidfileofproc() pidofproc() status() echo_success() echo_failure() echo_passed() echo_warning() update_boot_stage() success() failure() passed() warning() action() strstr()
2、使用echo命令输出一个绝对路径,使用grep取出其基名;
[root@localhost~]# echo "/etc/rc.d/init.d/functions"| grep -E -o "[^/]+$" functions
扩展:取出其路径名
[root@localhost~]# echo "/etc/rc.d/init.d/functions" | grep -E -o "^/.*/" /etc/rc.d/init.d/
3、找出ifconfig命令结果中的1-255之间数字;
[root@localhost ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.108 netmask255.255.255.0 broadcast 192.168.2.255
inet6 fe80::20c:29ff:fe0d:4a8b prefixlen 64 scopeid0x20<link>
ether 00:0c:29:0d:4a:8b txqueuelen 1000 (Ethernet)
RX packets 419 bytes 41325 (40.3KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 337 bytes 46637 (45.5KiB)
TX errors 0 dropped 0 overruns0 carrier 0 collisions 0
lo:flags=73<UP,LOOPBACK,RUNNING> mtu65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 4 bytes 340 (340.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 340 (340.0 B)
TX errors 0 dropped 0 overruns0 carrier 0 collisions 0
virbr0:flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:47:81:6c txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns0 carrier 0 collisions 0
[root@localhost ~]#ifconfig | grep -E -o "\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>" 192 168 2 108 255 255 255 0 192 168 2 255 64 29 38 5 0 0 0 0 40 8 0 0 0 0 0 73 127 0 0 1 255 0 0 0 1 128 0 4 0 0 0 0 0 4 0 0 0 0 0 0 192 168 122 1 255 255 255 0 192 168 122 255 52 54 47 81 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4、查找当前系统上没有属主或属组的文件;
[root@localhost~]# find / -nouser -o -nogroup /home/mandriva /home/mandriva/.bash_logout /home/mandriva/.bash_profile /home/mandriva/.bashrc /home/mandriva/.mozilla /home/mandriva/.mozilla/extensions /home/mandriva/.mozilla/plugins find: ‘/proc/3244/task/3244/fd/6’: 没有那个文件或目录 find:‘/proc/3244/task/3244/fdinfo/6’: 没有那个文件或目录 find: ‘/proc/3244/fd/6’: 没有那个文件或目录 find:‘/proc/3244/fdinfo/6’: 没有那个文件或目录 /var/spool/mail/mandriva [root@localhost ~]#
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;
[root@localhost ~]# find / \( -nouser -o-nogroup \) -a -atime -3
/home/mandriva
/home/mandriva/.mozilla
/home/mandriva/.mozilla/extensions
/home/mandriva/.mozilla/plugins
find: ‘/proc/3254/task/3254/fd/6’: 没有那个文件或目录
find: ‘/proc/3254/task/3254/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/3254/fd/6’: 没有那个文件或目录
find: ‘/proc/3254/fdinfo/6’: 没有那个文件或目录
5、查找/etc目录下大于1M,且类型为普通文件的所有文件;
[root@localhost ~]# find /etc -size +1M -a-type f -ls
4239312 6852 -r--r--r-- 1 root root 7014922 12月 11 00:24/etc/udev/hwdb.bin
202385046 3772 -rw-r--r-- 1 root root 3858924 11月 21 2015 /etc/selinux/targeted/policy/policy.29
4083229 1336 -rw-r--r-- 1 root root 1367395 3月 6 2015/etc/brltty/zh-tw.ctb
[root@localhost ~]#
6、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;
[root@localhost ~]# find /etc/init.d/ -perm/113 -ls
201494444 0 drwxr-xr-x 2 root root 66 12月 10 00:22/etc/init.d/
201868855 4 -rwxr-xr-x 1 root root 2989 9月 16 2015 /etc/init.d/netconsole
201868856 8 -rwxr-xr-x 1 root root 6630 9月 16 2015 /etc/init.d/network
7、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;
[root@localhost~]# find /etc/ -not \( -user root -o -user hadoop \) -atime -7 -ls find: 用户名‘hadoop’ 未知 [root@localhost ~]# find/etc/ -not \( -user root -o -user bin \) -atime -7 -ls 134780546 0 drwx------ 2 polkitd root 63 12月 10 00:21 /etc/polkit-1/rules.d 136093649 0 drwx--x--x 2 sssd sssd 6 8月 3 00:58 /etc/sssd [root@localhost ~]#
8、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;
[root@localhost~]# ls /etc/rc.d/ init.d rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d rc.local [root@localhost ~]#
[root@localhost~]# cp /etc/rc.d/rc.local /tmp/ [root@localhost ~]# ls /tmp/ mytest2 systemd-private-62113d01174b48f3b9b96b264b53cd52-cups.service-x1z7b8 rc.local systemd-private-62113d01174b48f3b9b96b264b53cd52-vmtoolsd.service-pkrcv0 [root@localhost ~]# sed -i 's/\(^[[:space:]]\)/#\1/g' /tmp/rc.local
9、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符
[root@localhost~]# cat /etc/rc.local #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local [root@localhost ~]# cp /etc/rc.d/rc.local /tmp/ [root@localhost ~]# ls /tmp/ mytest2 systemd-private-62113d01174b48f3b9b96b264b53cd52-cups.service-x1z7b8 rc.local systemd-private-62113d01174b48f3b9b96b264b53cd52-vmtoolsd.service-pkrcv0
[root@localhost~]# sed -i 's/^#[[:space:]]\+//g' /tmp/rc.local [root@localhost ~]# cat /tmp/rc.local #!/bin/bash THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # It is highly advisable to create own systemd services or udev rules to run scripts during boot instead of using this file. # In contrast to previous versions due to parallel execution during boot this script will NOT be run after all other services. # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure that this script will be executed during boot. touch /var/lock/subsys/local [root@localhost ~]#
10、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;
[root@localhost~]# sed 's/enabled=0/enabled=1/g;s/gpgcheck=1/gpgcheck=2/g;'/etc/yum.repos.d/CentOS-Media.rep
11、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20161202
[root@localhost~]# crontab -e
00 00 * *2,4,6 cp -r /var/log/messages /backup/messages_logs/messages-$(date +/%Y/%m/%d)
12、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
[root@localhost~]# crontab -e
00 */2 * * *cat /proc/meminfo |grep "^S" >> /stats/memory.txt
13、写一个脚本创建10用户user10-user19;密码同用户名;
[root@localhost~]# vi test02
#!/bin/sh
for i in {10..19};do
id user$i &>/dev/dull
if [ $? -eq 0 ];then
echo "user$i exits"
else
useradd user$i
echo "user$i"| passwd --stdin user$i
echo "user$i added"
fi
done