简体   繁体   English

如果在 bash 中包含特定文件名,则仅列出目录

[英]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.

相关问题 我需要使用 bash 创建不包含具有特定名称的文件的文件夹目录列表 - I need to create a list of folder directories that do not contain a file with a specific name using bash 仅当包含它的行不包含使用 Bash 的特定字符串时,如何替换文件中的字符串? - How to replace a string in a file only if the line containing it does not also contain a specific string using Bash? bash:检查目录中的多个文件是否包含列表中的字符串 - bash: check if multiple files in a directory contain strings from a list 如何使用 linux bash 将特定文件夹下所有子文件夹的名称仅列出到变量中 - How to list only name of all subfolders under specific folder into a variable using linux bash 如何仅在 Bash shell 的当前目录中仅打印包含字符串的文件? - How do I print only files that contain the string only in my current directory in Bash shell? 如何使BASH脚本仅在特定目录中工作? - How to make a BASH script work only in a specific directory? 目录名称Bash中的空格 - Spaces in directory name Bash 在bash中增加目录名称 - increment directory name in bash 循环遍历 bash 中的文件并过滤以目录名称开头 - Looping though a file in bash and filtering for a directory name beginning Bash - 列出所有子目录中的程序,目录名在文件之前 - Bash - listing programs in all subdirectories with directory name before file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM