Solved

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

Posted on 2011-09-16
6
276 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 76

Accepted Solution

by:
arnold earned 250 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 76

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 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 125 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 19

Assisted Solution

by:xterm
xterm earned 125 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 76

Assisted Solution

by:arnold
arnold earned 250 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now