Skip to main content
added 258 characters in body
Source Link
cas
  • 84.9k
  • 9
  • 138
  • 207

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"$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"$f" -C "$dir" fileofinterest.xml
done

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

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

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
Source Link
cas
  • 84.9k
  • 9
  • 138
  • 207

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

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