[英]list only directory if contain a specific file name in bash
Hel lo everyone, I need help with bash commands.大家好,我需要有关 bash 命令的帮助。
Here is the deal :这是交易:
I have several directories :我有几个目录:
such as如
path1/A/path2/
file1
file2
path1/B/path2/
file1
path1/C/path2/
file1
file2
path1/D/path2/
file1
file2
and a /path_to_this_file/file.txt
:和一个/path_to_this_file/file.txt
:
A
B
C
D
that I use such as :我使用的例如:
cat /path_to_this_file/file.txt | while read line; do ls path1/$line/path2/
then I can list all content in the paths BUT I would like to do a ls only for the path2
that does not have a file2
into their directory..然后我可以列出路径中的所有内容,但我只想为没有file2
到他们的目录中的path2
做一个 ls ..
Here only the path1/B/path2/
should be listed这里只应列出path1/B/path2/
Does someone have a code for that ?有人有代码吗?
Added an if statement to your code:在您的代码中添加了一个 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
Alternatively:或者:
xargs -I {} bash -c "[ ! -f "path1/{}/path2/file2" ] && ls path1/{}/path2" < /path_to_this_file/file.txt
This will do the job这将完成工作
while read line; do
ls "path1/$line/path2/file2" &> /dev/null || ls "path1/$line/path2"
done < /path_to_this_file/file.txt
Using mapfile
aka readarray
bash4+
only and a for loop
仅使用mapfile
aka readarray
bash4+
和for loop
#!/usr/bin/env bash
mapfile -t var < path_to_this_file/file.txt
for i in "${var[@]}"; do
if [[ ! -e path1/$i/path2/file2 ]]; then
ls "path1/$i/path2/"
fi
done
The ls
in the above code上面代码中的ls
ls "path1/$i/path2/"
Output is输出是
file1
If the you want to print just the PATH change the ls "path1/$i/path2/"
to如果您只想打印 PATH,请将ls "path1/$i/path2/"
更改为
echo "path1/$i/path2/"
Output is输出是
path1/B/path2/
If you want to print both PATH and file use如果要同时打印 PATH 和文件,请使用
echo "path1/$i/path2/"*
Output is输出是
path1/B/path2/file1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.