LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
 
LinkBack Search this Thread
Old 09-06-2005, 08:13 PM   #1
br8kwall
LQ Newbie
 
Registered: Aug 2005
Location: Youngstown, NY
Distribution: Debian
Posts: 15

Rep: Reputation: 0
script to gzip all directories in a directory


Ok, I feel kinda stupid for asking this because I'm sure there's an easy answer for this simple task. But I can't find an answer so here goes.

I need a bash script that will tar -czf (or zip) all the directories below where I am, but ignore the files. I've been trying to work with a combination of ls -d */ and xargs, but the I think the slashes that the ls command produces keeps screwing me up.

Consider the following example

in /home/jcz

[jcz@actinella ~]# ls -l
total 180
-rw------- 1 jcz jcz 2108 Aug 27 15:54 anaconda-ks.cfg
drwxr-xr-x 2 jcz jcz 4096 Sep 6 21:01 banners
drwxr-xr-x 2 jcz jcz 4096 Sep 6 21:01 documents
-rw-r--r-- 1 jcz jcz 65317 Aug 27 15:54 install.log
-rw-r--r-- 1 jcz jcz 6864 Aug 27 15:54 install.log.syslog
drwx------ 2 jcz jcz 4096 Sep 3 23:19 Mail
-rw-r--r-- 1 jcz jcz 57519 Aug 27 16:04 upgrade.log
-rw-r--r-- 1 jcz jcz 523 Aug 27 16:04 upgrade.log.syslog

I need to produce banners.tar.gz, documents.tar.gz, and Mail.tar.gz, but ignore the files.

But,
as you see te output from ls -d puts the annoying slashes that thwarts my little plan
[jcz@actinella ~]# ls -d */
banners/ documents/ Mail/

Any thoughts here?
 
Old 09-06-2005, 08:42 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
I assume you want the files IN those directories, as well, right?
Code:
for i in */ ; do tar czf $i.tar.gz $i ; done
 
Old 09-06-2005, 09:34 PM   #3
br8kwall
LQ Newbie
 
Registered: Aug 2005
Location: Youngstown, NY
Distribution: Debian
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks fr your help.

Yep, I need all the files to. Using your suggestion I'm getting some boo boos.

This

#!/bin/sh

for i in ls -d */
do
echo $i
tar czf $i.tar.gz $i
done

produces this

[jcz@actinella ~]$ ./zipdir.sh
ls
tar: ls: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors
-d
tar: You may not specify more than one `-Acdtrux' option
Try `tar --help' or `tar --usage' for more information.
cdcatalogs/
ddclient-3.6.6/
Desktop/

Any thoughts on what is throwing tar off?
 
Old 09-06-2005, 09:52 PM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
Yes... Yours is using ls, mine is not.

If you want to use the output of a command, you'd need backticks around the command.
Code:
#!/bin/bash
for i in */
    do echo $i
    tar czf $i.tar.gz $i
done
 
Old 09-06-2005, 09:59 PM   #5
br8kwall
LQ Newbie
 
Registered: Aug 2005
Location: Youngstown, NY
Distribution: Debian
Posts: 15

Original Poster
Rep: Reputation: 0
Hmmm. Well your version produces no errors, but it also makes no files. It runs but doesn't seem to do anything.
 
Old 09-06-2005, 10:02 PM   #6
br8kwall
LQ Newbie
 
Registered: Aug 2005
Location: Youngstown, NY
Distribution: Debian
Posts: 15

Original Poster
Rep: Reputation: 0
Alas, my version using backticks and ls also runs without error but produces no files

#!/bin/sh

for i in `ls -d */`
do
echo $i
tar czf $i.tar.gz $i
done
 
Old 09-06-2005, 10:09 PM   #7
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
Does my script echo anything? Because it works for me.
 
Old 09-06-2005, 10:14 PM   #8
br8kwall
LQ Newbie
 
Registered: Aug 2005
Location: Youngstown, NY
Distribution: Debian
Posts: 15

Original Poster
Rep: Reputation: 0
:-(

here is what I get when I run your script

[jcz@actinella ~]$ ./zipdir.sh
cdcatalogs/
ddclient-3.6.6/
Desktop/
[jcz@actinella ~]$ ls *.gz
backup-8.28.2005_18-27-57_kosbwxj.tar.gz backup-9.4.2005_08-28-49_kosbwxj.tar.gz
backup-8.28.2005_18-46-54_norttfhu.tar.gz backup-9.6.2005_23-03-57_kosbwxj.tar.gz
ddclient.tar.gz
geeklog-1.3.11sr1.tar.gz
[jcz@actinella ~]$
 
Old 09-06-2005, 10:21 PM   #9
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
OOPS. One tiny bug. I bet you'll find a file named '.tar.gz' in each directory.

Code:
#!/bin/bash
for dir in */
    do dir=`echo $dir | tr -d '/'`
    echo $dir
    tar czf $dir.tar.gz $dir
done
Sorry for the bug
 
Old 09-06-2005, 10:33 PM   #10
br8kwall
LQ Newbie
 
Registered: Aug 2005
Location: Youngstown, NY
Distribution: Debian
Posts: 15

Original Poster
Rep: Reputation: 0
YES!!!!!!!!!!! That's it. I've been trying to do this for weeks. Thank you so much!

[jcz@actinella ~]$ ./zipdir.sh
cdcatalogs
ddclient-3.6.6
Desktop

[jcz@actinella ~]$ ls -lht *.gz
-rw-rw-r-- 1 jcz jcz 17M Sep 6 23:31 Desktop.tar.gz
-rw-rw-r-- 1 jcz jcz 13K Sep 6 23:31 cdcatalogs.tar.gz
-rw-rw-r-- 1 jcz jcz 74K Sep 6 23:31 ddclient-3.6.6.tar.gz
 
Old 09-06-2005, 10:37 PM   #11
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
I'm glad that worked for you. Out of curiousity, what kind of cd cataloging software do you use? And do you use it for audio or data?
 
Old 09-06-2005, 10:42 PM   #12
br8kwall
LQ Newbie
 
Registered: Aug 2005
Location: Youngstown, NY
Distribution: Debian
Posts: 15

Original Poster
Rep: Reputation: 0
I needed something so that I could keep track of all the backup CDs I make. So using my clearly feeble scripting abilities I whipped this together. Lotsa room for improvement, but it's been working for me.

#!/bin/sh
# jcz 2004-jan-12
clear
# assumes iso9660 CD-ROM
# automount enabled so this is commnented out
# mount -t iso9660 -r /dev/hdc /hdc

echo "Run this program, then grep keywords in the "
echo "cdcatalogs directory to find which CD-ROM some file "
echo "is on. For example, the following command will find "
echo "palm, is not case sensitive, and includes the line"
echo "number of the log file it finds the word in."
echo ""
echo "grep -in 'Palm' cdcatalogs/* "
echo ""
echo " ----------------------------------------------"

# makes the directory to store the catalog files
mkdir ./cdcatalogs

# runs volname (part of the eject program) to extract the volume label information
cd=$(volname)

# enter user defined CD label (something written on the CD itself)
echo -n "Enter written CD-ROM label and any notes from the disc itself:
"
read labler


# trims white space after the name always written out by volname
cdshort=$(echo $cd | sed -e 's/[ntr ]*$//')
echo $cdshort
echo $cdshort"_catalog.txt"
disk=$cdshort"_catalog.txt"
echo $disk
echo $labler
echo "Disk Volume Label: "$cdshort > $disk
echo "Label and Notes on Disc: " $labler >> $disk
echo " ------------------------------------------" >> $disk
echo "-------- <<<<END DISC ENTRY>>> ------------" >> $disk
echo " ------------------------------------------" >> $disk
echo " " >> $disk

# keeps only relevant columns from ls, and date is in a fixed length format which is understandable
# by M$ Office products if needed
ls -ghGR --full-time /media/cdrecorder | awk '{ print $1 "\t" $3 "\t"$4 " " $5 "\t"$7 $8 $9 $10 $11 }'>> $disk

# fixes the line endings for windows if you want read the catalogs
# directly in Notepad
unix2dos $disk

# moves file to consistent directory
mv $disk ./cdcatalogs/

ls -lht ./cdcatalogs/

# not needed
# umount /hdc
# ejects the disk when done to prepare for next disk
eject
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM
Solaris 8 gzip issues- cannot gzip -d lynx sixosix Solaris / OpenSolaris 4 03-13-2005 03:17 PM
gzip a directory into a file tcma Linux - Software 4 10-07-2004 02:51 AM
Script to run script in multiple directories... seabass55 Linux - General 5 07-06-2004 12:44 AM


All times are GMT -5. The time now is 10:15 AM.

Main Menu
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