第03周作業(答題)
-
定義一個對所有用戶都生效的命令別名,例如:lftps='lfpt 172.168.0.1/pub'
- 答:可以有兩種設置方式:
- 按照配置文件的功能分類的設置方式(傳統方式),可以修改/etc/bashrc文件,在其末尾添加如下命令:
alias lftps='lftp 172.168.0.1/pub'
-
按照配置文件註釋中建議的方式(針對新系統,主要是防止在系統更新的過程中被新的默認配置覆蓋),可以root用戶身份在/etc/profile.d/下新建一腳本文件lftps.sh,編輯其內容爲:
#!/bin/bash alias lftps='lftp 172.168.0.1/pub'
- 顯示/etc/passwd文件中不以/bin/bash結尾的行
- 答:執行以下命令
[root@localhost ~]# grep -v "/bin/bash$" /etc/passwd
- 答:執行以下命令
- 找出/etc/passwd文件中,包含二位數字或者三位數字的行
- 答:有兩種方式,得出的結果不一定相同,可依需要選用
# 方式 1:此種方式不會過濾出形如“myuser123”的單詞所在的行 [root@localhost ~]# grep "\<[0-9]\{2,3\}\>" /etc/passwd
# 方式 2:此種方法可以過濾出形如“myuser123”的單詞所在的行 [root@localhost ~]# grep "[^[0-9]]*[0-9]\{2,3\}[^[0-9]]*" /etc/passwd
- 答:有兩種方式,得出的結果不一定相同,可依需要選用
- 顯示/proc/meminfo文件中以大寫或小寫S頭的行;用三種方式實現
- 答:
# 方式 1: [root@localhost ~]# grep "^[Ss]" /proc/meminfo
# 方式 2: [root@localhost ~]# grep -i "^[s]" /proc/meminfo
# 方式 3: [root@localhost ~]# grep -E "^(S|s)" /proc/meminfo
- 答:
- 使用echo輸出一個絕對路徑,使用egrep取出路徑名,類似執行dirname /etc/passwd的結果
- 答:
# 方法 1:此種方法可以正確處理/PATH/TO/FILE形式的路徑, # 也可以正確得到/FILE或/PATH形式路徑的路徑名“/”, # 但是不能處理/PATH/TO/FILE/形式的路徑 [root@localhost ~]# echo /etc/yum/pluginconf.d/langpacks.conf | egrep -o "^.*[/]" /etc/yum/pluginconf.d/ [root@localhost ~]#
# 方法 2:此種方法可以正確處理/PATH/TO/FILE形式及/PATH/TO/FILE/形式的路徑 # 但不能正確處理/FILE、/PATH、/FILE/、/PATH/形式的路徑,得到的結果爲空字符串 [root@localhost ~]# echo /etc/yum/pluginconf.d/langpacks.conf | egrep -o "^.*[^/]" | egrep -o "^.*[/]" | egrep -o "^.*[^/]" /etc/yum/pluginconf.d [root@localhost ~]#
- 答:
- 指出ifconfig中的IP地址,要求結果只顯示IP地址
- 答:
# 說明:過濾出所有的主機地址,包括組播地址 [root@localhost ~]# ifconfig | egrep -o "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>\.(\<([0-9]{1,2}|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.169.43.83 127.0.0.1 [root@localhost ~]#
- 答:
-
VIM定製自動縮進4個字符
- 答:編輯/etc/vimrc(全局)或~/.vimrc(個人),加入以下設置或將已有設置修改爲如下值:
set autoindent set smartindent set cindent set tabstop=4 set softtabstop=4 set shiftwidth=4
- 答:編輯/etc/vimrc(全局)或~/.vimrc(個人),加入以下設置或將已有設置修改爲如下值:
-
編寫腳本,實現自動添加三個用戶,並計算這三個用戶的UID之和
- 答:
#!/bin/bash # # 方法 1:通過查詢/etc/passwd文件得到新加用戶的UID #
useradd user1
useradd user2
useradd user3let UID1=$(grep "user1" /etc/passwd | cut -d":" -f3)
let UID2=$(grep "user2" /etc/passwd | cut -d":" -f3)
let UID3=$(grep "user3" /etc/passwd | cut -d":" -f3)let UIDSUM=$[$UID1 + $UID2 + $UID3]
echo $UIDSUM
#!/bin/bash
#方法 2:通過id命令得到新加用戶的UID
#
useradd user1
useradd user2
useradd user3let UID1=$(id -u user1)
let UID2=$(id -u user2)
let UID3=$(id -u user3)let UIDSUM=$[$UID1 + $UID2 + $UID3]
echo $UIDSUM
- 答:
-
find用法及常用用法的實例演示
- 答:find命令可在指定路徑下以文件名稱、從屬關係、文件類型、文件大小、時間戳、權限等信息做爲條件查找文件或目錄,其命令格式爲:
[root@localhost ~]# find [OPTION...] [起始路徑] [查找條件] [處理動作]
- 格式說明:
- 起始路徑:指定具體的搜索目標起始路徑,默認爲當前目錄
- 查找條件:指定的查找標準,可以根據文件名、大小、類型、從屬關係、權限等進行查找,由“選項”和“測試”組成,默認爲查找所有文件
- 處理動作:指定對符合條件的文件做出的操作
-
常見用法示例:
-
查找動作:
- 根據文件名查找:
# 示例 1:直接指定文件名進行查找,本例在/etc目錄及其子目錄下查找所有名稱爲“profile”的文件或目錄 [root@localhost ~]# find /etc -name profile /etc/lvm/profile /etc/profile [root@localhost ~]#
# 示例 2:通過glob風格的通配符進行查找,本倒在/etc目錄及其子目錄下查找所有名稱以“x”開頭,後跟任意個有效字符的文件或目錄 [root@localhost ~]# find /etc -name x* /etc/X11/xorg.conf.d /etc/xdg /etc/xinetd.d /etc/selinux/targeted/contexts/users/xguest_u /etc/selinux/targeted/contexts/x_contexts /etc/selinux/targeted/active/modules/100/xen /etc/selinux/targeted/active/modules/100/xguest /etc/selinux/targeted/active/modules/100/xserver /etc/vmware-tools/vgauth/schemas/xenc-schema.xsd /etc/vmware-tools/vgauth/schemas/xml.xsd /etc/vmware-tools/vgauth/schemas/xmldsig-core-schema.xsd [root@localhost ~]#
# 示例 3:通過glob風格的通配符進行查找,本例在/etc目錄及其子目錄下查找所有名稱以“X”開頭,後跟任意個有效字符的文件或目錄 [root@localhost ~]# find /etc -name X* /etc/X11 /etc/vmware-tools/vgauth/schemas/XMLSchema-hasFacetAndProperty.xsd /etc/vmware-tools/vgauth/schemas/XMLSchema-instance.xsd /etc/vmware-tools/vgauth/schemas/XMLSchema.dtd /etc/vmware-tools/vgauth/schemas/XMLSchema.xsd [root@localhost ~]#
# 示例 4:通過glob風格的通配符進行查找,本例在/etc目錄及其子目錄下查找所有名稱以“X”或“x”開頭,後跟任意個有效字符的文件或目錄 [root@localhost ~]# find /etc -name [Xx]* /etc/X11 /etc/X11/xorg.conf.d /etc/xdg /etc/xinetd.d /etc/selinux/targeted/contexts/users/xguest_u /etc/selinux/targeted/contexts/x_contexts /etc/selinux/targeted/active/modules/100/xen /etc/selinux/targeted/active/modules/100/xguest /etc/selinux/targeted/active/modules/100/xserver /etc/vmware-tools/vgauth/schemas/XMLSchema-hasFacetAndProperty.xsd /etc/vmware-tools/vgauth/schemas/XMLSchema-instance.xsd /etc/vmware-tools/vgauth/schemas/XMLSchema.dtd /etc/vmware-tools/vgauth/schemas/XMLSchema.xsd /etc/vmware-tools/vgauth/schemas/xenc-schema.xsd /etc/vmware-tools/vgauth/schemas/xml.xsd /etc/vmware-tools/vgauth/schemas/xmldsig-core-schema.xsd [root@localhost ~]#
# 示例 5:通過glob風格的通配符進行查找,本例在/etc目錄及其子目錄下查找所有名稱以“x”(忽略大小寫)開頭,後跟任意個有效字符的文件或目錄 [root@localhost ~]# find /etc -iname x* /etc/X11 /etc/X11/xorg.conf.d /etc/xdg /etc/xinetd.d /etc/selinux/targeted/contexts/users/xguest_u /etc/selinux/targeted/contexts/x_contexts /etc/selinux/targeted/active/modules/100/xen /etc/selinux/targeted/active/modules/100/xguest /etc/selinux/targeted/active/modules/100/xserver /etc/vmware-tools/vgauth/schemas/XMLSchema-hasFacetAndProperty.xsd /etc/vmware-tools/vgauth/schemas/XMLSchema-instance.xsd /etc/vmware-tools/vgauth/schemas/XMLSchema.dtd /etc/vmware-tools/vgauth/schemas/XMLSchema.xsd /etc/vmware-tools/vgauth/schemas/xenc-schema.xsd /etc/vmware-tools/vgauth/schemas/xml.xsd /etc/vmware-tools/vgauth/schemas/xmldsig-core-schema.xsd [root@localhost ~]#
# 示例 6:基於正則表達式模式,在整個文件名(包括路徑)中進行匹配,查找文件。本例中查找/etc目錄及其子目錄下,所有文件名(包括路徑)中存在“yum”和“.d”兩個字符串的文件或目錄 [root@localhost ~]# find /etc -regex ".*\<yum\>\..*\.\<d\>.*" /etc/yum.repos.d /etc/yum.repos.d/CentOS-Media.repo /etc/yum.repos.d/CentOS-163.repo [root@localhost ~]#
# 示例 7:基於正則表達式模式,在整個文件名(包括路徑)中進行匹配,查找文件,查找過程中忽略大小寫。本例中查找/etc目錄及其子目錄下,所有文件名(包括路徑)中存在“yum”和“.d”兩個字符串的文件或目錄(忽略大小寫) [root@localhost ~]# find /etc -iregex ".*\<yum\>\..*\.\<d\>.*" /etc/yum.repos.d /etc/yum.repos.d/CentOS-Media.repo /etc/yum.repos.d/CentOS-163.repo /etc/yum.repos.d/yum.test.D [root@localhost ~]#
- 根據文件從屬關係查找:
# 預設實驗環境:/tmp目錄下有如下文件,其中:test.myuser文件無屬組(已被刪除);test.user4文件無屬主、屬組(已被刪除) [root@localhost ~]# ls -l /tmp total 0 -rw-r--r--. 1 myuser 5017 0 Jul 11 15:51 test.myuser -rw-r--r--. 1 user1 mygrp 0 Jul 11 15:53 test.user1 -rw-r--r--. 1 user2 mygrp 0 Jul 11 15:53 test.user2 -rw-r--r--. 1 user3 mygrp 0 Jul 11 15:53 test.user3 -rw-r--r--. 1 5004 5004 0 Jul 11 15:55 test.user4 [root@localhost ~]#
# 示例 1:根據文件的屬主進行查找,本例查找/tmp目錄及其子目錄下所有屬主爲“myuser”的文件 [root@localhost ~]# find /tmp -user myuser /tmp/test.myuser [root@localhost ~]#
# 示例 2:根據文件的屬組進行查找,本例查找/tmp目錄及其子目錄下所有屬主爲“mygrp”的文件 [root@localhost ~]# find /tmp -group mygrp /tmp/test.user1 /tmp/test.user2 /tmp/test.user3 [root@localhost ~]#
# 示例 3:根據文件屬主的UID進行查找,本例在/tmp目錄及其子目錄下根據屬主UID“5015”查找文件 [root@localhost ~]# id -u myuser 5015 [root@localhost ~]# [root@localhost ~]# find /tmp -uid 5015 /tmp/test.myuser [root@localhost ~]#
# 示例 4:根據文件屬組的GID進行查找,本例在/tmp目錄及其子目錄下根據屬組GID“5011”查找文件 [root@localhost ~]# grep "^mygrp" /etc/group | cut -d":" -f3 5011 [root@localhost ~]# [root@localhost ~]# find /tmp -gid 5011 /tmp/test.user1 /tmp/test.user2 /tmp/test.user3 [root@localhost ~]#
# 示例 5:查找沒有屬主的文件(屬主已被刪除),本例查找/tmp目錄及其子目錄下所有無屬主(屬主己刪除)的文件 [root@localhost ~]# find /tmp -nouser /tmp/test.user4 [root@localhost ~]#
# 示例 6:查找沒有屬組的文件(屬組已被刪除),本例查找/tmp目錄及其子目錄下所有無屬組(屬組己刪除)的文件 [root@localhost ~]# find /tmp -nogroup /tmp/test.myuser /tmp/test.user4 [root@localhost ~]#
-
根據文件類型查找:
# 預設實驗環境:在/tmp目錄下有如下文件 [root@localhost ~]# tree /tmp /tmp ├── test.dir │ ├── test.sub │ └── test.subdir │ └── test.sublink -> /tmp/test.dir/test.sub ├── test.link -> test.myuser └── test.myuser 2 directories, 4 files [root@localhost ~]# [root@localhost ~]# ls -l /tmp total 0 drwxr-xr-x. 3 root root 41 Jul 11 16:31 test.dir lrwxrwxrwx. 1 root root 11 Jul 11 16:20 test.link -> test.myuser -rw-r--r--. 1 myuser 5017 0 Jul 11 15:51 test.myuser [root@localhost ~]# [root@localhost ~]# ls -l /tmp/test.dir total 0 -rw-r--r--. 1 root root 0 Jul 11 16:30 test.sub drwxr-xr-x. 2 root root 22 Jul 11 16:32 test.subdir [root@localhost ~]# [root@localhost ~]# ls -l /tmp/test.dir/test.subdir total 0 lrwxrwxrwx. 1 root root 22 Jul 11 16:41 test.sublink -> /tmp/test.dir/test.sub [root@localhost ~]#
# 示例 1:查找/tmp目錄及其子目錄下所有普通文件 [root@localhost ~]# find /tmp -type f /tmp/test.myuser /tmp/test.dir/test.sub [root@localhost ~]#
# 示例 2:查找/tmp目錄及其子目錄下所有目錄文件 [root@localhost ~]# find /tmp -type d /tmp /tmp/test.dir /tmp/test.dir/test.subdir [root@localhost ~]#
# 示例 3:查找/tmp目錄及其子目錄下所有符號鏈接文件 [root@localhost ~]# find /tmp -type l /tmp/test.dir/test.subdir/test.sublink /tmp/test.link [root@localhost ~]#
# 示例 4:查找/dev目錄及其子目錄下所有的塊設備文件 [root@localhost ~]# find /dev -type b /dev/dm-1 /dev/dm-0 /dev/sr0 /dev/sda2 /dev/sda1 /dev/sda [root@localhost ~]#
# 示例 5:查找/dev目錄及其子目錄下所有的字符設備文件 [root@localhost ~]# find /dev -type c /dev/vsock /dev/vcsa6 /dev/vcs6 ... /dev/null /dev/mem /dev/vga_arbiter [root@localhost ~]#
# 示例 6:查找/run目錄及其子目錄下所有的管道文件 [root@localhost ~]# find /run -type p /run/dmeventd-client /run/dmeventd-server /run/systemd/inhibit/1.ref /run/systemd/sessions/1.ref /run/systemd/initctl/fifo [root@localhost ~]#
# 示例 7:查找/run目錄及其子目錄下所有的套接字文件 [root@localhost ~]# find /run -type s /run/chrony/chronyd.sock /run/vmware/guestServicePipe /run/dbus/system_bus_socket /run/lvm/lvmetad.socket /run/lvm/lvmpolld.socket /run/udev/control /run/systemd/shutdownd /run/systemd/private /run/systemd/journal/socket /run/systemd/journal/stdout /run/systemd/cgroups-agent /run/systemd/notify [root@localhost ~]#
- 根據文件的大小查找:文件大小單位可爲:b(塊、512字節)、c(字節)、k(1024字節)、M(1024k)、G(1024M)等,如不指定則以塊爲單位
# 預設實驗環境:在/tmp/test目錄下有如下文件 [root@localhost ~]# ls -l /tmp/test total 32 -rw-r--r--. 1 root root 1024 Jul 11 17:02 file1k -rw-r--r--. 1 root root 1000 Jul 11 17:14 file1KB -rw-r--r--. 1 root root 2048 Jul 11 17:03 file2k -rw-r--r--. 1 root root 2000 Jul 11 17:15 file2KB -rw-r--r--. 1 root root 3072 Jul 11 17:03 file3k -rw-r--r--. 1 root root 3000 Jul 11 17:15 file3KB -rw-r--r--. 1 root root 500 Jul 11 17:25 file500B -rw-r--r--. 1 root root 512 Jul 11 17:25 file512B [root@localhost ~]#
# 示例 1:查找大小爲1b(塊),即大於0、小於等於512字節的文件,字節爲默認單位,可以省略 [root@localhost ~]# find /tmp/test -size 1b /tmp/test /tmp/test/file512B /tmp/test/file500B [root@localhost ~]#
# 示例 2:查找大小爲2b(塊),即大於512、小於等於1024字節的文件,字節爲默認單位,可以省略 [root@localhost ~]# find /tmp/test/ -size 2 /tmp/test/file1k /tmp/test/file1KB [root@localhost ~]#
# 示例 3:查找小於2b(小於等於512節字)的文件,字節爲默認單位,可以省略 [root@localhost ~]# find /tmp/test/ -size -2 /tmp/test/ /tmp/test/file512B /tmp/test/file500B [root@localhost ~]#
# 示例 4:查找小於3k(小於等於2048節字)的文件,此處的單位不可省略 [root@localhost ~]# find /tmp/test/ -size -3k /tmp/test/ /tmp/test/file1k /tmp/test/file2k /tmp/test/file1KB /tmp/test/file2KB /tmp/test/file512B /tmp/test/file500B [root@localhost ~]#
# 示例 5:查找大於1b(大於512字節)的文件, [root@localhost ~]# find /tmp/test/ -size +1 /tmp/test/file1k /tmp/test/file2k /tmp/test/file3k /tmp/test/file1KB /tmp/test/file2KB /tmp/test/file3KB [root@localhost ~]#
# 示例 6:查找大小2k(大於2048字節)的文件 [root@localhost ~]# find /tmp/test/ -size +2k /tmp/test/file3k /tmp/test/file3KB [root@localhost ~]#
-
根據時間戳查找:
-
以“天”爲單位查找
# 預設實驗環境:在/tmp/test目錄下有如下文件 [root@localhost ~]# ls 20190703 20190707 20190711 [root@localhost ~]# # 各文件時間戳信息如下: [root@localhost ~]# stat --printf "Name:\t%n \nAccess:\t%x \nModify:\t%y \n\n" 20190703 20190707 20190711 Name: 20190703 Access: 2019-07-03 12:00:00.000000000 +0800 Modify: 2019-07-03 12:00:00.000000000 +0800 Name: 20190707 Access: 2019-07-07 12:00:00.000000000 +0800 Modify: 2019-07-07 12:00:00.000000000 +0800 Name: 20190711 Access: 2019-07-11 12:00:00.000000000 +0800 Modify: 2019-07-11 12:00:00.000000000 +0800 [root@localhost ~]# # 當前系統時間爲: [root@localhost ~]# date Thu Jul 11 18:32:42 CST 2019 [root@localhost ~]#
# 示例 1:以當前時間點爲準,查找訪問時間距現在0天(超過或等於0,不足24小時)的文件 [root@localhost ~]# find /tmp/test -atime 0 /tmp/test /tmp/test/20190711 [root@localhost ~]#
# 示例 2:以當前時間點爲準,查找訪問時間距現在4天(超過或等於120小時,不足144小時)的文件 [root@localhost ~]# find /tmp/test -atime 4 /tmp/test/20190707 [root@localhost ~]#
# 示例 3:以當前時間點爲準,查找訪問時間距現在多於0天(超過或等於24小時)的文件 [root@localhost ~]# find /tmp/test -atime +0 /tmp/test/20190707 /tmp/test/20190703 [root@localhost ~]#
# 示例 4:以當前時間點爲準,查找訪問時間距現在多於4天(超過或等於120小時)的文件 [root@localhost ~]# find /tmp/test -atime +4 /tmp/test/20190703 [root@localhost ~]#
# 示例 5:以當前時間點爲準,查找訪問時間距現在不足1天(不足24小時)的文件 [root@localhost ~]# find /tmp/test -atime -1 /tmp/test /tmp/test/20190711 [root@localhost ~]#
# 示例 6:以當前時間點爲準,查找訪問時間距現在不足5天(不足120小時)的文件 [root@localhost ~]# find /tmp/test -atime -5 /tmp/test /tmp/test/20190707 /tmp/test/20190711 [root@localhost ~]#
- 也可使用-mtime選項根據修改時間查找或使用-ctime選項根據改變時間查找,用法與根據訪問時間查找類似,不重複舉例
- 以“分鐘”爲單位查找,可分別使用-amin、-mmin、-cmin選項進行按分鐘爲單位進行查找,具體方法與以“天”爲單位查找類似,不重複舉例
-
- 根據權限查找:
# 預設實驗環境:在/tmp/test目錄下有如下文件 [root@localhost test]# ls -l total 0 -rw-r-----. 1 root root 0 Jun 24 17:10 a -rw-rw-rw-. 1 root root 0 Jun 24 17:10 b -r--r-----. 1 root root 0 Jun 24 17:10 c -rwxrwxr-x. 1 root root 0 Jun 24 17:10 d -rwxrwxrwx. 1 root root 0 Jun 24 17:10 e -rw-r--r--. 1 root root 0 Jun 24 17:10 f -rw-r--r--. 1 root root 0 Jun 24 17:10 g [root@localhost test]#
- -perm MODE(精確匹配)
# 示例 1:精確匹配,查找8進制形式權限爲777的所有文件 [root@localhost test]# find . -perm 777 ./e [root@localhost test]#
# 示例 2:精確匹配,查找權限爲rw-r--r--的所有文件 [root@localhost test]# find . -perm u=rw,g=r,o=r ./f ./g [root@localhost test]#
- -perm /MODE:
# 倒 1:任何一類用戶擁有讀或寫權限,即符合要求 [root@localhost test]# find . -perm /666 . ./a ./b ./c ./d ./e ./f ./g [root@localhost test]#
# 例 2:任何一類用戶擁有寫權限即符合要求 [root@localhost test]# find . -perm /222 . ./a ./b ./d ./e ./f ./g [root@localhost test]#
# 例 3:任何一類用戶擁有執行權限即符合要求 [root@localhost test]# find .-perm /111 . ./d ./e [root@localhost test]#
# 例 4:u或g有一個擁有寫權限即符合要求 [root@localhost test]# find . -perm /220 . ./a ./b ./d ./e ./f ./g [root@localhost test]#
# 例 5:g或o中有一個擁有寫權限即符合要求 [root@localhost test]# find . -perm /022 ./b ./d ./e [root@localhost test]#
# 例 6:u和g分別必須擁有寫或執行權限才符合要求 [root@localhost test]# find . -perm /33 . ./b ./d ./e [root@localhost test]#
# 例 7:u和g都必須擁有寫權限才符合要求 [root@localhost test]# find . -perm /22 ./b ./d ./e [root@localhost test]#
- -perm -MODE:
# 示例 1:查找u具有rw權限,其它權限任意的所有文件 [root@localhost test]# find . -perm -u=rw . ./a ./b ./d ./e ./f ./g [root@localhost test]#
# 示例 2:查找g、o同時擁有寫權限,其它權限不限的所有文件 [root@localhost test]# find . -perm -g=w,o=w ./b ./e [root@localhost test]#
# 示例 3:以8進制權限表示形式,查找g、o同時擁有寫權限,其它權限不限的所有文件 [root@localhost test]# find . -perm -022 ./b ./e [root@localhost test]#
- -perm MODE(精確匹配)
- 條件組合:
# 預設實驗環境:在/tmp/test目錄下有如下文件 [root@localhost test]# ls -l total 0 -rw-r-----. 1 root root 0 Jul 15 09:33 a -rw-rw-rw-. 1 root root 0 Jul 15 09:33 b -r--r-----. 1 root root 0 Jul 15 09:33 c -rwxrwxr-x. 1 hadoop hadoop 0 Jul 15 09:33 d -rwxrwxrwx. 1 hadoop hadoop 0 Jul 15 09:33 e -rw-r--r--. 1 root root 0 Jul 15 09:33 f -rw-r--r--. 1 root root 0 Jul 15 09:33 g [root@localhost test]#
# 示例 1:與,查找g或o具有寫權限,且屬主爲root的所有文件 [root@localhost test]# find . -perm /g=w,o=w -a -user root ./b [root@localhost test]#
# 示例 2:或,查找所有o具有寫權限,或屬主爲root的文件 [root@localhost test]# find . -perm /o=w -o -user root . ./a ./b ./c ./e ./f ./g [root@localhost test]#
# 示例 3:非,查找所有屬主不爲root的文件 [root@localhost test]# find . -not -user root ./d ./e [root@localhost test]#
# 示例 4:非,查找所有屬主不爲root的文件 [root@localhost test]# find . ! -user root ./d ./e [root@localhost test]#
# 示例 4:分組組合,查找所有屬主爲root且o具有寫權限,或屬主不爲root的文件 [root@localhost test]# find . \( -user root -a -perm /002 \) -o -not -user root ./b ./d ./e [root@localhost test]#
- 根據文件名查找:
- 處理動作
# 示例 1:查找文件,並顯示輸出各文件的詳細信息 [root@localhost test]# find . -perm /011 -ls 50332721 0 drwxr-xr-x 2 root root 69 Jul 15 09:33 . 50332725 0 -rwxrwxr-x 1 hadoop hadoop 0 Jul 15 09:33 ./d 50332726 0 -rwxrwxrwx 1 hadoop hadoop 0 Jul 15 09:33 ./e [root@localhost test]#
# 示例 2:查找文件,查找條件中有“-o”,則“-ls”選項只會顯示最後一個“-o”後面的條件匹配到的項 [root@localhost test]# ls -l total 0 -rw-r-----. 1 root root 0 Jul 15 09:33 a -rw-rw-rw-. 1 root root 0 Jul 15 09:33 b -r--r-----. 1 root root 0 Jul 15 09:33 c -rwxrwxr-x. 1 hadoop hadoop 0 Jul 15 09:33 d -rwxrwxrwx. 1 hadoop hadoop 0 Jul 15 09:33 e -rw-r--r--. 1 root root 0 Jul 15 09:33 f -rw-r--r--. 1 root root 0 Jul 15 09:33 g [root@localhost test]# [root@localhost test]# find . -type f -perm /011 -o -perm /022 -ls 50332723 0 -rw-rw-rw- 1 root root 0 Jul 15 09:33 ./b [root@localhost test]# # 如需顯示所有所有查找到的項,則需使用“\(\)”包圍所有的查找條件 [root@localhost test]# find . \( -perm /011 -o -perm /022 \) -ls 50332721 0 drwxr-xr-x 2 root root 69 Jul 15 09:33 . 50332723 0 -rw-rw-rw- 1 root root 0 Jul 15 09:33 ./b 50332725 0 -rwxrwxr-x 1 hadoop hadoop 0 Jul 15 09:33 ./d 50332726 0 -rwxrwxrwx 1 hadoop hadoop 0 Jul 15 09:33 ./e [root@localhost test]#
# 示例 3:查找當前目錄及其子目錄下所有“*.log”文件,並刪除所有找到的文件 [root@localhost test]# find . -type f -name "*.log" -delete [root@localhost test]#
# 示例 4:查找當前目錄及其子目錄下所有屬主不爲root的文件,並將找到的文件的長格式信息寫入infos.txt文件 [root@localhost test]# ls -l total 0 -rw-r-----. 1 root root 0 Jul 15 09:33 a -rw-rw-rw-. 1 root root 0 Jul 15 09:33 b -r--r-----. 1 root root 0 Jul 15 09:33 c -rwxrwxr-x. 1 hadoop hadoop 0 Jul 15 09:33 d -rwxrwxrwx. 1 hadoop hadoop 0 Jul 15 09:33 e -rw-r--r--. 1 root root 0 Jul 15 09:33 f -rw-r--r--. 1 root root 0 Jul 15 09:33 g [root@localhost test]# [root@localhost test]# find . -not -user root -fls infos.txt [root@localhost test]# [root@localhost test]# cat infos.txt 50332725 0 -rwxrwxr-x 1 hadoop hadoop 0 Jul 15 09:33 ./d 50332726 0 -rwxrwxrwx 1 hadoop hadoop 0 Jul 15 09:33 ./e [root@localhost test]#
# 示例 5:查找當前目錄及其子目錄下所有屬主不爲root的文件,並將找到的文件刪除,刪除每一個文件之前都要求用戶確認 [root@localhost test]# ls -l total 0 -rw-r-----. 1 root root 0 Jul 15 09:33 a -rw-rw-rw-. 1 root root 0 Jul 15 09:33 b -r--r-----. 1 root root 0 Jul 15 09:33 c -rwxrwxr-x. 1 hadoop hadoop 0 Jul 15 09:33 d -rwxrwxrwx. 1 hadoop hadoop 0 Jul 15 09:33 e -rw-r--r--. 1 root root 0 Jul 15 09:33 f -rw-r--r--. 1 root root 0 Jul 15 09:33 g [root@localhost test]# [root@localhost test]# find . -not -user root -ok rm -f {} \; < rm ... ./d > ? y < rm ... ./e > ? y [root@localhost test]# [root@localhost test]# ls -l total 0 -rw-r-----. 1 root root 0 Jul 15 09:33 a -rw-rw-rw-. 1 root root 0 Jul 15 09:33 b -r--r-----. 1 root root 0 Jul 15 09:33 c -rw-r--r--. 1 root root 0 Jul 15 09:33 f -rw-r--r--. 1 root root 0 Jul 15 09:33 g [root@localhost test]#
# 示例 6:查找當前目錄及其子目錄下所有屬主不爲root的文件,並將找到的文件刪除,刪除文件之前不要求用戶確認 [root@localhost test]# ls -l total 0 -rw-r-----. 1 root root 0 Jul 15 09:33 a -rw-rw-rw-. 1 root root 0 Jul 15 09:33 b -r--r-----. 1 root root 0 Jul 15 09:33 c -rwxrwxr-x. 1 hadoop hadoop 0 Jul 15 09:33 d -rwxrwxrwx. 1 hadoop hadoop 0 Jul 15 09:33 e -rw-r--r--. 1 root root 0 Jul 15 09:33 f -rw-r--r--. 1 root root 0 Jul 15 09:33 g [root@localhost test]# [root@localhost test]# find . -not -user root -exec rm -f {} \; [root@localhost test]# [root@localhost test]# ls -l total 0 -rw-r-----. 1 root root 0 Jul 15 09:33 a -rw-rw-rw-. 1 root root 0 Jul 15 09:33 b -r--r-----. 1 root root 0 Jul 15 09:33 c -rw-r--r--. 1 root root 0 Jul 15 09:33 f -rw-r--r--. 1 root root 0 Jul 15 09:33 g [root@localhost test]#
# 示例 7:將find結果通過xargs命令傳遞給不支持通過管道傳道參數的下級命令(如:ls命令),爲防止文件全路徑中存在空白字符或反斜線從而影響程序正常工作,使用-print0選項使find命令輸出結果中使用空字符(null、\0)做爲分界符 [root@localhost test]# find /etc -name "*.repo" -print0 | xargs -0 ls -l -rw-r--r--. 1 root root 1584 Jul 9 16:58 /etc/yum.repos.d/CentOS-163.repo -rw-r--r--. 1 root root 566 Jun 11 15:06 /etc/yum.repos.d/CentOS-Media.repo [root@localhost test]#
-
- 答:find命令可在指定路徑下以文件名稱、從屬關係、文件類型、文件大小、時間戳、權限等信息做爲條件查找文件或目錄,其命令格式爲: