過濾目錄中所有的1級目錄的多種方法(不包含目錄本身 隱藏目錄 只能是1級目錄)
第二個測驗練習題1
1.如何過濾出已知當前目錄下oldboy中的所有一級目錄(提示:不包含oldboy目錄下面目錄的子目錄及隱藏目錄,即只能是一級目錄)?
模擬環境
cd ~ mkdir oldboy -p cd oldboy/ mkdir ext/oldboy test xiaodong xiaofan xingfujie -p touch jeacen oldboy wodi.gz yingsui.gz LANG=en_US.UTF-8 ########################################################################## [root@shhaioldboy02-LNB oldboy 14:49]$ cd ~ [root@shhaioldboy02-LNB ~ 14:49]$ mkdir oldboy -p [root@shhaioldboy02-LNB ~ 14:49]$ cd oldboy/ [root@shhaioldboy02-LNB oldboy 14:49]$ mkdir ext/oldboy test xiaodong xiaofan xingfujie -p [root@shhaioldboy02-LNB oldboy 14:49]$ touch jeacen oldboy wodi.gz yingsui.gz [root@shhaioldboy02-LNB oldboy 14:49]$.UTF-8 [root@shhaioldboy02-LNB oldboy 14:49]$ ls ext jeacen oldboy test wodi.gz xiaodong xiaofan xingfujie yingsui.gz [root@shhaioldboy02-LNB oldboy 14:49]$ pwd /root/oldboy [root@shhaioldboy02-LNB oldboy 14:49]$
1.5.1、tree -dL 1過濾目錄中所有的1級目錄
當tree不加參數時
ps:如果一個命令顯示的內容有你想要的結果,這個命令參數中,就有相應參數只顯示你想要的結果
[root@shhaioldboy02-LNB ~ 14:56]$ tree /root/oldboy /root/oldboy ├── ext │ └── oldboy ├── jeacen ├── oldboy ├── test ├── wodi.gz ├── xiaodong ├── xiaofan ├── xingfujie └── yingsui.gz 6 directories, 4 files [root@shhaioldboy02-LNB ~ 14:56]$
使用 tree -dL 1 顯示目錄下所有的1級目錄
tree -dL 1 參數說明:
-L 顯示目錄下的級數,1表示顯示中的1級的文件和目錄
-d 只顯示目錄
[root@shhaioldboy02-LNB oldboy 15:03]$ tree -dL 1 /root/oldboy/ /root/oldboy/ ├── ext ├── test ├── xiaodong ├── xiaofan └── xingfujie 5 directories [root@shhaioldboy02-LNB oldboy 15:03]$
1.5.2、find -maxdepth 1 -type d查找目錄中所有的 1級目錄
參數說明:
find 目錄名 -maxdepth 1 -type d #查找目錄中所有的1級目錄
-type d 只查找目錄
-maxdepth num 指定找目錄下面信息內容目錄深度為(num)
01、find /root/oldboy/ -type d -maxdepth 1 進行查找發現報錯信息:
報錯信息說明:建議你最好把-maxdepth參數信息放在所有參數最前面
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
[root@shhaioldboy02-LNB oldboy 15:18]$ find /root/oldboy/ -type d -maxdepth 1
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
#說明:建議你最好把-maxdepth參數信息放在所有參數最前面
/root/oldboy/ /root/oldboy/test /root/oldboy/xingfujie /root/oldboy/xiaofan /root/oldboy/xiaodong /root/oldboy/ext [root@shhaioldboy02-LNB oldboy 15:18]$
02、不出現警告提示的方法:將-maxdepth 1放在前面
find /root/oldboy/ -maxdepth 1 -type d
[root@shhaioldboy02-LNB oldboy 15:18]$ find /root/oldboy/ -maxdepth 1 -type d /root/oldboy/ /root/oldboy/test /root/oldboy/xingfujie /root/oldboy/xiaofan /root/oldboy/xiaodong /root/oldboy/ext [root@shhaioldboy02-LNB oldboy 15:24]$
03、find查找1級目錄,不顯示目錄本身:
find ./ -maxdepth 1 -type d ! -name "."
! -name 排除,不顯示指定的名字 !取反
[root@shhaioldboy02-LNB oldboy 15:59]$ find ./ -maxdepth 1 -type d ./ #使用 ! -name 可以將目錄本身排除在外,使其不顯示目錄本身 ./test ./xingfujie ./xiaofan ./xiaodong ./ext [root@shhaioldboy02-LNB oldboy 15:59]$ find ./ -maxdepth 1 -type d ! -name "." ./test ./xingfujie ./xiaofan ./xiaodong ./ext [root@shhaioldboy02-LNB oldboy 16:00]$
1.5.3、ls -l 目錄名|過濾以d開頭的信息 來查找目錄中所有的1級目錄
01、ls -l 目錄名|grep "^d" 查找目錄中的所有1級目錄
[root@shhaioldboy02-LNB oldboy 16:21]$ ls -l /root/oldboy/ |grep "^d" drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext drwxr-xr-x 2 root root 4096 Jul 17 16:41 test drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie [root@shhaioldboy02-LNB oldboy 16:21]$
02、ls -l 目錄名|sed -n '/^d/p' 查找目錄中的所有1級目錄
[root@shhaioldboy02-LNB oldboy 16:31]$ ls -l /root/oldboy/ |sed -n '/^d/p' drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext drwxr-xr-x 2 root root 4096 Jul 17 16:41 test drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie [root@shhaioldboy02-LNB oldboy 16:31]$
03、ls -l 目錄名| awk '/^d/' 查找目錄中的所有1級目錄
[root@shhaioldboy02-LNB oldboy 16:31]$ ls -l /root/oldboy/ |awk '/^d/' drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext drwxr-xr-x 2 root root 4096 Jul 17 16:41 test drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie [root@shhaioldboy02-LNB oldboy 16:37]$
1.5.4、ls -lF 目錄名|過濾以/結尾的信息 來查找目錄中所有的1級目錄
ls -lF -F給目錄加上標記 以/結尾
[root@shhaioldboy02-LNB oldboy 16:40]$ ls -lF /root/oldboy/ total 20 drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext/ -rw-r--r-- 1 root root 0 Jul 18 14:49 jeacen -rw-r--r-- 1 root root 0 Jul 18 14:49 oldboy drwxr-xr-x 2 root root 4096 Jul 17 16:41 test/ -rw-r--r-- 1 root root 0 Jul 18 14:49 wodi.gz drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie/ -rw-r--r-- 1 root root 0 Jul 18 14:49 yingsui.gz [root@shhaioldboy02-LNB oldboy 16:42]$
01、ls -lF 目錄名|grep "/$" 查找目錄中的所有1級目錄
ls -lF 目錄名|grep "/$" #查找以 / 結尾的
[root@shhaioldboy02-LNB oldboy 16:47]$ ls -lF /root/oldboy/ |grep "/$" drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 test/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie/ [root@shhaioldboy02-LNB oldboy 16:47]$
02、ls -lF 目錄名|sed -n "/\/$/p" 查找目錄中的所有1級目錄
[root@shhaioldboy02-LNB oldboy 16:49]$ ls -lF /root/oldboy/ |sed -n "/\/$/p" drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 test/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie/ [root@shhaioldboy02-LNB oldboy 16:51]$
03、ls -lF 目錄名|awk "/\/$/" 查找目錄中的所有1級目錄
[root@shhaioldboy02-LNB oldboy 16:53]$ ls -lF /root/oldboy/ |awk "/\/$/" drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 test/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie/ [root@shhaioldboy02-LNB oldboy 16:54]$
1.5.5、通過硬鏈接信息查找
通過硬鏈接數值發現 目錄硬鏈接數值都大於1
[root@shhaioldboy02-LNB oldboy 16:56]$ ll /root/oldboy/ total 20 drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext -rw-r--r-- 1 root root 0 Jul 18 14:49 jeacen -rw-r--r-- 1 root root 0 Jul 18 14:49 oldboy drwxr-xr-x 2 root root 4096 Jul 17 16:41 test -rw-r--r-- 1 root root 0 Jul 18 14:49 wodi.gz drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie -rw-r--r-- 1 root root 0 Jul 18 14:49 yingsui.gz [root@shhaioldboy02-LNB oldboy 16:59]$
01、ls -l 目錄名|awk '$2!=1' 截取第二列硬鏈接信息不等於1的 來查找目錄中的所有1級目錄
[root@shhaioldboy02-LNB oldboy 17:05]$ ls -l /root/oldboy/ |awk '$2!=1' total 20 drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext drwxr-xr-x 2 root root 4096 Jul 17 16:41 test drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie [root@shhaioldboy02-LNB oldboy 17:05]$
02、ls -l 目錄名|awk '$2>1' 截取第二列硬鏈接信息大於1的 來查找目錄中的所有1級目錄
[root@shhaioldboy02-LNB oldboy 17:05]$ ls -l /root/oldboy/ |awk '$2>1' total 20 drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext drwxr-xr-x 2 root root 4096 Jul 17 16:41 test drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie [root@shhaioldboy02-LNB oldboy 17:09]$
1.5.6、cd 目錄名 ; ls -ld */ 來查找目錄中的所有1級目錄
[root@shhaioldboy02-LNB oldboy 17:16]$ cd /root/oldboy/ ; ls -ld */ drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 test/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie/ [root@shhaioldboy02-LNB oldboy 17:17]$
1.5.7、ls -p 給目錄加上標記 /
[root@shhaioldboy02-LNB oldboy 17:24]$ ls -lp total 20 drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext/ -rw-r--r-- 1 root root 0 Jul 18 14:49 jeacen -rw-r--r-- 1 root root 0 Jul 18 14:49 oldboy drwxr-xr-x 2 root root 4096 Jul 17 16:41 test/ -rw-r--r-- 1 root root 0 Jul 18 14:49 wodi.gz drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie/ -rw-r--r-- 1 root root 0 Jul 18 14:49 yingsui.gz [root@shhaioldboy02-LNB oldboy 17:24]$
ls -p 與 ls -F 類似
[root@shhaioldboy02-LNB oldboy 17:24]$ ls -lp total 20 drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext/ -rw-r--r-- 1 root root 0 Jul 18 14:49 jeacen -rw-r--r-- 1 root root 0 Jul 18 14:49 oldboy drwxr-xr-x 2 root root 4096 Jul 17 16:41 test/ -rw-r--r-- 1 root root 0 Jul 18 14:49 wodi.gz drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie/ -rw-r--r-- 1 root root 0 Jul 18 14:49 yingsui.gz #################################################### [root@shhaioldboy02-LNB oldboy 17:24]$ ls -lF total 20 drwxr-xr-x 3 root root 4096 Jul 17 16:41 ext/ -rw-r--r-- 1 root root 0 Jul 18 14:49 jeacen -rw-r--r-- 1 root root 0 Jul 18 14:49 oldboy drwxr-xr-x 2 root root 4096 Jul 17 16:41 test/ -rw-r--r-- 1 root root 0 Jul 18 14:49 wodi.gz drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaodong/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xiaofan/ drwxr-xr-x 2 root root 4096 Jul 17 16:41 xingfujie/ -rw-r--r-- 1 root root 0 Jul 18 14:49 yingsui.gz [root@shhaioldboy02-LNB oldboy 17:28]$
過濾目錄中所有的1級目錄的多種方法(不包含目錄本身 隱藏目錄 只能是1級目錄)