linux - 如果在 bash 中包含特定文件名,则仅列出目录
问题描述
大家好,我需要 bash 命令方面的帮助。
这是交易:
我有几个目录:
如
path1/A/path2/
file1
file2
path1/B/path2/
file1
path1/C/path2/
file1
file2
path1/D/path2/
file1
file2
和一个/path_to_this_file/file.txt
:
A
B
C
D
我使用的是:
cat /path_to_this_file/file.txt | while read line; do ls path1/$line/path2/
然后我可以列出路径中的所有内容,但我只想为path2
那些没有file2
进入他们的目录的 ls 做一个 ls 。
此处仅path1/B/path2/
应列出
有人有代码吗?
解决方案
在您的代码中添加了 if 语句:
cat /path_to_this_file/file.txt |
while read line
do
if [ ! -f "path1/$line/path2/file2" ]; then
ls path1/$line/path2/
fi
done
或者:
xargs -I {} bash -c "[ ! -f "path1/{}/path2/file2" ] && ls path1/{}/path2" < /path_to_this_file/file.txt
推荐阅读
- python - 在特殊列下添加该列的总和并通过python写入文件
- javascript - 传入一个完整的对象 ala 对象解构良好实践/是否具有性能影响?
- php - “无效的识别‘配置’:错误的语言代码。” 什么时候不应该
- html - 使用 Beautifulsoup 从模棱两可的标签中抓取
- java - 如何在 Struts2 Web 应用程序中处理套接字数据?
- javascript - 如何在 HTML 中使用 JavaScript 读取本地二进制文件
- r - R:计算每个模型的行数,不包括某些变量(如果存在)
- active-directory - 寻找有关设置 Auth0 - Active Directory 联合的指导
- excel - 自动筛选代码的参数不是可选错误
- r - 在同一图上重新排序 3 个 geom_smooth 的图例标签