Differences between revisions 2 and 12 (spanning 10 versions)
Revision 2 as of 2007-05-28 22:24:31
Size: 983
Editor: redondos
Comment: extention -> extension
Revision 12 as of 2015-03-05 00:30:34
Size: 1437
Editor: izabera
Comment: syntax hl
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq15)]] <<Anchor(faq15)>>
Line 5: Line 5:
{{{
    zcat *.gz
{{{#!highlight bash
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.
Line 11: Line 11:
{{{
    for file in *.gz
    do
        echo "$file"
        # do something with "$file"
    done
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:

{{{#!highlight bash
# Bourne
for file in ./*.gz
do
    echo "$file"
    # do something with "$file"
done
Line 19: Line 24:
To do it recursively, you should use a loop, plus the find command: To do it recursively, use [[UsingFind|find]]:
Line 21: Line 26:
{{{
    while read file; do
        echo "$file"
        # do something with "$file"
    done < <(find . -name '*.gz' -print)
{{{#!highlight bash
# Bourne
find . -name '*.gz' -type f -exec do-something {} \;
Line 28: Line 31:
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]. If you need to process the files inside your shell for some reason, then read the `find` results in a loop:

{{{#!highlight bash
# Bash
while IFS= read -r file; do
    echo "Now processing $file"
    # do something fancy with "$file"
done < <(find . -name '*.gz' -print)
}}}

This example uses ProcessSubstitution (see also [[BashFAQ/024|FAQ #24]]), although a pipe may also be suitable in many cases. However, it does not correctly handle filenames that contain newlines. To handle arbitrary filenames, see [[BashFAQ/020|FAQ #20]].

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

   1 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:

   1 # Bourne
   2 for file in ./*.gz
   3 do
   4     echo "$file"
   5     # do something with "$file"
   6 done

To do it recursively, use find:

   1 # Bourne
   2 find . -name '*.gz' -type f -exec do-something {} \;

If you need to process the files inside your shell for some reason, then read the find results in a loop:

   1 # Bash
   2 while IFS= read -r file; do
   3     echo "Now processing $file"
   4     # do something fancy with "$file"
   5 done < <(find . -name '*.gz' -print)

This example uses ProcessSubstitution (see also FAQ #24), although a pipe may also be suitable in many cases. However, it does not correctly handle filenames that contain newlines. To handle arbitrary filenames, see FAQ #20.


CategoryShell

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