vim,grep,shell腳本實例及find用法

vim,grep,shell腳本實例及find用法



1、定義一個對所有用戶都生效的命令別名

定義一個對所有用戶都生效的命令別名需要更改全局配置文件/etc/bashrc,

例如,我們以root用戶編輯/etc/bashrc,在文件的最後一行增加alias like='ls'

當我們新啓一個shell進程的時候,列出命令別名,會發現剛定義的別名like

[root@localhost ~]# tail /etc/bashrc
                . "$i" >/dev/null
            fi
        fi
    done

    unset i
    unset -f pathmunge
fi
#vim:ts=4:sw=4
alias like='ls'                        //此處顯示了我追加在文件最後的內容,定義別名like
[root@localhost ~]# alias
alias cls='clear'
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias like='ls'                        //別名like
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# like -l
總用量 12
drwxr-xr-x. 2 root  root      6 4月   3 00:02 00-02-17
-rw-r-----. 1 root  root   1257 3月  19 19:05 anaconda-ks.cfg
drwxr-xr-x. 2 root  root      6 3月  19 14:47 Desktop
drwxr-xr-x. 2 root  root      6 3月  19 14:47 Documents
drwxr-xr-x. 2 root  root      6 3月  19 14:47 Downloads
-rw-r--r--. 1 root  root    132 4月  30 16:44 grep.txt
-rwxr-xr-x. 1 jacky docker  511 4月  19 20:44 inittab
drwxr-xr-x. 2 root  root      6 3月  19 14:47 Music
drwxr-xr-x. 2 root  root      6 3月  19 14:47 Pictures
drwxr-xr-x. 2 root  root      6 3月  19 14:47 Public
drwxr-xr-x. 2 root  root      6 3月  19 14:47 Templates
drwxr-xr-x. 2 root  root      6 3月  19 14:47 Videos
drwxr-xr-x. 2 root  root      6 3月  19 14:47 桌面

2、顯示/etc/passwd文件中不以/bin/bash結尾的行。

~]# grep -v "/bin/bash$" /etc/passwd

3、找出/etc/passwd文件中,包含二位數子或三位數的行。

grep:    ~]# grep "\<[0-9]\{2,3\}\>" /etc/passwd

egrep:   ~]# egrep "\<[0-9]{2,3}\>" /etc/passwd

4、顯示/proc/meminfo文件中以大寫或小寫s開頭的行;至少以三種方式實現

A: ~]# grep "^[sS]" /proc/meminfo
B: ~]# egrep "^(s|S)" /proc/meminfo
C: ~]# grep "^s" /tmp/super.txt && grep "^S" /tmp/super.txt
D: ~]# grep -i "^s" /proc/meminfo
5、使用echo輸出一個絕對路徑,使用egrep取出路徑名,類型執行dirname /etc/passwd的結果

grep實現
[root@localhost ~]# echo /etc/rc.d/init.d/functions | grep -o "^/.*/"
/etc/rc.d/init.d/

egrep實現
[root@localhost ~]# echo /etc/rc.d/init.d/functions | grep -E -o "^/(.*)/"
/etc/rc.d/init.d/

6、找出ifconfig中的Ip地址。要求結果只顯示IP地址。

A: 解析:因爲IP地址的區間爲1.0.0.1 - 223.255.255.254,所以按照1-223.0-255.0-255.1-254去匹配
[root@localhost ~]# ifconfig | egrep -o "\<([1-9]|[1-9][0-9]|1[0-9]{2}|21[0-9]|22[0-3])\>.(\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>.){2}\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])\>"
192.168.220.7
127.0.0.1
192.168.122.1
B:取出網卡ens33的ip地址,先過濾到Ip的行再用cut命令,以空格爲分隔符取第10列,剛好就是IP地址
[root@localhost tmp]# ifconfig ens33 | grep "inet.*broad" | cut -d' ' -f10
192.168.220.7
7、vim定製自動縮進四個字符。

末行模式下

:set tabstop=4

8、編寫腳本,實現自動添加三個用戶,並計算這三個用戶的uid之和。

[root@localhost tmp]# cat test.sh 
#!/bin/bash

id neo || useradd neo
id jerry || useradd jerry
id louis || useradd louis
UID1=`id -u neo`
UID2=`id -u jerry`
UID3=`id -u louis`
echo "$[$UID1+$UID2+$UID3]"
[root@localhost tmp]# . test.sh 
id: neo: no such user
id: jerry: no such user
id: louis: no such user
15033

9、find用法以及常用用法的示例演示。

find: 實時查找工具,通過遍歷指定其實路徑下文件系統層級結構完成文件查找;

​ 工作特性:

​ 查找速度略慢;

​ 精確查找;

​ 實時查找;

用法:

find [OPTIONS] [查找起始路徑] [查找條件] [處理動作]處理動作可加ls 表明查找後結果按ls-l排列

查找起始路徑: 指定具體搜索目標起始路徑;默認爲當前目錄;

查找條件: 指定的查找標準,可以根據文件名,大小、類型、從屬關係、權限等等標準進行;默認爲找出指定路徑下的所有文件;

處理動作: 對符合查找天劍的文件作出的操作,例如刪除等操作;默認爲輸出至標準輸出;

查找條件:
        表達式:選項和測試

        測試:結果通常爲布爾型("true"or"false")

        根據文件名查找:
               -name "pattern"
               -iname "pattern"
                        (支持glob風格的通配符);
                        *,?,[],[^]

               -regex pattern:基於正則表達式模式查找文件,匹配是整個路徑,而非基名。

        根據文件從屬關係查找:
                -user USERNAME: 查找屬主指定用戶的所有文件;
                -group GRPNAME: 查找屬組指定組的所有文件;

                -uid UID:查找屬主指定UID的所有文件;
                -gid GID:查找屬組指定GID的所有文件;

                -nouser: 查找沒有屬主的文件;
                -nogroup: 查找沒有屬組的文件;

        根據文件的類型查找:
                -type TYPE:
                        f:普通文件
                        d:目錄文件
                        l:符號鏈接文件
                        b:塊設備文件
                        c:字符設備文件
                        p:管道文件
                        s:套接字文件

        組合測試:
               與:-a,默認組合邏輯;必須符合全部條件
               或:-o,符合其中之一條件即可
               非:-not,!

               !A -a !B = !(A -o B)
               !A -o !B = !(A -a B)

example.

example1
[root@localhost tmp]# find /tmp -nouser -type f -ls
67654200    4 -rw-r--r--   1 5010     5010          541 2月 13 17:48 /tmp/fstab
[root@localhost tmp]# find /tmp -nouser -a -type f -ls
67654200    4 -rw-r--r--   1 5010     5010          541 2月 13 17:48 /tmp/fstab
examle2
1、找出/tmp目錄下屬主爲非root的所有文件;

    ]# find /tmp -not -user root -ls

2、找出/tmp目錄下文件名中不包含fstab字符串的文件;
    ]# find /tmp -not -iname "*fatab*"

3、找出/tmp目錄下屬主爲非root,而且文件名不包含fstab字符串的文件;
    ]# find /tmp -not -user root -a -not -iname "*fstab*" -ls

或者
    ]# find /tmp -not \( -user root -o -iname "*fstab*" \) -ls

根據文件大小查找

-size [+|-]#UNIT
          常用單位: k,M,G
           #UNIT:(#-1,#]此處表示值大於-1的值 小於等於#值
           -#UNIT:[0,#-1]
           +#UNIT:(#,oo)

根據時間戳查找:

以“天”爲單位:
    -atime [+|-]#                                       /訪問時間
            #:[#,#-1)
            -#:(#,0]
            +#:(oo,#-1]  此處的#在後方表達式中是一個負數 

    -mtime                              /修改時間
    -ctime                              /改動時間                   

以“分鐘”爲單位:
    -amin
    -mmin
    -cmin
example.

找到/etc下 訪問時間在一週之前的文件
#] find /etc -atime +7 -ls

24小時內/etc下修改過的文件
#] find /etc -mtime -1 -ls

根據權限查找

-perm [/|-] mode
        mode: 精確權限匹配
        /mode: 任何一類用戶(u,g,o)的權限中的任何一位(r,d,x)符合條件即滿足;
                9位權限之間存在“或”關係

        -mode: 每一類用戶(u,g,o)的權限中的每一位(r,w,x)同時符合條件即滿足;
                9位權限之間存在“與”關係

example.

[root@localhost test]# touch a b c d e f g
[root@localhost test]# 
[root@localhost test]# chmod 640 a 
[root@localhost test]# chmod 666 b
[root@localhost test]# chmod 440 c
[root@localhost test]# chmod 775 d
[root@localhost test]# chmod 777 e
[root@localhost test]# ll
總用量 0
-rw-r-----. 1 root root 0 2月  15 15:50 a
-rw-rw-rw-. 1 root root 0 2月  15 15:50 b
-r--r-----. 1 root root 0 2月  15 15:50 c
-rwxrwxr-x. 1 root root 0 2月  15 15:50 d
-rwxrwxrwx. 1 root root 0 2月  15 15:50 e
-rw-r--r--. 1 root root 0 2月  15 15:50 f
-rw-r--r--. 1 root root 0 2月  15 15:50 g
[root@localhost test]# find ./ -perm 644 -ls
372669    0 -rw-r--r--   1 root     root            0 2月 15 15:50 ./f
372670    0 -rw-r--r--   1 root     root            0 2月 15 15:50 ./g

[root@localhost test]# find ./ -perm /222 -ls
372648    0 drwxr-xr-x   2 root     root           69 2月 15 15:50 ./
372650    0 -rw-r-----   1 root     root            0 2月 15 15:50 ./a
372665    0 -rw-rw-rw-   1 root     root            0 2月 15 15:50 ./b
372667    0 -rwxrwxr-x   1 root     root            0 2月 15 15:50 ./d
372668    0 -rwxrwxrwx   1 root     root            0 2月 15 15:50 ./e
372669    0 -rw-r--r--   1 root     root            0 2月 15 15:50 ./f
372670    0 -rw-r--r--   1 root     root            0 2月 15 15:50 ./g
[root@localhost test]# find ./ -perm /111 -ls
372648    0 drwxr-xr-x   2 root     root           69 2月 15 15:50 ./
372667    0 -rwxrwxr-x   1 root     root            0 2月 15 15:50 ./d
372668    0 -rwxrwxrwx   1 root     root            0 2月 15 15:50 ./e
[root@localhost test]# find ./ -perm /001 -ls
372648    0 drwxr-xr-x   2 root     root           69 2月 15 15:50 ./
372667    0 -rwxrwxr-x   1 root     root            0 2月 15 15:50 ./d
372668    0 -rwxrwxrwx   1 root     root            0 2月 15 15:50 ./e
[root@localhost test]# find ./ -perm /002 -ls
372665    0 -rw-rw-rw-   1 root     root            0 2月 15 15:50 ./b

處理動作

    -print: 輸出至標準輸出;默認的動作;
    -ls: 類似於對查找到的文件執行“ls-l”命令,輸出文件的詳細信息;
    -delete: 刪除查找到的文件;
    -fls /PATH/TO/SOMEFILE: 把查找到的所有文件的長格式信息保存至指定文件中(類似於把ls的輸出結果保存到指定文件中);
    -ok COMMAND {} \; :對查找到的每個文件執行有COMMAND表示的命令;每次操作都由用戶進行確認
    -exec COMMAND {} \;查找到的每個文件執行有COMMAND表示的命令;不用用戶確認直接執行命令

example.

                    [root@localhost tmp]# find ./ -nouser
                ./fstab
                [root@localhost tmp]# find ./ -nouser -a -nogroup
                ./fstab
                [root@localhost tmp]# find ./ -nouser -a -nogroup -ls
                67654200    4 -rw-r--r--   1 5010     5010          541 2月 13 17:48 ./fstab
                [root@localhost tmp]# find ./ -nouser -a -nogroup -ok chown root:root {} \;
                < chown ... ./fstab > ? y
                [root@localhost tmp]# ll
                總用量 12
                -rw-r--r--. 1 root  root  451 2月  13 17:48 crontab
                -rw-r--r--. 1 root  root  541 2月  13 17:48 fstab

                [root@localhost test]# find ./ -perm /002
                ./b
                ./e
                [root@localhost test]# find ./ -perm /002 -exec mv {} {}.danger \;
                [root@localhost test]# ll
                總用量 4
                -rw-r-----. 1 root root   0 2月  15 15:50 a
                -rw-r--r--. 1 root root 144 2月  15 16:13 abc.test
                -rw-rw-rw-. 1 root root   0 2月  15 15:50 b.danger
                -r--r-----. 1 root root   0 2月  15 15:50 c
                -rwxrwxr-x. 1 root root   0 2月  15 15:50 d
                -rwxrwxrwx. 1 root root   0 2月  15 15:50 e.danger
                -rw-r--r--. 1 root root   0 2月  15 15:50 f
                -rw-r--r--. 1 root root   0 2月  15 15:50 g
    注意: find傳遞查找到的文件路徑至後面的命令時,是先查找出所有符合條件的文件路徑,並一次性傳遞給後面的命令;
        但是有些命令不能接受過長的參數,此時命令執行會失敗;另一種方式可規避此問題;
            find | xargs COMMAND

參數代換命令:xargs

xargs [option] COMMAND

   -o:如果輸入的stdin含特殊字符,例如`,\,空格字符等,這個參數可以將它還原成一般字符,這個參數可以用於特殊狀態。

   -e:這個是EOF(end of file)的意思,後面接一個字符串char,當xargs分析到這個字符串是,就會停止繼續工作。
   -p: 在執行每個命令的參數時,都會詢問用戶的意思。
   -n:後面接次數,每次command命令執行時,要使用幾個參數的意思。

   當xargs後面沒有接任何命令時,默認是以echo來進行輸出。

example

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

[root@localhost ~]# find /var -user root -group mail -ls
67161923    0 drwxrwxr-x   2 root     mail          219 2月 14 14:43 /var/spool/mail

2、查找/usr目錄下不屬於root,bin或hadoop的所有文件或目錄;用兩種方法;

A.
[root@localhost ~]# find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls
 96954    0 drwx------   2 polkitd  root          287 3月 19  2019 /usr/share/polkit-1/rules.d
1000398   16 -rwsr-sr-x   1 abrt     abrt        15432 11月 14  2018 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
B.
[root@localhost ~]# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls
 96954    0 drwx------   2 polkitd  root          287 3月 19  2019 /usr/share/polkit-1/rules.d
1000398   16 -rwsr-sr-x   1 abrt     abrt        15432 11月 14  2018 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache

3、查找/etc目錄下最近一週內其內容修改過,且屬主不是root用戶也不是hadoop用戶的文件或目錄;

[root@localhost ~]# find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) -ls

4、查找當前系統上沒有屬主或屬組,且最近一週內曾被訪問過的文件或目錄;

[root@localhost ~]# find / \( -nouser -o -nogroup \) -atime -7 -ls
   105    0 drwx------   3 5010     5010           78 2月 14 14:43 /home/neo
67108939    0 drwxr-xr-x   4 5010     5010           39 3月 19  2019 /home/neo/.mozilla
134284490    0 drwxr-xr-x   2 5010     5010            6 6月 10  2014 /home/neo/.mozilla/extensions
201326666    0 drwxr-xr-x   2 5010     5010            6 6月 10  2014 /home/neo/.mozilla/plugins
find: ‘/proc/17459/task/17459/fd/5’: 沒有那個文件或目錄
find: ‘/proc/17459/task/17459/fdinfo/5’: 沒有那個文件或目錄
find: ‘/proc/17459/fd/6’: 沒有那個文件或目錄
find: ‘/proc/17459/fdinfo/6’: 沒有那個文件或目錄
67654182    0 -rw-rw----   1 5010     mail            0 2月 14 14:43 /var/spool/mail/neo

5、查找/etc目錄下大於1M且類型爲普通文件的所有文件;

[root@localhost ~]#  find /etc -size +1M -type f -exec ls -lh {} \;
-r--r--r--. 1 root root 7.2M 3月  19 2019 /etc/udev/hwdb.bin
-rw-r--r--. 1 root root 1.4M 3月  19 2019 /etc/selinux/targeted/contexts/files/file_contexts.bin
-rw-r--r--. 1 root root 3.7M 3月  19 2019 /etc/selinux/targeted/policy/policy.31
-rw-------. 1 root root 3.7M 3月  19 2019 /etc/selinux/targeted/active/policy.kern
-rw-------. 1 root root 3.7M 3月  19 2019 /etc/selinux/targeted/active/policy.linked
-rw-r--r--. 1 root root 1.4M 4月  11 2018 /etc/brltty/zh-tw.ctb

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

[root@localhost ~]# find /etc -not -perm /222 -ls
33615593  180 -r--r--r--   1 root     root       183421 3月 19  2019 /etc/pki/ca-trust/extracted/java/cacerts
67181253  328 -r--r--r--   1 root     root       334001 3月 19  2019 /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
100708941  248 -r--r--r--   1 root     root       251593 3月 19  2019 /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
100708942  200 -r--r--r--   1 root     root       201168 3月 19  2019 /etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
100708943  168 -r--r--r--   1 root     root       171863 3月 19  2019 /etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
235701    4 -r--r--r--   1 root     root          531 5月  3  2017 /etc/lvm/profile/cache-mq.profile
235702    4 -r--r--r--   1 root     root          339 5月  3  2017 /etc/lvm/profile/cache-smq.profile
235703    4 -r--r--r--   1 root     root         3020 8月  5  2017 /etc/lvm/profile/command_profile_template.profile
235704    4 -r--r--r--   1 root     root         2309 5月  3  2017 /etc/lvm/profile/lvmdbusd.profile
235705    4 -r--r--r--   1 root     root          828 8月  5  2017 /etc/lvm/profile/metadata_profile_template.profile
235706    4 -r--r--r--   1 root     root           76 5月  3  2017 /etc/lvm/profile/thin-generic.profile
235707    4 -r--r--r--   1 root     root           80 5月  3  2017 /etc/lvm/profile/thin-performance.profile
100769850    4 -r--------   1 root     root           45 3月 19  2019 /etc/openldap/certs/password
67183454    4 ----------   1 root     root          975 2月 16 11:25 /etc/gshadow
34025845    4 -r--r--r--   1 root     root          460 4月 11  2018 /etc/dbus-1/system.d/cups.conf
67183449    4 ----------   1 root     root         1608 2月 16 11:25 /etc/shadow
67294134    4 ----------   1 root     root         1579 2月 15 12:18 /etc/shadow-
100861801    4 -r--r--r--   1 root     root           63 8月 23  2017 /etc/ld.so.conf.d/kernel-3.10.0-693.el7.x86_64.conf
100821074    4 -r--r--r--   1 root     root           63 3月 18  2019 /etc/ld.so.conf.d/kernel-3.10.0-957.10.1.el7.x86_64.conf
102850752 7348 -r--r--r--   1 root     root      7522390 3月 19  2019 /etc/udev/hwdb.bin
67294386    4 -r--r--r--   1 root     root           33 3月 19  2019 /etc/machine-id
790438    4 -r--r--r--   1 root     root          146 4月 11  2018 /etc/pam.d/cups
67191422    4 ----------   1 root     root          964 2月 15 12:18 /etc/gshadow-
67513217    4 -r--r-----   1 root     root         3938 6月  7  2017 /etc/sudoers

7、查找/etc目錄至少一類用戶沒有執行權限的文件;

[root@localhost ~]# find /etc -not -perm -111 -ls

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

[root@localhost ~]# find /etc/init.d/ -perm -113 -ls
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章