Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Want to tar/gzip a few files into a specific directory structure?

Posted on 2011-09-16
6
Medium Priority
?
309 Views
Last Modified: 2012-05-12
Hello,

I have several images that I want to tar or gzip into specific directories.  For instance:
image1.png => my_images/image1.png
image2.png => thumbnail/image2.png

So basically I want to specify when creating the archive, "put these files in this directory in the archive and these files in another directory".

That is the first part of the question.  The second part is while uncompressing I want to also specify a different destination for all the files depending on the directory they are in.  If the second part is not possible I can uncompress them all and then just run copy commands for each specific file.

I'm doing this in PHP but I probably will end up calling a shell script to all of it.  Thanks for any help or advice!!
0
Comment
Question by:parlays
6 Comments
 
LVL 80

Accepted Solution

by:
arnold earned 1000 total points
ID: 36550887
The file has to be in the directory.

presumably you are appending data to an existing tar archive?
tar --append --file=oldarchive.tar my_images/image1.png
http://www.gnu.org/software/automake/manual/tar/appending-files.html
0
 
LVL 80

Expert Comment

by:arnold
ID: 36550943
Oh, it would be a two step process sine the append can only be done to the raw archive versus compressed one.

Not sure whether you can append a compresed tar archive entry into a Gziped file.
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 500 total points
ID: 36554031
Easiest way to do this is copy or move the files to the required directories first and then tar/gz the directories. Do you know which files need to go into which directory? Is there a pattern?

So you could do, assuming that the current directory is where the images are:

mkdir my_images thumbnails
mv <filepattern1> my_images/
mv <filepattern2> thumbnail/
tar cf images.tar my_images thumbnail
gzip images.tar

Open in new window


Note that <filepattern1> and <filepattern2> are somthing like image*.png and thumb*.png, depending on your filenames.

When uncompressing you can do it in a similar way by creating a tmp directory (t.t) first:

mkdir t.t
gunzip images.tar.gz
mv images.tar t.t/
cd t.t
tar xf images.tar
mv my_images/* <destination dir for images>/
mv  thumbnail/* <destination dir for thumbnails>/

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 19

Assisted Solution

by:xterm
xterm earned 500 total points
ID: 36581264
FYI, modern gnu tar has g(un)zip capabilities built-in, so you can save a few steps:

tar czf images.tar.gz my_images thumbnail

and

tar xzf images.tar.gz

(and omit the gzip/gunzip stuff)
0
 
LVL 80

Assisted Solution

by:arnold
arnold earned 1000 total points
ID: 36581691
While the gunzip/gzip option exist, it does not work with adding files into an existing archive.

i.e. the user has an existing archive into which directory/file needs to be added/appended.

tar -cf tarfile.tar somedirectory
tar -rf tarfile.tar somenewdirectory
0
 

Author Closing Comment

by:parlays
ID: 36582099
thanks everyone!!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question