Differences between revisions 5 and 7 (spanning 2 versions)
Revision 5 as of 2008-11-22 14:09:46
Size: 1032
Editor: localhost
Comment: converted to 1.6 markup
Revision 7 as of 2009-12-30 08:28:46
Size: 1146
Editor: MatthiasPopp
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
    zcat *.gz     zcat -- *.gz
Line 9: Line 9:
(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: 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:
Line 31: Line 35:

----
CategoryShell

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 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 FAQ #20. To see why the find command comes after the loop instead of before it, see FAQ #24.


CategoryShell

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