简体   繁体   中英

Script create tar of a Parent directory which will have individual tar's of its sub directories

I want to write a script which will take two parameters 1. Name of Parent directory (ex. mainFolder) 2. The name of the tar to be created

The parent directory will have subfolers (folder1,folder2,folder3 etc). I need a script which will create "mainFolder.tar" which will consists of "folder1.tar","folder2.tar","folder3.tar" ...etc...

tar cvf mainFolder.jar mainFolder

this will create tar of parent directly "mainFolder" only...Can anybody tell me how can I make mainFolder.tar to create & contain tar's of its sub directories...Need a Shell Script (ex. createTar.sh)which will do it..

while creating tar, only "directories" inside the parent folder "mainFolder" should be considered for tar..and not ".txt " files..

Here is a POSIX compliant solution for your problem. There may be a simpler way (not using any temporary files) but it would involve special and likely non-portable arguments to tar .

#!/bin/sh


startdir="$(pwd)"

mkdir -p parent/horse/frog parent/dog/frog parent/fish/frog

mkdir newparent

(
    cd parent

    for i in *
    do if [ -d "$i" ] 
       then tar -cf "$startdir/newparent/${i}.tar" ${i}
       fi
       if [ -f "$i" ] 
       then cp "$i" "$startdir/newparent/"
       fi
    done
)

tar -cf newparent.tar newparent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM