Linux基础练习题(三)

时间:2023-02-27 09:33:15

1、显示当前系统上root、fedora或user1用户的默认shell;

[root@www ~]# egrep "^(root|fedora|user1)" /etc/passwd|cut -d: -f1,7
root:/bin/bash
user1:/bin/bash
fedora:/bin/zsh

2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行;例如:hello();

[root@www ~]# egrep "\w+\(\)" /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() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {

3、使用echo命令输出一个绝对路径,使用grep取出其基名;扩展:取出其路径名;

# 取出其基名
[root@www ~]# echo "/var/log/messages"|egrep -o  "[^/]+\/?$"
messages
# 取出其路径名
[root@www ~]# echo "/var/log/messages"|grep -o "^/.*/"
/var/log/

4、找出ifconfig命令结果中的1-255之间的数字;

[root@www ~]# ifconfig |egrep -o "\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"

5、挑战题:写一个模式,能匹配出合理的IP地址;

[root@www ~]# ifconfig | egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
192.168.1.141
255.255.255.0
192.168.1.255
127.0.0.1
255.0.0.0

6、挑战题:写一个模式,能匹配出所有的邮件地址;

[root@www ~]# egrep -o  "\w+@\w+\.(cn|com)" /etc/test.txt
tom@163.com
jerry@163.com
zhangsan@testdomain.cn

7、查找/var目录下属主为root,且属组为mail的所有文件或目录;

[root@www ~]# find /var/ -user root -a -group mail -ls
134295636    0 drwxrwxr-x   2 root     mail          103 12月 30 00:38 /var/spool/mail
136092594  884 -rw-------   1 root     mail       900983 12月 30 00:38 /var/spool/mail/root

8、查找当前系统上没有属主或属组的文件;查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;

[root@www ~]# find / -atime -3 -nouser -a -nogroup -exec ls -l {} \;
-rw-rw-rw- 1 1000 1000 6 12月 30 00:35 /etc/test.txt

9、查找/etc目录下所有用户都有写权限的文件;

[root@www ~]# find /etc/ -type f -perm -222 -ls
67190743    0 -rw-rw-rw-   1 root     root            0 12月 30 00:10 /etc/test.txt

10、查找/etc目录下大于1M,且类型为普通文件的所有文件;

[root@www ~]# find /etc/ -type f -size +1M -exec ls -lh {} \;
-r--r--r--. 1 root root 6.7M 12月  4 18:22 /etc/udev/hwdb.bin
-rw-r--r--. 1 root root 3.7M 11月 21 2015 /etc/selinux/targeted/policy/policy.29

11、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件;

[root@www ~]# find /etc/init.d/ -perm -113 -ls
 12777    0 -rwxr-xrwx   1 root     root            0 12月 30 00:17 /etc/init.d/test.txt

12、查找/usr目录下不属于root,bin或hadoop的文件;

[root@www ~]# find /usr/ -not \( -user root -o -user bin -o -user hadoop \) -ls
618038    0 drwx------   2 polkitd  root            6 6月 10  2014 /usr/share/polkit-1/rules.d

13、查找/etc目录下至少有一类用户没有写权限的文件;

[root@www ~]# find /etc/ -not -perm -222 -ls

14、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;

[root@www ~]# find /etc/ -mtime -7 -type f -a -not \( -user root -o -user hadoop \) -exec stat {} \;
  文件:"/etc/test.txt"
  大小:6          块:8          IO 块:4096   普通文件
设备:803h/2051d   Inode:67190743    硬链接:1
权限:(0666/-rw-rw-rw-)  Uid:( 1000/enzhi.wang)   Gid:( 1000/enzhi.wang)
最近访问:2016-12-30 00:10:38.089508753 +0800
最近更改:2016-12-30 00:35:36.988831137 +0800
最近改动:2016-12-30 00:36:09.092832268 +0800
创建时间:-