0

I've got a folder with thousands of gzipped archive files that contain several XML files. Now I need a script that loops over all archive files and selectively extracts a file with a certain filename (i.e. fileofinterest.xml) into a directory that has to be created by using the filename of the gzipped archive without the extension tar.gz.

So here's a sample of the archive directory containing the gzipped archive files and the desired output-directory with generated directories and extracted files:

home/
├─ archives/
│  ├─ archive.20.01.tar.gz
│  ├─ archive.20.02.tar.gz
├─ extracted/
│  ├─ archive.20.01/
│  │  ├─ fileofinterest.xml
│  ├─ archive.20.02/
│  │  ├─ fileofinterest.xml

This is my code so far, creation of directories is completely missing:

for f in ./*.tar.gz; do echo "Processing $f"; tar -x -f $f -C ../extracted/ fileofinterest.xml; done

2 Answers 2

1

If you're using bash (not ash or similar POSIX-limited shell), you could do something like this:

for f in ./*.tar.gz; do
  dir="../extracted/${f/%.tar.gz}"
  mkdir -p "$dir"
  echo "Processing $f"
  tar -x -f "$f" -C "$dir" fileofinterest.xml
done

Note: $dir will have values like ../extracted/./archive.20.01, but (except for aesthetics) this doesn't matter, any extra ./ directories in a path are effectively ignored. If it bothers you, add dir=${dir/\/.\//\/} before the mkdir -p.

Otherwise, you could use basename:

for f in ./*.tar.gz; do
  dir="../extracted/$(basename "$f" .tar.gz)"
  mkdir -p "$dir"
  echo "Processing $f"
  tar -x -f "$f" -C "$dir" fileofinterest.xml
done
1

Untested, but should get you in the right direction:

for f in *.tar.gz; 
do 
    echo "Processing $f"
    BaseName=${f::-6}
    mkdir ../extracted/$BaseName
    tar -x -f $f -C ../extracted/$BaseName fileofinterest.xml
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.