如何过滤出已知当前目录下/llj中的所有一级目录?
测试环境,/llj下有许多的目录、文件等:
[root@test llj]# ll
total 0
drwxr-xr-x 2 root root 17 Dec 3 10:04 ext
-rw-r--r-- 1 root root 0 Dec 3 10:04 jeacen
-rw-r--r-- 1 root root 0 Dec 3 10:03 llj
drwxr-xr-x 2 root root 6 Dec 3 10:02 test
-rw-r--r-- 1 root root 0 Dec 3 10:02 wodi.gz
drwxr-xr-x 2 root root 6 Dec 3 10:04 xiaodong
drwxr-xr-x 2 root root 6 Dec 3 09:58 xiaofan
drwxr-xr-x 2 root root 6 Dec 3 10:04 xingfujie
-rw-r--r-- 1 root root 0 Dec 3 10:02 yingsui.gz
[root@test llj]#
目标:过滤出已知当前目录下/llj中的所有一级目录
解决方案:
一、观察到 ll /llj 中目录的信息都是以 d 开头,使用 grep,和正则筛选出来。
[root@test llj]# ls -l |grep "^d"
drwxr-xr-x 2 root root 17 Dec 3 10:04 ext
drwxr-xr-x 2 root root 6 Dec 3 10:02 test
drwxr-xr-x 2 root root 6 Dec 3 10:04 xiaodong
drwxr-xr-x 2 root root 6 Dec 3 09:58 xiaofan
drwxr-xr-x 2 root root 6 Dec 3 10:04 xingfujie
二、观察到目录的第二列连接数都是 >1(=2),使用 awk 删选:
[root@test llj]# ls -l |awk '$2>1'
drwxr-xr-x 2 root root 17 Dec 3 10:04 ext
drwxr-xr-x 2 root root 6 Dec 3 10:02 test
drwxr-xr-x 2 root root 6 Dec 3 10:04 xiaodong
drwxr-xr-x 2 root root 6 Dec 3 09:58 xiaofan
drwxr-xr-x 2 root root 6 Dec 3 10:04 xingfujie
三、给目录一定的特征,想到 ls 的 -p 和-F参数,让目录后跟/,那么用 grep 删选出 / 结尾的:
[root@test llj]# ls -lp|grep '/$'
drwxr-xr-x 2 root root 17 Dec 3 10:04 ext/
drwxr-xr-x 2 root root 6 Dec 3 10:02 test/
drwxr-xr-x 2 root root 6 Dec 3 10:04 xiaodong/
drwxr-xr-x 2 root root 6 Dec 3 09:58 xiaofan/
drwxr-xr-x 2 root root 6 Dec 3 10:04 xingfujie/
[root@test llj]# ls -lF|grep '/$'
drwxr-xr-x 2 root root 17 Dec 3 10:04 ext/
drwxr-xr-x 2 root root 6 Dec 3 10:02 test/
drwxr-xr-x 2 root root 6 Dec 3 10:04 xiaodong/
drwxr-xr-x 2 root root 6 Dec 3 09:58 xiaofan/
drwxr-xr-x 2 root root 6 Dec 3 10:04 xingfujie/
四、既然 ls 可以,那么 tree 当然可以,使用tree的 -F 参数,然后删选出 / :
[root@test llj]# tree -L 1 -F |grep '/'
|-- ext/
|-- test/
|-- xiaodong/
|-- xiaofan/
|-- xingfujie/
五、直接使用 tree 的 -d 参数,只显示目录:
[root@test llj]# tree -Ld 1 | grep -v '\.'
|-- ext
|-- test
|-- xiaodong
|-- xiaofan
`-- xingfujie5 directories
六、是时候使用 find 了,-maxdepth参数可以指定查找的目录级数为1,-type可以指定查找类型为目录,! 和 -name参数可以去除查找到的当前目录 . ,实现如下:
[root@test llj]# find -maxdepth 1 -type d ! -name "."
./ext
./xiaofan
./test
./xiaodong
./xingfujie
七、使用sed和awk过滤ls结果
[root@test llj]# ls -l | sed -n '/^d/p'
drwxr-xr-x 2 root root 17 Dec 3 10:04 ext
drwxr-xr-x 2 root root 6 Dec 3 10:02 test
drwxr-xr-x 2 root root 6 Dec 3 10:04 xiaodong
drwxr-xr-x 2 root root 6 Dec 3 09:58 xiaofan
drwxr-xr-x 2 root root 6 Dec 3 10:04 xingfujie
[root@test llj]# ls -l | awk '/^d/'
drwxr-xr-x 2 root root 17 Dec 3 10:04 ext
drwxr-xr-x 2 root root 6 Dec 3 10:02 test
drwxr-xr-x 2 root root 6 Dec 3 10:04 xiaodong
drwxr-xr-x 2 root root 6 Dec 3 09:58 xiaofan
drwxr-xr-x 2 root root 6 Dec 3 10:04 xingfujie
[root@test llj]# ls -F | sed -n '/\/$/p'
ext/
test/
xiaodong/
xiaofan/
xingfujie/
[root@test llj]# ls -F | awk '/\/$/'
ext/
test/
xiaodong/
xiaofan/
xingfujie/
八、另类查询办法
[root@test llj]# ls -dl */
drwxr-xr-x 2 root root 17 Dec 3 10:04 ext/
drwxr-xr-x 2 root root 6 Dec 3 10:02 test/
drwxr-xr-x 2 root root 6 Dec 3 10:04 xiaodong/
drwxr-xr-x 2 root root 6 Dec 3 09:58 xiaofan/
drwxr-xr-x 2 root root 6 Dec 3 10:04 xingfujie/
源自:晓明酱