Solved

Custom tar script to traverse directories and creat individual tarballs? **Help**

Posted on 2009-05-06
10
714 Views
Last Modified: 2013-12-02
So i have directive listed

 ls -la
total 44
drwxr-xr-x 10 root       root       4096 May  6 12:32 .
drwxr-xr-x 22 root       root       4096 Apr 15 11:03 ..
drwxr-xr-x 13 root       root       4096 Jan  9 12:38 adodb
drwxr-xr-x  2 root       root       4096 Nov 12 10:44 cgi-bin
drwxr-xr-x  3 root       root       4096 Jan  8 14:07 error
drwxr-xr-x  2 root       root       4096 Apr  6 13:39 html
drwxr-xr-x  3 root       root       4096 Jan  8 14:09 icons
drwxrw-r--  5 virtualftp virtualftp 4096 May  5 12:40 iWatch
drwxr-x---  4 virtualftp virtualftp 4096 May  5 12:45 Marvel
drwxr-xr-x  2 root       root       4096 May  6 12:34 users

I want a tarball of each folder seperately like:

Marvel.tar.gz
iWatch.tar.gz
etc

Would this work?

#!/bin/bash
for dir in */
    do dir=`echo $dir | tr -d '/'`
    echo $dir
    tar czf $dir.tar.gz $dir
done
 

I've attempted this:

tar -czf -f !.tar.gz !
-bash: !.tar.gz: event not found

So please help me here im lost. I dont know perl that well. I understand the code and the logic. Just dont know
the syntax
0
Comment
Question by:eunit3130
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 

Author Comment

by:eunit3130
ID: 24317882
May i also add that i the directory listing i have above is a sample. The one i really have has multiple folders that actually are listed as:

blah.sample.com
blah2.sample.com
blah3.sample.com
blah4.sample.com
randomfolder
cgi-stuff
phpStuff

i only want the direcoties that have the ending ".com"
0
 

Author Comment

by:eunit3130
ID: 24317986
would this work?


 
#!/bin/bash
for dir in */
    if($dir =~ m/(com)/)
do 
    dir=`echo $dir | tr -d '/'`
      echo $dir
      tar czf /newlocation/$dir.tar.gz $dir
done

Open in new window

0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 500 total points
ID: 24319132
try this

for file in *
do
   if [ -d $file ]
   then
         tar czf  /newlocation/$file.tar.gz $file
   fi
done
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:eunit3130
ID: 24327507
i understand what your saying but i want only directories with the "com" word within its directory name. So the directories listed:

site1.domain.com
site2.domain.com
site3.domain.com
site3.domain.com

etc.  Would this do the trick?


#!/bin/bash
for dir in */
  do 
   if[$dir |grep -e \<com>\="com"]
    then
      dir=`echo $dir | tr -d '/'`
      echo $dir
      tar czf /newlocation/$dir.tar.gz $dir
   fi
done

Open in new window

0
 

Author Comment

by:eunit3130
ID: 24329322
this works!
#!/bin/bash
 
for dir in */
do
    dir=`echo $dir | tr -d '/'`
    echo $dir
    tar czf /resultsScript/$dir.tar.gz $dir

Open in new window

0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 500 total points
ID: 24332750
good :)
0
 

Author Comment

by:eunit3130
ID: 24336700
however what im looking for is a single tar line command that would tar all directores ending in .com to there own individual tar balls without runnning a script.
ex:
tar czf /tmp/results/$1.tar  *'com/'
tar: /tmp/results/.tar: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now

Open in new window

0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 500 total points
ID: 24337103
why don't you save the script in a file, make it executable (with chmod +x filename) and then call it with arguments
0
 

Accepted Solution

by:
eunit3130 earned 0 total points
ID: 24337549
Solution:
[root@testWeb user]# ls | grep com | while read line;
> do
> tar czf /tmp/$line.tar.gz $line
> done
[root@testWeb user]# ls /tmp
test1.com.tar.gz   test2.com.tar.gz   test3.com.tar.gz

Open in new window

0
 

Author Comment

by:eunit3130
ID: 24376266
ls -A -d -1 */ |  awk -F'/' '{ print $1 }' | xargs -I {} tar czpf /tmp/'{}.tar.gz' '{}' && rm -Rf */

will tarball each dir in the current folder in its own archive and remove the source folder
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Samba is the de-facto standard program (or, more correctly: suite of programs) that UNIX and Linux systems use to share files with Microsoft Windows (and more recently, Mac OS-X) systems. Currently, there are 2 common versions of Samba available,…
Have you ever stumbled upon a software that is so great that you just love? It happened to me. Love at first sight. Filezilla Server.   Ok its not the most advanced ftp server I've came across. But its a fairly simple piece of software to get the …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question