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
。
分类:
linux shell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2021-12-30 R语言中判断两个数据框是否完全相同
2021-12-30 R语言中order排序出现2大于10的情况?
2021-12-30 R语言中NA的判断
2020-12-30 centos7 中 安装gcc编译器
2020-12-30 vmware虚拟机安装mint-20全过程
2020-12-30 vmware虚拟机安装deepin20全过程
2020-12-30 vmware虚拟机安装openSUSE-Leap-15.2全过程