473,748 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Script to automate extraction of file from compressed archives

I am a perl newbie who is trying to write a script to automate a
task.

I have a large collection of compressed archives (mostly .tar.gz,
tar.bz2, tar.Z, .tgz etc). This are stored in a number of directories
/ sub-directories).

I am looking for a script that will recursively extract a single file
from each of these archives e.g. the file INSTALL, for the extracted
file to be moved to a different location and renamed to the name of
the archive itself, but keeping the same directory structure;

e.g.

Suppose I have archives files, x.tar.gz, y.tar.gz, and x.tar.Z in
/home/peter/a/

and in /home/peter/a/b/ files ab.tar.gz, b.tar.bz2, c.tgz

I would like the script to recursively extract the INSTALL from all of
these files, for the INSTALL files to be copied to /tmp, and renamed
to the name of the archive, so that in

/tmp/a/ there will be files named x.tar.gz, y.tar.gz, and x.tar.Z
(which are just the relevant INSTALL files), and in /tmp/a/b/ files
ab.tar.gz, b.tar.bz2, c.tgz (again these files to be just the INSTALL
files).
I appreciate that tar -zf name.tar.gz -x <file name> extracts just a
file, but it creates directories etc, which mean the above is
unworkable.

Would be really grateful for any help you can give. Please bear in
mind that I am not very technically minded.

Thanks,
Peter
pe************* ***@fastmail.fm

May 7 '06 #1
1 6334
Peter Thorne wrote:
I am looking for a script that will recursively extract a single file
from each of these archives e.g. the file INSTALL, for the extracted
file to be moved to a different location and renamed to the name of
the archive itself, but keeping the same directory structure;


This should be quite straightforward , especially if you intend to use an
external program like `tar' to unarchive your files.

You should need only one function that accepts as its sole argument a
directory name. This function is recursive, in that it calls itself,
passing along the name of whatever subdirectory it's currently looking
at. This function is just called from one starting point in your
program, and should be passed the initial directory name (perhaps from a
command-line switch).

The code might look like:

#!/usr/bin/perl

my $tar_path = '/bin/tar'; # Path to the tar program
my $starting_dir = shift; # Starting point for extraction

extract_dir($st arting_dir);

exit;

sub extract_dir {
my $current_dir = shift; # Current directory level

# Get a filehandle for this directory
opendir my $DIR, $current_dir;

# Go through each file in this directory
for my $filename (readdir $DIR) {
# Check to see if this is a regular file
if (-f "$current_d ir/$filename") {
# Extract the `INSTALL' file
system $tar_path, '-xf', $filename, 'INSTALL';
}

# Check to see if this is a subdirectory
if (-d "$current_d ir/$filename") {
# Go through this directory
extract_dir("$c urrent_dir/$filename");
}
}

return;
}
And of course, you'll want to add additional logic to make sure that the
current file it's iterating over isn't the same directory or a parent
directory (`.' and `..') or some cyclical symlink, and that if it's a
regular file, that it's an archive you actually want to extract. Given
your cited example, you'll probably want to also check the type of
archive in order to pass `tar' the appropriate arguments (since of
course, my example only works on a basic uncompressed archive).

Hope this gives some direction.

- Michael Wehner
May 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
6986
by: Chuck Amadi | last post by:
Sorry to bovver you again (again) here's script. I still can't see why the get_payload() doesn't produce the plain text message body of an emails in the testwwws users mailbox. As you can see I have tried a few things but no joy what am I missing. Is the another snippet in relation to get_payload to access the body contents print and process to a file. Cheers
8
8843
by: Dennis Hotson | last post by:
Hi, I'm trying to write a function that adds a file-like-object to a compressed tarfile... eg ".tar.gz" or ".tar.bz2" I've had a look at the tarfile module but the append mode doesn't support compressed tarfiles... :( Any thoughts on what I can do to get around this?
4
3065
by: googlinggoogler | last post by:
Hiya, The title says it all really, but im a newbie to python sort of. I can read in files and write files no probs. But what I want to do is read in a couple of files and output them to one single file, but then be able to take this one single file and recreate the files I put into it. Im really at a loss as to how I go about recovering the files?
0
1484
by: Howell, Scott | last post by:
I am looking for a simple extraction/load program to load data daily from a Progress 8.3B database via ODBC to a mysql 3.23.57 database. It does not necessarily need to do any data-type transforms. It need to run on Windows 2000 and be able to run it as a service, or as a cron task if running under Unix. Any suggestions? --
12
1613
by: Charles Law | last post by:
This is a bit of a vague question, but I am just starting on this, and wonder if anyone has ideas of where to start. I have a program that controls some external equipment. It sends messages in various flavours to cause the equipment to turn signals on and off, and change voltages and such like. Attached to this external equipment, is another piece of equipment that responds to these changes in signals and voltages.
13
2802
by: Leonardo Francalanci | last post by:
With mysql I know how much space a row will take, based on the datatype of it columns. I also (approximately) know the size of indexes. Is there a way to know that in postgresql? Is there a way to pack (compress) data, as with myisampack for mysql? Thank you
12
5745
by: pac | last post by:
I'm preparing to distribute a Windows XP Python program and some ancillary files, and I wanted to put everything in a .ZIP archive. It proved to be inordinately difficult and I thought I would post my solution here. Is there a better one? Suppose you have a set of files in a directory c:\a\b and some additional files in c:\a\b\subdir. Using a Python script, you would
2
2508
by: Vyz | last post by:
I am looking for a PDF to text script. I am working with multibyte language PDFs on Windows Xp. I need to batch convert them to text and feed into an encoding converter program Thanks for any help in this regard
1
1627
snowfall
by: snowfall | last post by:
My project is compressed in the .dat format through the VisualAge for Java(VAJ) IDE. I want to extract it and my IDE is corrupt. Can you suggest someother ways to extract a .dat file? TIA
0
8994
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4878
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.