grep命令和find命令的練習

1、顯示系統上root、Fedora、或user1用戶的默認shell

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

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

[root@localhost ~]# grep -E -o  "[_[:alnum:]]+\(\)" /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@localhost ~]# echo $(which passwd)
/usr/bin/passwd
[root@localhost ~]# echo $(which passwd) | grep -E -o "[^/]+$"
passwd
[root@localhost ~]# echo $(which passwd) | grep -E -o "^/.*/" | grep -E -o "^/.*[^/]"
/usr/bin

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

[root@localhost ~]# ifconfig | grep -E -o "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>" | sort -n | uniq
1
2
3
6
9
14
15
29
64
73
127
128
168
192
250
255

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

[root@localhost ~]# ifconfig | grep -E -o "(\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>" 
192.168.2.250
255.255.255.0
192.168.2.255
127.0.0.1
255.0.0.0

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

grep -E "^([a-zA-Z0-9_-\.+]+@[a-zA-Z0-9_.]+\.[a-zA-Z]{2,5})$"

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

[root@localhost ~]# find /var -user root -group mail -ls
134321240    4 drwxrwxr-x   2 root     mail         4096 Jul 22 00:12 /var/spool/mail
115656    0 drwx------   2 root     mail            6 Jun  9  2014 /var/spool/mqueue

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

[root@localhost ~]# find / \( -nouser -a -nogroup \) -ls
134217856    0 drwx------   2 1001     1001           59 7月  6 20:58 /home/slackware
134217857    4 -rw-r--r--   1 1001     1001           18 11月 20  2015 /home/slackware/.bash_logout
134217858    4 -rw-r--r--   1 1001     1001          193 11月 20  2015 /home/slackware/.bash_profile
134217859    4 -rw-r--r--   1 1001     1001          231 11月 20  2015 /home/slackware/.bashrc
find: ‘/proc/5115/task/5115/fd/6’: 沒有那個文件或目錄
find: ‘/proc/5115/task/5115/fdinfo/6’: 沒有那個文件或目錄
find: ‘/proc/5115/fd/6’: 沒有那個文件或目錄
find: ‘/proc/5115/fdinfo/6’: 沒有那個文件或目錄
429338    0 drwx------   2 4006     4006           59 7月 17 07:12 /users/www
429339    4 -rw-r--r--   1 4006     4006           18 11月 20  2015 /users/www/.bash_logout
429340    4 -rw-r--r--   1 4006     4006          193 11月 20  2015 /users/www/.bash_profile
429341    4 -rw-r--r--   1 4006     4006          231 11月 20  2015 /users/www/.bashrc

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

[root@localhost ~]# find / \( -nouser -a -nogroup \) -a -atime -3 -ls 
134217856    0 drwx------   2 1001     1001           59 7月  6 20:58 /home/slackware
find: ‘/proc/5113/task/5113/fd/6’: 沒有那個文件或目錄
find: ‘/proc/5113/task/5113/fdinfo/6’: 沒有那個文件或目錄
find: ‘/proc/5113/fd/6’: 沒有那個文件或目錄
find: ‘/proc/5113/fdinfo/6’: 沒有那個文件或目錄
429338    0 drwx------   2 4006     4006           59 7月 17 07:12 /users/www

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

[root@localhost ~]# find /etc -perm -222 -ls
134320260    0 lrwxrwxrwx   1 root     root           17 7月  6 13:10 /etc/mtab -> /proc/self/mounts
67323147    0 lrwxrwxrwx   1 root     root           49 7月  6 13:10 /etc/pki/tls/certs/ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
67323148    0 lrwxrwxrwx   1 root     root           55 7月  6 13:10 /etc/pki/tls/certs/ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
109446    0 lrwxrwxrwx   1 root     root           49 7月  6 13:10 /etc/pki/tls/cert.pem -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
109451    0 lrwxrwxrwx   1 root     root           59 7月  6 13:10 /etc/pki/ca-trust/source/ca-bundle.legacy.crt -> /usr/share/pki/ca-trust-legacy/ca-bundle.legacy.default.crt
201652199    0 lrwxrwxrwx   1 root     root           40 7月  6 13:10 /etc/pki/java/cacerts -> /etc/pki/ca-trust/extracted/java/cacerts
134320284    0 lrwxrwxrwx   1 root     root           14 7月  6 13:10 /etc/redhat-release -> centos-release
134320285    0 lrwxrwxrwx   1 root     root           14 7月  6 13

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

[root@localhost ~]# find /etc -size +1M -a -type f -exec ls -alh {} \;
-r--r--r--. 1 root root 6.7M 7月   6 13:18 /etc/udev/hwdb.bin
-rw-r--r--. 1 root root 3.7M 11月 20 2015 /etc/selinux/targeted/policy/policy.29

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

(1)
N27_shijinlongN27_shijinlong
上一篇 2017-07-30 20:58
下一篇 2017-07-30 21:06

相關推薦

  • week3:cut,sort,grep等shell工具的應用及用戶,組管理類命令

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意,同一個用戶登錄多次只顯示一次即可    who | cut -d' ' -f1 | sort –u 2、取出最后登錄到當前系統的用戶相關信息    who | cut -d' ' -f1 | tail -1 | id 3、取出當…

    Linux干貨 2016-11-20
  • 第五周作業

    1、顯示當前系統上root、fedora或user1用戶的默認shell; [root@localhost ~]# awk -F: '/^(root|user1|fedora)/{print $1,"shell is",$NF}' /etc/pass…

    Linux干貨 2016-09-15
  • Linux GNU AWK使用

                                    Linux GNU AWK使用 本章內容: awk介紹 awk基本用法 awk變量 awk格式化 awk操作符 awk條件判斷…

    系統運維 2016-10-01
  • 磁盤管理

    回顧 {a..f} a b c d e f ]# arr=({a..z}) ]# echo ${#arr[*]} 26 ]# N1=N2 ]# N2=zhuchaoming ]# echo ${!N1} eval N1=\$$N2 echo $N1 創建臨時文件 mktemp NAME.XXX 交互式登錄 /etc/profile –> /…

    Linux干貨 2016-09-07
  • linux基礎命令1

    內容包含:文件管理、重定向和管道、用戶組和權限管理、文本處理工具

    Linux干貨 2017-11-19
  • Linux網絡管理

                          Linux網絡管理   本章內容: 網絡概念 OSI七層模型 網絡設備 TCP/IP IP地址 配置網絡 實現網絡組 測試網絡 網絡工具   網絡用戶應用程序: WEB瀏覽器(…

    系統運維 2016-09-12

評論列表(1條)

  • 馬哥教育
    馬哥教育 2017-08-04 16:14

    find的功能非常強大,學有余力建議深入學習。

欧美性久久久久