bash - Print the directory where the 'find' linux command finds a match -


i have bunch of directories; of them contain '.todo' file.

/storage/bcc9f9d00663a8043f8d73369e920632/.todo /storage/bae9bbf30ccef5210534e875fc80d37e/.todo /storage/cbb46ff977ee166815a042f3deefb865/.todo /storage/8abcbf3194f5d7e97e83c4fd042ab8e7/.todo /storage/9db9411f403bd282b097cbf06a9687f5/.todo /storage/99a9ba69543cd48ba4bd59594169bbac/.todo /storage/0b6fb65d4e46cbd8a9b1e704cfacc42e/.todo 

i'd 'find' command print me directory, this

/storage/bcc9f9d00663a8043f8d73369e920632 /storage/bae9bbf30ccef5210534e875fc80d37e /storage/cbb46ff977ee166815a042f3deefb865 ... 

here's have far, lists '.todo' file well

#!/bin/bash storagefolder='/storage' find $storagefolder -name .todo  -exec ls -l {} \; 

should dumb stupid, i'm giving :(

to print directory name only, use -printf '%h\n'. recommended quote variable doublequotes.

find "$storagefolder" -name .todo -printf '%h\n' 

if want process output:

find "$storagefolder" -name .todo -printf '%h\n' | xargs ls -l 

or use loop process substitution make use of variable:

while read -r dir;     ls -l "$dir" done < <(exec find "$storagefolder" -name .todo -printf '%h\n') 

the loop process 1 directory @ time whereas in xargs directories passed ls -l in 1 shot.

to make sure process 1 directory @ time, add uniq:

find "$storagefolder" -name .todo -printf '%h\n' | uniq | xargs ls -l 

or

while read -r dir;     ls -l "$dir" done < <(exec find "$storagefolder" -name .todo -printf '%h\n' | uniq) 

if don't have bash , don't mind preserving changes variables outside loop can use pipe:

find "$storagefolder" -name .todo -printf '%h\n' | uniq | while read -r dir;     ls -l "$dir" done 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -