Bash脚本-遍历某目录下的所有的tar文件,依次以文件名为目录名解压到另一个目录下。

脚本代码如下:

#!/bin/sh

TARDIR="/ifs/DRR_Datasets/Seismic_Dataset/M_E"
UNTARDIR="/ifs/DRR_Datasets_Unzipped/Seismic_Dataset/M_E"

printf "Entered path: $TARDIR.\n\n"
cd "$TARDIR"

for tar in *.tar
do
  dirname=`echo $tar | sed 's/\.tar$//'`
  printf "Directory name to extract this file is: %s.\n" $dirname
  dirfullpath="$UNTARDIR/$dirname"
  printf "Directory full path to extract this file is:%s.\n" $dirfullpath
  
  mkdir "$dirfullpath"
  tar -xvf $tar -C $dirfullpath 

  printf "\n\n"
  
done

同样的,解压zip的代码也是一样的,替换掉解压命令即可:

#!/bin/sh

ZIPDIR="/ifs/DRR_Datasets_Unzipped/More_Dataset/Data_Warehouse_data/instacart-market-basket-analysis/zips"
UNZIPDIR="/ifs/DRR_Datasets_Unzipped/More_Dataset/Data_Warehouse_data/instacart-market-basket-analysis/unzips"

printf "Entered path: $ZIPDIR.\n\n"
cd "$ZIPDIR"

for zip in *.zip
do
  dirname=`echo $zip | sed 's/\.zip$//'`
  printf "Directory name to extract this file is: %s.\n" $dirname
  dirfullpath="$UNZIPDIR/$dirname"
  printf "Directory full path to extract this file is:%s.\n" $dirfullpath
  
  mkdir "$dirfullpath"
  unzip $zip -d $dirfullpath
  printf "\n\n"
  
done

posted on 2020-07-13 14:25  中道学友  阅读(219)  评论(0编辑  收藏

导航

技术追求准确,态度积极向上