linux 中实现仅对指定目录下的目录或者文件单独进行迭代

 

001、测试目录如下,分别包含目录、文件

[root@pc1 test]# ls                   ## 测试目录
dir1  dir2  dir3  dir4  file1  file2  file3  file4

 

002、仅对目录进行迭代

 a、

[root@pc1 test]# ls                                       ## 测试目录
dir1  dir2  dir3  dir4  file1  file2  file3  file4
[root@pc1 test]# ls -d                                    ## ls -d并不能列出所有的目录
.
[root@pc1 test]# ls -d */                                 ## 正确做法,添加*/, 列出所有的目录
dir1/  dir2/  dir3/  dir4/
[root@pc1 test]# for i in `ls -d */`; do echo $i; done    ## 利用for循环结构迭代
dir1/
dir2/
dir3/
dir4/

 

b、输出指定目录下所有目录的绝对路径

[root@pc1 test]# ls
dir1  dir2  dir3  dir4  file1  file2  file3  file4
[root@pc1 test]# pwd
/home/test
[root@pc1 test]# ls -d /home/test/*/        ## 输出指定目录下的所有的目录
/home/test/dir1/  /home/test/dir2/  /home/test/dir3/  /home/test/dir4/
[root@pc1 test]# ls -d /home/test/*/ | while read i; do echo $i; done   ## 对指定目录下的所有的目录进行迭代
/home/test/dir1/
/home/test/dir2/
/home/test/dir3/
/home/test/dir4/

 

c、

[root@pc1 test]# ls
dir1  dir2  dir3  dir4  file1  file2  file3  file4
[root@pc1 test]# ls -F
dir1/  dir2/  dir3/  dir4/  file1  file2  file3  file4
[root@pc1 test]# ls -F | grep "/$"            ## 借助ls -F 列出目录,增加grep过滤
dir1/
dir2/
dir3/
dir4/
[root@pc1 test]# for i in $(ls -F | grep "/$"); do echo $i; done       ## 对目录进行迭代
dir1/
dir2/
dir3/
dir4/

 

d、

[root@pc1 test]# ls
dir1  dir2  dir3  dir4  file1  file2  file3  file4
[root@pc1 test]# ls -l     ## 借助ls -l, 目录文件的开头为d,借助grep进行过滤,然后输出最后一列
total 0
drwxr-xr-x. 2 root root 6 Dec 30 10:39 dir1
drwxr-xr-x. 2 root root 6 Dec 30 10:39 dir2
drwxr-xr-x. 2 root root 6 Dec 30 10:39 dir3
drwxr-xr-x. 2 root root 6 Dec 30 10:39 dir4
-rw-r--r--. 1 root root 0 Dec 30 10:39 file1
-rw-r--r--. 1 root root 0 Dec 30 10:39 file2
-rw-r--r--. 1 root root 0 Dec 30 10:39 file3
-rw-r--r--. 1 root root 0 Dec 30 10:39 file4
[root@pc1 test]# ls -l | grep "^d" | awk '{print $NF}'    ## 仅输出最后的目录
dir1
dir2
dir3
dir4
[root@pc1 test]# ls -l | grep "^d" | awk '{print $NF}' | while read i; do echo $i; done   ## 迭代
dir1
dir2
dir3
dir4

 

e1、借助find命令实现

[root@pc1 test]# ls
dir1  dir2  dir3  dir4  file1  file2  file3  file4
[root@pc1 test]# find ./ -mindepth 1 -type d     ##  在当前目录中进行查找,最低深度为1,查找的类型为目录
./dir1
./dir2
./dir3
./dir4
[root@pc1 test]# find ./ -mindepth 1 -type d | awk -F "/" '{print $NF}'    ## 仅仅输出目录名称
dir1
dir2
dir3
dir4
[root@pc1 test]# find ./ -mindepth 1 -type d | awk -F "/" '{print $NF}' | while read i; do echo $i; done  ## 迭代
dir1
dir2
dir3
dir4

 

e2、

[root@pc1 test]# ls
dir1  dir2  dir3  dir4  file1  file2  file3  file4
[root@pc1 test]# find ./ -mindepth 1 -type d -exec basename {} \;      ## 列出文件名
dir1
dir2
dir3
dir4
[root@pc1 test]# find ./ -mindepth 1 -type d -exec basename {} \; | while read i; do echo $i; done  ## 迭代
dir1
dir2
dir3
dir4

 

 

 

003、仅对文件进行迭代

a、

[root@pc1 test]# ls                  ## 测试目录
a.txt  b.txt  dir1  dir2  dir3  dir4  file1  file2  file3  file4
[root@pc1 test]# ls -F | grep -vE "[@*/$]"    ## 借助ls -F; 然后利用grep过滤掉软链接,可执行文件,目录文件
file1
file2
file3
file4
[root@pc1 test]# ls -F | grep -vE "[@*/$]" | xargs -n 1 echo     ## 迭代
file1
file2
file3
file4

 

b、借助find命令实现

[root@pc1 test]# ls
a.txt  b.txt  dir1  dir2  dir3  dir4  file1  file2  file3  file4
[root@pc1 test]# find ./ -mindepth 1 -type f -not -perm -o=x   ## 查找深度最低为1;查找类型为文件;排除其他人有执行权限的文件
./file1
./file2
./file3
./file4
[root@pc1 test]# find ./ -mindepth 1 -type f -not -perm -o=x -exec basename {} \;   ## 仅仅输出文件名
file1
file2
file3
file4                                                                                ## 借助while进行迭代
[root@pc1 test]# find ./ -mindepth 1 -type f -not -perm -o=x -exec basename {} \; | while read i; do echo $i; done
file1
file2
file3
file4

 

 

posted @ 2023-12-30 10:39  小鲨鱼2018  阅读(3)  评论(0编辑  收藏  举报