0

This is a recurrent issue I have always had while dealing with iterations or actions over files found using find.

I have the following files:

 $ find . -name "*ES 03-11*" 
./01jan/ES 03-11.txt
./02feb/ES 03-11.txt
./03mar/ES 03-11.txt
./04apr/ES 03-11.txt
./05may/ES 03-11.txt

And I would like to launch the following command:

$ cat "./01jan/ES 03-11.txt" "./02feb/ES 03-11.txt" "./03mar/ES 03-11.txt" "./04apr/ES 03-11.txt" "./05may/ES 03-11.txt" | process

Which means concatenating each line provided by find but enclosed into double quotes or simple quotes I guess.

I have tried this:

find . -name "*ES 03-11*" | awk '{printf "\"%s\" ", $0}' | xargs cat | process

Which seems to work, but I am wondering if there is any other way to do it without using awk or doing something easy to remember or type.

I am using FreeBSD and Bourne Shell.

4
1

To summarize you can use the -exec method:

find . -name "*ES 03-11*" -exec cat {} +

or an xargs approach:

find . -name "*ES 03-11*" | xargs -I xx cat xx
1

This is exactly what the NULL-termination features are for. Unfortunately although find -print0 exists, the xargs command on FreeBSD seems not to have the matching -0. This precludes xargs as part of any solution.

Another solution is to iterate across your pattern

for file in */*'ES 03-11'*; do cat "$file"; done | process

Or, for many files,

for dir in *
do
    [ -d "$dir" ] || continue
    for file in "$dir"/*'ES 03-11'*
    do
        [ -f "$file" ] && cat "$file"
    done
done | process

Or even directly

cat */*'ES 03-11'* | process

Not the answer you're looking for? Browse other questions tagged or ask your own question.