过滤目录中所有的1级目录的多种方法(不包含目录本身 隐藏目录 只能是1级目录)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了过滤目录中所有的1级目录的多种方法(不包含目录本身 隐藏目录 只能是1级目录)相关的知识,希望对你有一定的参考价值。
1.5、过滤目录中所有的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 ########################################################################## [[email protected] oldboy 14:49]$ cd ~ [[email protected] ~ 14:49]$ mkdir oldboy -p [[email protected] ~ 14:49]$ cd oldboy/ [[email protected] oldboy 14:49]$ mkdir ext/oldboy test xiaodong xiaofan xingfujie -p [[email protected] oldboy 14:49]$ touch jeacen oldboy wodi.gz yingsui.gz [[email protected] oldboy 14:49]$.UTF-8 [[email protected] oldboy 14:49]$ ls ext jeacen oldboy test wodi.gz xiaodong xiaofan xingfujie yingsui.gz [[email protected] oldboy 14:49]$ pwd /root/oldboy [[email protected] oldboy 14:49]$
1.5.1、tree -dL 1过滤目录中所有的1级目录
当tree不加参数时
ps:如果一个命令显示的内容有你想要的结果,这个命令参数中,就有相应参数只显示你想要的结果
[[email protected] ~ 14:56]$ tree /root/oldboy /root/oldboy ├── ext │ └── oldboy ├── jeacen ├── oldboy ├── test ├── wodi.gz ├── xiaodong ├── xiaofan ├── xingfujie └── yingsui.gz 6 directories, 4 files [[email protected] ~ 14:56]$
使用 tree -dL 1 显示目录下所有的1级目录
tree -dL 1 参数说明:
-L 显示目录下的级数,1表示显示中的1级的文件和目录
-d 只显示目录
[[email protected] oldboy 15:03]$ tree -dL 1 /root/oldboy/ /root/oldboy/ ├── ext ├── test ├── xiaodong ├── xiaofan └── xingfujie 5 directories [[email protected] 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.
[[email protected] 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 [[email protected] oldboy 15:18]$
02、不出现警告提示的方法:将-maxdepth 1放在前面
find /root/oldboy/ -maxdepth 1 -type d
[[email protected] 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 [[email protected] oldboy 15:24]$
03、find查找1级目录,不显示目录本身:
find ./ -maxdepth 1 -type d ! -name "."
! -name 排除,不显示指定的名字 !取反
[[email protected] oldboy 15:59]$ find ./ -maxdepth 1 -type d ./ #使用 ! -name 可以将目录本身排除在外,使其不显示目录本身 ./test ./xingfujie ./xiaofan ./xiaodong ./ext [[email protected] oldboy 15:59]$ find ./ -maxdepth 1 -type d ! -name "." ./test ./xingfujie ./xiaofan ./xiaodong ./ext [[email protected] oldboy 16:00]$
1.5.3、ls -l 目录名|过滤以d开头的信息 来查找目录中所有的1级目录
01、ls -l 目录名|grep "^d" 查找目录中的所有1级目录
[[email protected] 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 [[email protected] oldboy 16:21]$
02、ls -l 目录名|sed -n '/^d/p' 查找目录中的所有1级目录
[[email protected] 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 [[email protected] oldboy 16:31]$
03、ls -l 目录名| awk '/^d/' 查找目录中的所有1级目录
[[email protected] 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 [[email protected] oldboy 16:37]$
1.5.4、ls -lF 目录名|过滤以/结尾的信息 来查找目录中所有的1级目录
ls -lF -F给目录加上标记 以/结尾
[[email protected] 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 [[email protected] oldboy 16:42]$
01、ls -lF 目录名|grep "/$" 查找目录中的所有1级目录
ls -lF 目录名|grep "/$" #查找以 / 结尾的
[[email protected] 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/ [[email protected] oldboy 16:47]$
02、ls -lF 目录名|sed -n "//$/p" 查找目录中的所有1级目录
[[email protected] 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/ [[email protected] oldboy 16:51]$
03、ls -lF 目录名|awk "//$/" 查找目录中的所有1级目录
[[email protected] 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/ [[email protected] oldboy 16:54]$
1.5.5、通过硬链接信息查找
通过硬链接数值发现 目录硬链接数值都大于1
[[email protected] 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 [[email protected] oldboy 16:59]$
01、ls -l 目录名|awk '$2!=1' 截取第二列硬链接信息不等于1的 来查找目录中的所有1级目录
[[email protected] 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 [[email protected] oldboy 17:05]$
02、ls -l 目录名|awk '$2>1' 截取第二列硬链接信息大于1的 来查找目录中的所有1级目录
[[email protected] 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 [[email protected] oldboy 17:09]$
1.5.6、cd 目录名 ; ls -ld */ 来查找目录中的所有1级目录
[[email protected] 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/ [[email protected] oldboy 17:17]$
1.5.7、ls -p 给目录加上标记 /
[[email protected] 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 [[email protected] oldboy 17:24]$
ls -p 与 ls -F 类似
[[email protected] 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 #################################################### [[email protected] 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 [[email protected] oldboy 17:28]$
以上是关于过滤目录中所有的1级目录的多种方法(不包含目录本身 隐藏目录 只能是1级目录)的主要内容,如果未能解决你的问题,请参考以下文章