Differences between revisions 6 and 10 (spanning 4 versions)
Revision 6 as of 2009-09-15 17:42:25
Size: 1123
Editor: GreyCat
Comment: fixes
Revision 10 as of 2014-09-16 04:24:56
Size: 200
Editor: E9659
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
<<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
}}}

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

The `--` prevents a filename beginning with a hyphen from causing unexpected results.

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:

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

To do it recursively, you should use a loop, plus the [[UsingFind|find]] command:

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

For more hints in this direction, see [[BashFAQ/020|FAQ #20]]. To see why the `find` command comes after the loop instead of before it, see [[BashFAQ/024|FAQ #24]].
Light Technician Anthony from Carignan, has hobbies and interests including cycling, Samsung,nokia,sony Xperia,iphone, and dolls. During the last year has made a visit to Rock Shelters of Bhimbetka.

Light Technician Anthony from Carignan, has hobbies and interests including cycling, Samsung,nokia,sony Xperia,iphone, and dolls. During the last year has made a visit to Rock Shelters of Bhimbetka.

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