473,507 Members | 2,377 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 6319
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($starting_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_dir/$filename") {
# Extract the `INSTALL' file
system $tar_path, '-xf', $filename, 'INSTALL';
}

# Check to see if this is a subdirectory
if (-d "$current_dir/$filename") {
# Go through this directory
extract_dir("$current_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
6949
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...
8
8816
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...
4
3043
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...
0
1473
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...
12
1589
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...
13
2771
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...
12
5706
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...
2
2497
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...
1
1619
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
7223
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,...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
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,...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5042
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
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 ...
0
412
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...

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.