LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 02: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, 04:46 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you want to copy directory trees to another location?
 
Old 06-13-2006, 05: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, 05:23 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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

Rep: Reputation: 66
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, 09: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, 01: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, 01: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



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 10:48 AM
When to use tar , gzip and tar+gzip ? tofee Linux - Newbie 4 03-31-2006 05:59 AM
script to gzip all directories in a directory br8kwall Programming 11 09-06-2005 10:42 PM
tar and gzip c0c0deuz Linux - General 2 10-31-2002 03:04 PM
is there a way to traverse a directory structure & execute a command hemlock Linux - Newbie 1 09-07-2002 12:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:08 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
Open Source Consulting | Domain Registration