linux - how to untar tgz files distinctly -
i have number of .tgz files exact same name, want untar them in 1 directory uniq names. preference append name of folder .tgz came from,
original location= /folder1/folder2/2015/04/08/something1.tgz original location= /folder1/folder2/2015/04/09/something2.tgz
after untar, them decompressed below
target_folder= 20150408_something1 target_folder= 20150409_something2
something1.tar.gz
, something2.tar.gz
don't have same name. assume mean contain same toplevel folder/files?
in case (and assuming folder hierarchy consistent in path tarballs) should able use this.
for arc in /folder1/folder2/2015/04/08/something1.tgz /folder1/folder2/2015/04/09/something2.tgz; # filename of archive. fname=$(basename "$arc") # strip off extension filename. fname=${fname%.*} # last 3 directory components. dname=$(awk -f / '{print $(nf-3)$(nf-2)$(nf-1)}' <<<"$arc") mkdir "${dname}_$fname" || { ret=$?; echo "could not create directory ${dname}_$fname."; exit $ret; } tar -xvf "$arc" -c "${dname}_$fname" done
Comments
Post a Comment