Differences between revisions 2 and 3
Revision 2 as of 2007-05-28 22:24:31
Size: 983
Editor: redondos
Comment: extention -> extension
Revision 3 as of 2007-11-30 02:55:12
Size: 999
Editor: GreyCat
Comment: change internal links
Deletions are marked like this. Additions are marked like this.
Line 28: Line 28:
For more hints in this direction, see [#faq20 FAQ #20], below. To see why the find command comes after the loop instead of before it, see [#faq24 FAQ #24]. For more hints in this direction, see [:BashFAQ#faq20:FAQ #20], below. To see why the find command comes after the loop instead of before it, see [:BashFAQ#faq24:FAQ #24].

Anchor(faq15)

How can I run a command on all files with the extension .gz?

Often a command already accepts several files as arguments, e.g.

    zcat *.gz

(One some systems, you would use gzcat instead of zcat. If neither is available, or if you don't care to play guessing games, just use gzip -dc instead.) If an explicit loop is desired, or if your command does not accept multiple filename arguments in one invocation, the for loop can be used:

    for file in *.gz
    do
        echo "$file"
        # do something with "$file"
    done

To do it recursively, you should use a loop, plus the find command:

    while read file; do
        echo "$file"
        # do something with "$file"
    done < <(find . -name '*.gz' -print)

For more hints in this direction, see [:BashFAQ#faq20:FAQ #20], below. To see why the find command comes after the loop instead of before it, see [:BashFAQ#faq24:FAQ #24].

BashFAQ/015 (last edited 2015-03-05 00:30:34 by izabera)