加深對grep命令和find命令的熟悉

1、顯示當前系統上root、fedorauser1用戶的默認shell。

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


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

grep -E -o "[[:alpha:]]+\(\)" /etc/rc.d/init.d/functions
[root@localhost ~]# grep -E -o “[[:alpha:]]+\(\)” /etc/rc.d/init.d/functions
str()
checkpid()
readlink()
fgrep()
loop()
loop()
run()
pidof()
daemon()
killproc()
pidfileofproc()
pidofproc()
status()
success()
failure()
passed()
warning()
stage()
success()
failure()
passed()
warning()
action()
strstr()
confirm()
dev()
file()
true()
false()
sysctl()
random()
point()
crypto()

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

echo "/var/log/messages"|grep -Eo  "[^/]+\/?$"
[root@localhost ~]# echo “/var/log/messages”|grep -Eo  “[^/]+\/?$”
messages

 echo "/var/log/messages"|grep -Eo  "^/.*/"
[root@localhost ~]# echo “/var/log/messages”|grep -Eo  “^/.*/”
/var/log/

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

ifconfig |grep -Eo "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"
[root@localhost ~]# ifconfig |grep -Eo “\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>”
29
192
168
3
35
192
168
3
255
255
255
255
64
1
7
6
127
1
255
1
128
1
4
4
240
240
240
240

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

[root@localhost ~]# ifconfig |grep -Eo "([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
[root@localhost ~]# ifconfig |grep -Eo “([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])”
192.168.3.35
192.168.3.255
255.255.255.0
127.0.0.1
255.0.0.0

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

grep -E "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+/.[A-Za-z]{2,6}"

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

find /var -user root -group mail -ls
[root@localhost ~]# find /var -user root -group mail -ls
1440148    4 drwxrwxr-x   2 root     mail         4096 7月 31 01:54 /var/spool/mail


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

find / -nouser -o -nogroup -ls

find / ( -nouser -o -nogroup ) -atime -3 -ls

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

find /etc -perm -222 -ls

結果全部為鏈接文件。


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

find /etc -size +1M -type f -ls
[root@localhost ~]# find /etc -size +1M -type f -ls
1320064 2148 -rw-r–r–   1 root     root      2198778 7月 21 18:12 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
1191199 7124 -rw-r–r–   1 root     root      7292689 7月 21 18:11 /etc/selinux/targeted/policy/policy.24
1191196 7124 -rw-r–r–   1 root     root      7292689 7月 21 18:11 /etc/selinux/targeted/modules/active/policy.kern

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

(0)
N27_huiyezhichengN27_huiyezhicheng
上一篇 2017-07-30 20:17
下一篇 2017-07-30 20:47

相關推薦

  • N21沉舟第七周作業

    1、創建一個10G分區,并格式為ext4文件系統;    (1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;    (2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳; mke2fs -t ex…

    Linux干貨 2016-08-22
  • 馬哥教育網絡21期+第五周練習博客

    1、顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行; [root@localhost ~]#  grep '[^[:space:]]\+' /boot/grub2/grub.cfg 2、顯示/etc/rc.d/rc.sysinit文件中以#開頭,后面跟至少一個空白字符…

    Linux干貨 2016-08-08
  • Linux文本處理工具grep,egrep

    簡介:     grep即(Global search REgular expression and Print out the line)全局的搜索正則表達式并且打印顯示出來。     通俗點講:根據用戶指定的文本模式(搜索條件)對目標文件進行逐行搜索,顯示能匹配到的行。 &n…

    Linux干貨 2015-05-11
  • 關于RHEL6中ulimit的nproc限制

    ulimit 用于限制 shell 啟動進程所占用的資源,支持以下各種類型的限制:所創建的內核文件的大小、進程數據塊的大小、Shell 進程創建文件的大小、內存鎖住的大小、常駐內存集的大小、打開文件描述符的數量、分配堆棧的最大大小、CPU 時間、單個用戶的最大線程數、Shell 進程所能使用的最大虛…

    Linux干貨 2016-10-19
  • 學習linux第四天

    Linux學習第4天   Linux內部命令和外部命令    內部命令:實際上是shell程序的一部分,其中包含的是一些比較簡單的linux系統命令,這些命令由shell程序識別并在shell程序內部完成運行,通常在linux系統加載運行時shell就被加載并駐留在系統內存中。內部命令是寫在bashy源碼里面的,其執行速度比外部…

    Linux干貨 2016-10-29

評論列表(1條)

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

    看來grep和find已經很好掌握了,如果再加上sed和awk就理完美了,加油。

欧美性久久久久