Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-13-2006, 02:29 AM
|
#1
|
|
Member
Registered: Jan 2006
Posts: 70
Rep:
|
Script tar+gzip traverse a directory
Hi Guys,
I would like to ask a help in creating a script that would traverse a directory and doing a tar on those directories. Example scenario is this. I have a folder named temp and in that directory, I have 3 other directories which I want to tar on a different file location, say for example /home/user1. How am I be able to accomplish such. I am not familiar with bash/perl programming as I have only started learning it. Thanks in advance.
|
|
|
|
06-13-2006, 04:46 AM
|
#2
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
you want to copy directory trees to another location?
|
|
|
|
06-13-2006, 05:21 AM
|
#3
|
|
Member
Registered: Jan 2006
Posts: 70
Original Poster
Rep:
|
something like that. but i want it to be a tar.gz. I found some codes on the internet and it looks something like this.
#!/bin/perl
use strict;
use warnings;
use File::Find;
my $directory='/some_dir/files';
my $location='/usr/home/files1/';
find (\&fetch_directory, $directory);
sub fetch_directory{
#if its a directory print its name
if (-d && $File::Find::name ne $directory)
{
if(!system("sudo tar czf /usr/home/files1/$_.tar.gz $Fil
e::Find::name")){
print "$_ compressed.\n"}
else{
print "$_ not compressed.\n"
}
}
}
Please feel free to comment on the code. I just modified code from internet to suite my needs.
|
|
|
|
06-13-2006, 05:23 AM
|
#4
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
?????
well it's easy isn't it?
tar czvf blah.tgz dir
don't get what all the perl is for?
you need to explain better
|
|
|
|
06-13-2006, 06:43 AM
|
#5
|
|
Senior Member
Registered: Jul 2004
Location: Nantes (France)
Distribution: Arch Linux
Posts: 1,897
Rep:
|
Do you mean something like this?
Code:
~# cd /temp
/temp# tar -cf - subdirA subdirB subdirC | (cd /home/user1 && tar -xf -)
Yves.
|
|
|
|
06-13-2006, 09:01 PM
|
#6
|
|
Member
Registered: Jan 2006
Posts: 70
Original Poster
Rep:
|
thanks for all your input. i got the script working already. thanks.
|
|
|
|
06-16-2006, 01:25 AM
|
#7
|
|
Member
Registered: Jan 2006
Posts: 70
Original Poster
Rep:
|
Hi Guys,
How can I modify the script such that it will only process the parent directory in /some_dir/files folder? My problem now is that it goes down up to the last file/folder of the sub directories. Thanks.
|
|
|
|
06-16-2006, 01:52 AM
|
#8
|
|
Member
Registered: Jan 2006
Posts: 70
Original Poster
Rep:
|
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:04 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|