tags:

views:

31

answers:

2

How could I read the content of a parent folder and if sub-folders are found, then make a tar.gz files of those subfolders found. However, I know that subfolders will have the following filename format: name-1.2.3 What I want is to create a tar.gz file that looks like: name-1.2.3-20100928.tar.gz Any help will be appreciated.

A: 
#!/bin/sh
DATE=`/bin/date +%y%m%e`

cd /path/to/your/folder

for folder in *; do
    if [ -d $folder ]; then
        tar -cvzf $folder-$DATE.tar.gz $folder
    fi
done
mezzie
A: 

Better immunize your scripts against spaces in names:

#!/bin/bash
# Yes nested quoting is allowed with $(...) syntax, no matter whether syntax coloration does
DATE="$(/bin/date "+%y%m%e")"

cd /path/to/your/folder
for folder in * ; do
    if test -d "$folder" ; then
        tar cvzf "$folder-$DATE.tar.gz" "$folder"
    fi
done
Benoit