解压缩不同文件夹中的.gz文件,并将它们合并为一个文件

时间:2012-10-01 07:35:04

标签: linux bash unix gz

我在不同的文件夹中有几个同名的.gz文件。所以我想解压缩所有这些.gz文件,并将所有输出文件合并到一个文件中。

3 个答案:

答案 0 :(得分:1)

find . -name "xyz.gz"|xargs zcat >output_file

答案 1 :(得分:1)

如果您事先不知道文件的名称,您可能会发现以下脚本很有用。您应该将其作为my-script.sh /path/to/search/for/duplicate/names /target/dir/to/create/combined/files运行。它查找给定路径中出现多个文件名的所有文件名,并将其内容组合到目标目录中的单个文件中。

#! /bin/bash
path=$1
target=$2
[[ -d $path   ]] || { echo 'Path not found'   ; exit 1 ; }
[[ -d $target ]] || { echo 'Target not found' ; exit 1; }

find "$path" -name '*.gz' | \
    rev | cut -f1 -d/ | rev | \              # remove the paths
    sort | uniq -c | \                       # count numbers of occurrences
    grep -v '^ *1 ' | \                      # skip the unique files
    while read _num file ; do                # process the files in a loop
    find -name "$file" -exec zcat {} \; | \  # find the files with the given name and output their content
        gzip > "$target/${file##*/}"         # gzip the target file
done

答案 2 :(得分:0)

find some/dir -name foo.gz -exec zcat {} \; > output.file
相关问题