難搞的grep、find練習題

馬哥教育網絡班21期-第五周博客作業

1、顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行;

[root@caicai ~]# grep --color "^[[:space:]]\+" /boot/grub/grub.conf
          root (hd0,0)
                   kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/vg_caicai-lv_root rd_NO_LUKS rd_LVM_LV=vg_caicai/lv_root LANG=en_US.UTF-8 rd_LVM_LV=vg_caicai/lv_swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
                            initrd /initramfs-2.6.32-504.el6.x86_64.img

2、顯示/etc/rc.d/rc.sysinit文件中以#開頭,后面跟至少一個空白字符,而后又有至少一個非空白字符的行;

[root@caicai ~]# grep "^\#[[:space:]]\+[^[:space:]]\+" /etc/rc.d/rc.sysinit
  # /etc/rc.d/rc.sysinit - run once at boot time # Taken in part from Miquel van Smoorenburg's bcheckrc. 
  # Check SELinux status 
  # Print a text banner. 
  # Only read this once. 
  # Initialize hardware 
  . 
  . 
  . 并未粘貼給出所有匹配到的行。

3、打出netstat -tan命令執行結果中以‘LISTEN’,后或跟空白字符結尾的行;

[root@caicai ~]# netstat -tan | grep "LISTEN[[:space:]]\+" 
tcp        0      0 0.0.0.0:52627              0.0.0.0:*                   LISTEN       
tcp        0      0 0.0.0.0:22                    0.0.0.0:*                   LISTEN       
tcp        0      0 127.0.0.1:631              0.0.0.0:*                   LISTEN       
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN       
tcp        0      0 0.0.0.0:111                  0.0.0.0:*                   LISTEN       
tcp        0      0 :::22                                 :::*                        LISTEN       
tcp        0      0 ::1:631                             :::*                        LISTEN       
tcp        0      0 ::1:25                               :::*                        LISTEN       
tcp        0      0 :::38556                           :::*                        LISTEN       tcp        0      0 :::111                               :::*                        LISTEN

4、添加用戶bash, testbash, basher, nologin (此一個用戶的shell為/sbin/nologin),而后找出當前系統上其用戶名和默認shell相同的用戶的信息;

這題我使用bash腳本 
#!/bin/bash 
id bash &> /dev/mull || useradd bash 
id testbash &> /dev/null || useradd testbash 
id basher &> /dev/null || useradd basher 
id nologin &> /dev/null || useradd -s /sbin/nologin nologin 
grep --color "^\(\b[[:alnum:]]\+\b\).*\1$" /etc/passwd 
#grep --color -E "^(\b[[:alnum:]]+\b).*\1$" /etc/passwd

5、顯示當前系統上root、fedora或user1用戶的默認shell;

[root@caicai ~]# grep -E --color "^(root|fedora|user1)" /etc/passwd | cut -d: -f7 /bin/bash /bin/bash

6、找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號的行,形如:hello();

[root@caicai ~]# grep -E --color "\b.*[[:alpha:]].*\b\(\)" /etc/rc.d/init.d/functions  
fstab_decode_str() { 
checkpid() { 
__readlink() { 
__fgrep() { 
__umount_loop() { 
__umount_loopback_loop() { 
__pids_var_run() { 
. 
. 
. 
并未粘貼給出所有匹配到的行。

7、使用echo命令輸出一個絕對路徑,使用grep取出其基名;擴展:取出其路徑名

[root@caicai ~]# echo "/etc/qwe/asd/zxc" | grep -o "\b\/[[:alnum:]]\+\b$" 
/zxc 

[root@caicai ~]# echo "/etc/qwe/asd/zxc" | grep -o --color "\/.*\/" 
/etc/qwe/asd/

8、找出ifconfig命令結果中的1-255之間數字;

[root@caicai ~]# ifconfig | grep -E --color "\b[0-9]{1,2}\b|\b1[0-9]{1,2}\b|2[0-4][0-9]?\b|\b25[0-5]?\b"

9、挑戰題:寫一個模式,能匹配合理的IP地址;

[root@caicai ~]# ifconfig | grep -E --color "((\b[0-9]{1,2}\b|\b1[0-9]{1,2}\b|2[0-4][0-9]?|\b25[0-5]?)\b\.){3}(\b[0-9]{1,2}\b|\b1[0-9]{1,2}\b|2[0-4][0-9]?|\b25[0-5]?\b)"
           inet addr:192.168.183.128  Bcast:192.168.183.255  Mask:255.255.255.0           
           inet addr:127.0.0.1  Mask:255.0.0.0

10、挑戰題:寫一個模式,能匹配出所有的郵件地址;

[root@caicai ~]# cat mail  
123@163.com 
asdaf@qq.com 
j4l3jlqF@outlook.com 
jijibab 
1234 
asd_123@yahoo.com  lkskdj@yahoo.com 


[root@caicai ~]# grep --color ".*@.*\.com" ./mail     
123@163.com 
asdaf@qq.com 
j4l3jlqF@outlook.com 
asd_123@yahoo.com  lkskdj@yahoo.com

11、查找/var目錄下屬主為root,且屬組為mail的所有文件或目錄;

[root@caicai ~]# find /var -user root -a -group mail -ls  
525464    4 drwxrwxr-x   2 root     mail         4096 Jul 14 19:40 /var/spool/mail

12、查找當前系統上沒有屬主或屬組的文件;

[root@caicai ~]# find / -nouser -a -nogroup

# 進一步:查找當前系統上沒有屬主或屬組,且最近3天內曾被訪問過的文件或目錄;

[root@caicai ~]# find / -nouser -a -nogroup -a -atime -3

13、查找/etc目錄下所有用戶都有寫權限的文件;

[root@caicai ~]# find /etc -perm -111

14、查找/etc目錄下大于1M,且類型為普通文件的所有文件;

[root@caicai ~]# find /etc -size +1M -a -type f -exec ls -lh {} \; 
-rw-r--r--. 1 root root 7.8M Jun 24 18:52 /etc/selinux/targeted/policy/policy.24 
-rw-r--r--. 1 root root 7.8M Jun 24 18:52 /etc/selinux/targeted/modules/active/policy.kern 
-rw-r--r--. 1 root root 2.2M Jun 24 18:56 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml

15、查找/etc/init.d/目錄下,所有用戶都有執行權限,且其它用戶有寫權限的文件;

[root@caicai ~]# find /etc/init.d/ -perm -113

16、查找/usr目錄下不屬于root、bin或hadoop的文件;

[root@caicai ~]# find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls 
795159   12 -rwsr-xr-x   1 abrt     abrt        10296 Oct 16  2014 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

17、查找/etc/目錄下至少有一類用戶沒有寫權限的文件;

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

18、查找/etc目錄下最近一周內其內容被修改過,且不屬于root或hadoop的文件;

[root@caicai ~]# find /etc -mtime -7 -a -not -user root -o -not -user hadoop

原創文章,作者:N21_志建,如若轉載,請注明出處:http://www.www58058.com/25551

(0)
N21_志建N21_志建
上一篇 2016-07-26 16:42
下一篇 2016-07-26 16:43

相關推薦

  • 第三周作業

    1. 列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次則顯示一次即可。 who | cut -d' ' -f1 | sort -u 2. 取出最后登錄當前系統的用戶的相關信息 grep  "$(who …

    Linux干貨 2016-12-19
  • Linux文件的權限與解析

    一,文件的基本權限: 通常,你使用ls -l 命令,就會看到這樣的行:   讓我們解析一下這些字段代表的意思: -rw-r–r–. 1 root root 1018 Nov 6 2016 usb_modeswitch.conf 文件類型權限  硬鏈接數 文件所有者 文件所屬組 文件容量  文件最后被修改時…

    2017-07-30
  • quota AND raid

    1.磁盤配額 將home目錄獨立出來單獨的分區,限制用戶的使用。 (1)先創建分區,如下圖的分區sda6 (2)命令同步系統的新增加分區:partx –a /dev/sda (3)命令格式化新分區:     (4)把創建的設備掛載到/mnt/home下    (5)遷移數據,移動之前要確保home分區沒有使用。權限是?!?/p>

    Linux干貨 2016-09-19
  • 系統管理之Systemd詳解(centos7)

    這篇著重講解下Syetemd的相關知識,systemd可以說是centos7上的重大改革,功能之強大媲美一個操作系統,那下面就從以下幾點來進行講解:CentOS7啟動Unit介紹服務管理和查看啟動排錯破解口令修復grub2 啟動流程: post–>BISO–>bootloader(MBR)–>kernel(ramdisk)–>…

    Linux干貨 2016-09-21
  • CentOS系統啟動流程、selinux、Systemd剖析

    交互式登錄配置文件讀取順序: /etc/profile –> /etc/profile.d/*.sh –> ~/.bash_profile –> ~/.bashrc –> /etc/bashrc 非交互式登錄配置文件讀取順序: ~/.bashrc –> /etc/bas…

    Linux干貨 2018-03-04
  • 淺談Linux終端類型

    Linux終端類型 作者:任飛鵬            日期:2016-10-13 終端是什么: 終端(Terminal)也稱終端設備,是計算機網絡中處于網絡最外圍的設備,主要用于用戶信息的輸入以及處理結果的輸出等。 早期計算機系統中,由于計算機主機…

    Linux干貨 2016-10-19

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-08-17 15:13

    寫的很好,排版也很棒,加油

欧美性久久久久