Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress output to StdOut when piping echo

Tags:

bash

unix

echo

pipe

I'm making a bash script that crawls through a directory and outputs all files of a certain type into a text file. I've got that working, it just also writes out a bunch of output to console I don't want (the names of the files)

Here's the relevant code so far, tmpFile is the file I'm writing to:

for DIR in `find . -type d` # Find problem directories
do
        for FILE in `ls "$DIR"` # Loop through problems in directory
        do
                if [[ `echo ${FILE} | grep -e prob[0-9]*_` ]]; then
                        `echo ${FILE} >> ${tmpFile}`
                fi
        done
done

The files I'm putting into the text file are in the format described by the regex prob[0-9]*_ (something like prob12345_01)

Where I pipe the output from echo ${FILE} into grep, it still outputs to stdout, something I want to avoid. I think it's a simple fix, but it's escaping me.

like image 269
Wuzseen Avatar asked Jan 19 '26 21:01

Wuzseen


1 Answers

All this can be done in one single find command. Consider this:

find . -type f -name "prob[0-9]*_*" -exec echo {} >> ${tmpFile} \;

EDIT:

Even simpler: (Thanks to @GlennJackman)

find . -type f -name "prob[0-9]*_*" >> $tmpFile
like image 148
anubhava Avatar answered Jan 21 '26 13:01

anubhava