Skip to content
Advertisement

list only directory if contain a specific file name in bash

Hel lo everyone, I need help with bash commands.

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 :

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..

Here only the path1/B/path2/ should be listed

Does someone have a code for that ?

Advertisement

Answer

Added an if statement to your code:

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
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement