Как извлечь все сжатые файлы в каталог и подкаталоги и переименовать с префиксом родительского каталога - PullRequest
0 голосов
/ 12 февраля 2020

У меня есть список *.tar.gz файлов с именем A01.tar.gz... A10.tar.gz. Каждый файл содержит каталог с именем Files, и этот каталог содержит 3 файла .gz с именем 1.txt.gz, 2.csv.gz and 3.tsv.gz. Я хотел бы извлечь все файлы gz в один каталог и переименовать их с префиксом их родительского / родительского каталога. пример: A01_1.txt, A10_.1.txt. Как я могу сделать это с bash?

Редактировать: Я нашел решение

for filename in *.tar.gz; do
  tar xvzf "$filename" --one-top-level; #the file will have the same name as the tar.gz file
    for F in "${filename%%.*}"/*/*.gz; do 
     STEM=$(basename "${F}" .gz) #get the base names of the target .gz files
     gunzip -c "${F}" > ./"${filename%%.*}${STEM}" #redirects the output of gunzip to a file which will have the prefix of the grand parent file+ the base name of the target .gz file.
    done
  done #all the files will be extracted to the working directory and will have the prefix of their original tag.gz file.

Спасибо всем, кто ответил.

...