I have a directory tree containing archive .tar.gz files that I want to extract at the location where they recide.
How can I achieve such an operation?
I have a directory tree containing archive .tar.gz files that I want to extract at the location where they recide.
How can I achieve such an operation?
Hi,
Did you try man tar
tar xzf file.tar.gz
Try something like this?
find "$(pwd)" -type d |
while read dir
do
cd "$dir" || continue
for archive in *.tar.gz
do
if [ -r "./$archive" ]; then
tar zxf "./$archive"
fi
done
done
Wouldn't you need to account for the archives being gzipped; either by gunzip
ping them first or by adding the -z
option to tar
.
Yes, indeed -z had fallen off. Added to my post...
I am specifying the directories where the extraction is to take place in the variable argv_dir
. For example having argv_dir="graphics"
is giving me the error
./untar.sh: line 62: cd: graphics/maverik: No such file or directory
./untar.sh: line 62: cd: graphics/panorama: No such file or directory
./untar.sh: line 62: cd: graphics/guile-opengl: No such file or directory
./untar.sh: line 62: cd: graphics/3dldf: No such file or directory
./untar.sh: line 62: cd: graphics/plotutils: No such file or directory
./untar.sh: line 62: cd: graphics/gpaint: No such file or directory
./untar.sh: line 62: cd: graphics/dia: No such file or directory
./untar.sh: line 62: cd: graphics/gift: No such file or directory
./untar.sh: line 62: cd: graphics/libxmi: No such file or directory
./untar.sh: line 62: cd: graphics/xaos: No such file or directory
./untar.sh: line 62: cd: graphics/gimp: No such file or directory
./untar.sh: line 62: cd: graphics/gsegrafix: No such file or directory
The code is as follows
for f in ${argv_dir} ; do
find "${f}" -type d |
while read dir
do
cd "${dir}" || continue
for archive in *.tar.gz
do
if [ -r "./$archive" ]; then
echo "$dir"
echo -e "tar -zxf ./$archive\n"
#tar -zxf ./$archive
fi
done
done
done
However when I use find "$(pwd)/$f" -type d
things work ok
I want to be able to use both a relative path or a full path in the variable f
Some versions of tar
(and cpio
and pax
) need to be given options to specify the type of archive to create when building a new archive, but automatically detect the archive type when extracting from or adding to an existing archive. Since we haven't been told what operating system is being used in this case, there isn't any way for us to check the man pages for that system to see if -z
option is required when extracting files for the OP's system. The standards require pax
to do this, but no longer require that cpio
and tar
even be present on conforming implementations. Although the standards never required that any of these utilities produce or process compressed archives, I don't remember seeing any version of pax
that has options to produce a compressed archive that needed to be told that that archive was compressed when extracting files from it.
One might note that using the command argv_dir="graphics"
sets argv_dir
to a relative pathname and that your script attempts to change directory into a directory based on that relative path every time it reads a directory from the find
command's output. Since it never changes directory back to its starting point from the directories it descends into, the 2nd time through the loop is highly unlikely to find the directory that find
found under ./graphics
when it is sitting in ./graphics/first_directory
where first_directory is the name of the first directory that find
found in ./graphics
.
You might have a better chance of getting what you want if you set argv_dir
to an absolute pathname. Alternatively, you could change the loop to move you back to your original location after processing a subdirectory:
do
cd "${dir}" || continue
for archive in *.tar.gz
do
if [ -r "./$archive" ]; then
echo "$dir"
echo -e "tar -zxf ./$archive\n"
#tar -zxf ./$archive
fi
done
cd -
done