LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 06-13-2006, 03:29 AM   #1
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Rep: Reputation: 15
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.
 
Old 06-13-2006, 05:46 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
you want to copy directory trees to another location?
 
Old 06-13-2006, 06:21 AM   #3
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
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.
 
Old 06-13-2006, 06:23 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
?????
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
 
Old 06-13-2006, 07:43 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Do you mean something like this?
Code:
~# cd /temp
/temp# tar -cf - subdirA subdirB subdirC | (cd /home/user1 && tar -xf -)
Yves.
 
Old 06-13-2006, 10:01 PM   #6
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
thanks for all your input. i got the script working already. thanks.
 
Old 06-16-2006, 02:25 AM   #7
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
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.
 
Old 06-16-2006, 02:52 AM   #8
gn00kie
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
I think this thread is the answer to my problems...

http://www.linuxquestions.org/questi...d.php?t=360881
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tar and gzip - how do i point to a output directory RedHat123 Linux - Newbie 4 04-20-2011 11:48 AM
When to use tar , gzip and tar+gzip ? tofee Linux - Newbie 4 03-31-2006 06:59 AM
script to gzip all directories in a directory br8kwall Programming 11 09-06-2005 11:42 PM
tar and gzip c0c0deuz Linux - General 2 10-31-2002 04:04 PM
is there a way to traverse a directory structure & execute a command hemlock Linux - Newbie 1 09-07-2002 01:22 AM


All times are GMT -5. The time now is 08:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration