Shell脚本一枚

脚本如下:

#!/bin/bash
dir=$1
dir1=""
for file in `find $dir -type f`
do
   dir2=${file%/*}
   name=${file%.*}
   name1=${name%.*}
   cd $dir2
   gunzip $file
   if [ -z "$dir1" -o "$dir1" != "$dir2" ];then
      gzip -c "$name" > "$name1".gz
      rm -rf "$name"
      dir1="$dir2"
   else
      gzip -c "$name" >> "$name1".gz
      rm -rf "$name"
   fi
done

需要说明的是:

1. ${file%/*}:拿掉最后一个 / 及其右边的字符串

2. ${file%.*}:拿掉最后一个 .  及其右边的字符串

3. 第一个gzip是将file1压缩成foo.gz,第二个gzip是将file2添加到foo.gz中

    gzip -c file1 > foo.gz
    gzip -c file2 >> foo.gz

 

原文地址:https://www.cnblogs.com/ivictor/p/4994701.html