N25第五周 grep 和find 命令使用示例

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

   [root@localhost grub]# grep "^[[:space:]]\+.*" grub.conf
   root (hd0,0)

    限于格式要求,只截取部分顯示結果

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

    [root@localhost rc.d]# grep "^#[[:space:]]\+[^[:space:]]" 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
    # Set default affinity
    # Load other user-defined modules
    # Load modules (for backward compatibility with VARs)
    # Configure kernel parameters
    # Set the hostname.
    # Sync waiting for storage.
    # Device mapper & related initialization
    # Start any MD RAID arrays that haven't been started yet
    # Remount the root filesystem read-write.
    # Clean up SELinux labels
    # If relabeling, relabel mount points.
    # Mount all other filesystems (except for NFS and /proc, which is already
    # mounted). Contrary to standard usage,
    # filesystems are NOT unmounted in single user mode.
    # The 'no' applies to all listed filesystem types. See mount(8).
    # Update quotas if necessary
    # Check to see if a full relabel is needed
    # Initialize pseudo-random number generator
    # Configure machine if necessary.
    # Clean out /.
    # Do we need (w|u)tmpx files? We don't set them up, but the sysadmin might...
    # Clean up /var.
    # Clean up utmp/wtmp
    # Clean up various /tmp bits
    # Make ICE directory
    # Start up swapping.
    # Set up binfmt_misc
    # Boot time profiles. Yes, this should be somewhere else.
    # Now that we have all of our basic modules loaded and the kernel going,
    # let's dump the syslog ring somewhere so we can find it later
    # create the crash indicator flag to warn on crashes, offer fsck with timeout
    # Let rhgb know that we're leaving rc.sysinit
    [root@localhost rc.d]#

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

    [root@localhost rc.d]# netstat -tan | grep "LISTEN[[:space:]]*$"
    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 :::22                       :::*                        LISTEN      
    tcp        0      0 ::1:631                     :::*                        LISTEN      
    tcp        0      0 ::1:25                      :::*                        LISTEN

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

    [root@localhost ~]# grep "^\([a-z]\+\b\).*\1$" /etc/passwd
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    bash:x:501:501::/home/bash:/bin/bash
    nologin:x:503:503::/home/nologin:/sbin/nologin
    [root@localhost ~]#

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

    [root@localhost ~]# grep -E "^(root|fedora|user1)\b.*" /etc/passwd | cut -d: -f1,7
    root:/bin/bash
    fedora:/bin/bash
    user1:/bin/bash
    [root@localhost ~]#

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

    [root@localhost ~]# grep "[[:alnum:]]\+()" /etc/rc.d/init.d/functions
    fstab_decode_str() {
    checkpid() {
    __readlink() {
    __fgrep() {
    __umount_loop() {
    __umount_loopback_loop() {
    __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() {
    confirm() {
    get_numeric_dev() {
    is_ignored_file() {
    is_true() {
    is_false() {
    apply_sysctl() {
    key_is_random() {
    find_crypto_mount_point() {
    init_crypto() {
    [root@localhost ~]#

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

  [root@localhost ~]# echo /tmp/test/ | grep -o "[^/]\+\/\?$"
  test/


  擴展:取出其路徑名

   [root@localhost ~]# echo /tmp/test/ | grep -o "^\/\+[[:alnum:]]\+\b"
    /tmp

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

 [root@localhost ~]# ifconfig | grep  -E -o "\b(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]{1,2})\b"
    192
    168
    1
    104
    255
    255
    255
    192
    168
    1
    255
    64
    29
    41
    25
    116
    4
    111
    73
    127
    1
    255
    1
    128
    4
    4
    192
    168
    122
    1
    255
    255
    255
    192
    168
    122
    255
    [root@localhost ~]#

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

[root@localhost ~]# ifconfig | grep -P -o  "\b((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)\b"
    192.168.1.104
    255.255.255.0
    192.168.1.255
    127.0.0.1
    255.0.0.0
    192.168.122.1
    255.255.255.0
    192.168.122.255
或者

[root@localhost ~]# ifconfig | grep -E -o "((2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)\.){3}(2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)"
    192.168.1.104
    255.255.255.0
    192.168.1.255
    127.0.0.1
    255.0.0.0
    192.168.122.1
    255.255.255.0
    192.168.122.255

以上是匹配所有的ip地址。

如果要匹配跟隨inet后面的地址則為:

 [root@localhost ~]# ifconfig | grep "inet\b" | cut -d" " -f10
 192.168.1.104
 127.0.0.1
 192.168.122.1
 [root@localhost ~]#

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

    [root@localhost ~]# grep -E -o "\b[^[:space:]]+\@[^[:space:]]+\b" /tmp/test/mailstest.text
    163frj@msina.com
    4355@qq.com
    greatwall_china@gov.com
    goodluck_232@facebook.com
    455iirr@rl.ro
    ab55&00_@gmail.com

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

[root@localhost ~]# find /var -user root -group mail -ls
134221194    4 drwxrwxr-x   2 root     mail         4096 Dec 22 06:48 /var/spool/mail
137126650  132 -rw-------   1 root     mail       131956 Nov 30 19:31 /var/spool/mail/root

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

[root@localhost ~]# find  / -nouser -o -nogroup -ls
find: ‘/proc/15106’: No such file or directory
find: ‘/proc/15114/task/15114/fd/6’: No such file or directory
find: ‘/proc/15114/task/15114/fdinfo/6’: No such file or directory
find: ‘/proc/15114/fd/6’: No such file or directory
find: ‘/proc/15114/fdinfo/6’: No such file or directory


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

[root@localhost ~]# find  /  \( -nouser -o -nogroup \)  -atime  -3  -ls
find: ‘/proc/16974/task/16974/fd/6’: No such file or directory
find: ‘/proc/16974/task/16974/fdinfo/6’: No such file or directory
find: ‘/proc/16974/fd/6’: No such file or directory
find: ‘/proc/16974/fdinfo/6’: No such file or directory
403645670    0 drwx------   3 1002     1002           74 Nov 12 16:21 /home/slackware
3484079    0 drwxr-xr-x   4 1002     1002           37 Nov  8 20:14 /home/slackware/.mozilla
137119261    0 drwxr-xr-x   2 1002     1002            6 Jun  9  2014 /home/slackware/.mozilla/extensions
272578502    0 drwxr-xr-x   2 1002     1002            6 Jun  9  2014 /home/slackware/.mozilla/plugins
405829138    0 drwx------   3 200      1005           74 Nov 16 19:03 /home/openstack
4526238    0 drwxr-xr-x   4 200      1005           37 Nov  8 20:14 /home/openstack/.mozilla
137132447    0 drwxr-xr-x   2 200      1005            6 Jun  9  2014 /home/openstack/.mozilla/extensions
272578520    0 drwxr-xr-x   2 200      1005            6 Jun  9  2014 /home/openstack/.mozilla/plugins
405829147    0 drwx------   3 1005     1007           74 Nov 16 19:16 /home/mix
4334240    0 drwxr-xr-x   4 1005     1007           37 Nov  8 20:14 /home/mix/.mozilla
137132451    0 drwxr-xr-x   2 1005     1007            6 Jun  9  2014 /home/mix/.mozilla/extensions
272578522    0 drwxr-xr-x   2 1005     1007            6 Jun  9  2014 /home/mix/.mozilla/plugins
139922069    0 drwx------   3 1005     distro         74 Dec 20 04:30 /home/mandriva
270994653    0 drwxr-xr-x   4 1005     distro         37 Nov  8 20:14 /home/mandriva/.mozilla
405832494    0 drwxr-xr-x   2 1005     distro          6 Jun  9  2014 /home/mandriva/.mozilla/extensions
3482209    0 drwxr-xr-x   2 1005     distro          6 Jun  9  2014 /home/mandriva/.mozilla/plugins

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

[root@localhost ~]# find /etc  -perm -222 -ls | head -n 10
667104    0 lrwxrwxrwx   1 root     root           37 Nov 30 04:10 /etc/xdg/
655829    0 lrwxrwxrwx   1 root     root           19 Nov 30 03:54 /e
653507    0 lrwxrwxrwx   1 root     root           11 Nov 30 03:53 /etc/in
662151    0 lrwxrwxrwx   1 root     root           21 Nov 30 03:58 /etc/gd/
655834    0 lrwxrwxrwx   1 root     root           16 Nov 30 03:54 /etc/ssl/c
670719    0 lrwxrwxrwx   1 root     root           29 Nov 30 04:08 /etc/vmware-tools/
670721    0 lrwxrwxrwx   1 root     root           25 Nov 30 04:08 /et
669583    0 lrwxrwxrwx   1 root     root           22 Nov 30 04:06 /etc/grub.conf -> ../
667434    0 lrwxrwxrwx   1 root     root           19 Nov 30 04:01 /et -> ..
668205    0 lrwxrwxrwx   1 root     root           18 Nov 30 04:02 /etc/rc.d/rc5.d/

限于格式,只截取部分結果展示。

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

[root@localhost init.d]# find /etc -size +1M -type f -exec ls -lh {} \;
-rw-r--r--. 1 root root 2.0M Nov 30 04:02 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
-rw-r--r--. 1 root root 7.0M Nov 30 04:09 /etc/selinux/targeted/modules/active/policy.kern
-rw-r--r--. 1 root root 7.0M Nov 30 04:09 /etc/selinux/targeted/policy/policy.24

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

[root@localhost ~]# find /etc/init.d/ -perm -113 -exec ls -lh {} \;
 ---x--x-wx. 1 root root 0 Dec 24 11:22 /etc/init.d/test.text

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

[root@localhost ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls

395905   12 -rwsr-xr-x   1 abrt     abrt    9904 Nov 22  2013 /usr/libexec/abrt-action-

限于格式,只截取部分結果。

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

[root@localhost ~]# find /etc/ -not -perm -222 -exec ls -lh {} \;

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

[root@localhost etc]# find /etc -mtime -7 -not \( -user root -o -user hadoop \) -ls
667125    0 --w--w--w-   1 admin    admin           0 Dec 24 13:12 /etc/findtest.text
[root@localhost etc]#

以上示例為學生作業,如有錯誤之處和不足,歡迎指正!

原創文章,作者:diglinux,如若轉載,請注明出處:http://www.www58058.com/64639

(0)
diglinuxdiglinux
上一篇 2016-12-25
下一篇 2016-12-25

相關推薦

  • gawk基礎

     一、gawk 是什么 在了解gawk之前,先了解一下文本三工具      文本過濾工具:grep|egrep|fgrep   其中fgrep不支持正則表達式 1.行編輯器:sed      sed 有兩種空間   模式空間    保持空間 2. 文…

    Linux干貨 2016-06-24
  • haproxy實驗

    實驗1: 部署discuz 1、  不做會話綁定 基于roundrobin —————————10.1.72.40|30————————&#821…

    Linux干貨 2016-12-05
  • 如何學好C語言

    有人在酷殼的留言版上詢問下面的問題 keep_walker : 今天晚上我看到這篇文章。 http://programmers.stackexchange.com/questions/62502/small-c-projects 我也遇到了和提問的老外一樣的問題。。能給像遇到這樣煩惱的程序員一點建議嘛?謝謝! 我相信,這可能是很多朋友的問題,我以前…

    Linux干貨 2016-08-15
  • GPG——另一種加密信息的方式

    GPG ·使用gpg實現對稱加密 ·對稱加密file文件          gpg -c file          ls file.gpg ·在另一臺主機上解密file   &n…

    Linux干貨 2016-09-22
  • linux系統監控 sar命令詳解

    sar(System Activity Reporter系統活動情況報告)是目前 Linux 上最為全面的系統性能分析工具之一,可以從多方面對系統的活動進行報告, 包括:文件的讀寫情況、系統調用的使用情況、磁盤I/O、CPU效率、內存使用狀況、進程活動及IPC有關的活動等。 本文主要以CentOS 6.3 x64系統為例,介紹s…

    Linux干貨 2015-06-17
  • 文本處理和正則表達式練習(0805)

    1、找出ifconfig命令結果中本機的所有IPv4地址     1.1 Centos7     1.2 Centos6 2、查出分區空間使用率的最大百分比值,取各分區利用率的數值 取出各分區數值 取出最大百分比 3、查出用戶UID最大值的用戶名、 UID及shell類型 4、查出/tmp…

    Linux干貨 2016-08-06

評論列表(2條)

  • 馬哥教育
    馬哥教育 2017-02-17 11:03

    寫的很好,排版也很棒,提一個問題,255.255.255.255是一個合理的ip地址嗎?

    • diglinux
      diglinux 2017-02-17 11:22

      @馬哥教育謝謝,這不是個合理的ip,需要修正。

欧美性久久久久