关于:提取*.gz 文件并将原始文件移动到其他文件夹

About: extracting *.gz files and move a original file to other folder

我几乎是 shell 脚本的新手,但不知道某些命令。 我正在尝试写下面的 shell 脚本,请指点一下。 1. 从特定目录读取 *.gz 文件 2.解压到其他文件夹 3. 将原始文件移动到另一个文件夹。 我可以用三个单独的 shell 脚本来完成,但我希望它包含一个 shell 脚本。然后这个脚本将是 cronjob,每 5 分钟 运行。 我试图像下面这样开始,但不知何故我有点困惑如何获取文件列表。我可以在这里做另一个脚本,但想包含在一个脚本中。"


#!/bin/bash

while IFS= read file; do
    gzip -c "$file" > "zipdir/$(basename "$file").gz"
done < filelist
-----------------------------------------

PS: 文件每 5 分钟创建一次。

有几种方法可以实现您正在寻找的东西(我会考虑 notify)。无论如何......这是一个非常简单的实现:

$ source=~/tmp/source   # directory where .gz files will be created
$ target=~/tmp/target   # target directory for uncompressed files
$ archive=~/tmp/archive # archive dir for .gz files
$ shopt -s nullglob     # avoid retiring unexpanded paths
$ for gz in ${source}/*.gz ; do gzip -dc "$gz" > ${target}/$(basename "$gz" .gz) ; mv "$gz" ${archive}/ ; done
$ shopt -u nullglob     # reset nullglob

如果您确定 "source" 目录将始终包含 .gz 文件,您可以避免 shopt。

另一种解决方案(不需要 shopt)是这样的:

find ${source} -name '*.gz' -print0 | while read -d '' -r gz; do
    gzip -dc "$gz" > ${target}/$(basename "$gz" .gz)
    mv "$gz" ${archive}/
done

第一行看起来有点复杂,因为它管理包含空格的源文件名...