【发布时间】:2012-10-01 07:35:03
【问题描述】:
我在不同的文件夹中有几个同名的 .gz 文件。所以我想解压缩所有这些 .gz 文件并将所有输出文件合并到一个文件中。
【问题讨论】:
我在不同的文件夹中有几个同名的 .gz 文件。所以我想解压缩所有这些 .gz 文件并将所有输出文件合并到一个文件中。
【问题讨论】:
find . -name "xyz.gz"|xargs zcat >output_file
【讨论】:
如果您事先不知道文件的名称,您可能会发现以下脚本很有帮助。你应该以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
【讨论】:
find some/dir -name foo.gz -exec zcat {} \; > output.file
【讨论】: