LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-26-2010, 01:27 PM   #1
brownviper
LQ Newbie
 
Registered: May 2010
Posts: 2

Rep: Reputation: 0
script help


Hi Guys,

totally new to scripting.
I have a large list of folders that I want to compress using tar.
the script I want to right is to travers all the folders and for each I would tar the contents to a tar file with the same name of that folder. and the it should be stored inside the same forlder.

whats the best way to go about this?

TIA

Viper
 
Old 05-26-2010, 01:42 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

When you say traverse: do the directories have subdirectories
that need to be tared individually?


Cheers,
Tink
 
Old 05-26-2010, 02:09 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Pseudo-code:

Code:
for file in $(ls -R); do
     if file is a directory
           tar -czvf <$file+otherstuff.tar.gz> $file/*
     fi
done
Replace this:
<$file+otherstuff.tar.gz>
with the syntax to get the archive name that you want

Last edited by pixellany; 05-26-2010 at 02:11 PM.
 
Old 05-26-2010, 07:51 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
pixellany's code would work, but are you only wanting to build tars of directories that ONLY contain files?

eg
Code:
dir1 - dir2 - file1
            - file2
     - dir3
     - file1
     - file2
From the above example should the tar of dir1 only contain file1 & file2 that is at first level or should all the sub directories and files be there too?
 
Old 05-27-2010, 05:08 PM   #5
brownviper
LQ Newbie
 
Registered: May 2010
Posts: 2

Original Poster
Rep: Reputation: 0
The folders I want to compress contain files and folders.
 
Old 05-27-2010, 05:22 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by brownviper View Post
The folders I want to compress contain files and folders.
Let's start again. Do you want each subdirectory, no matter
where in the structure, compresses & tared individually?
Or just on the top level?


Cheers,
Tink
 
Old 05-27-2010, 05:57 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by brownviper View Post
The folders I want to compress contain files and folders.
Code:
tar -czvf archivename.tar.gz dirname/*
This will put everything in dirname into the archive--regardless of whether it is a file or a directory.

Try commands like this before putting them in a script.
 
  


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
Need help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 08:29 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 05:50 PM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM


All times are GMT -5. The time now is 10:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration