简体   繁体   English

父目录的脚本创建tar,它将具有其子目录的各个tar

[英]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 我想编写一个包含两个参数的脚本:1.父目录的名称(例如mainFolder)2.要创建的tar的名称

The parent directory will have subfolers (folder1,folder2,folder3 etc). 父目录将具有子文件夹(folder1,folder2,folder3等)。 I need a script which will create "mainFolder.tar" which will consists of "folder1.tar","folder2.tar","folder3.tar" ...etc... 我需要一个脚本来创建“ mainFolder.tar”,该脚本将由“ folder1.tar”,“ folder2.tar”,“ folder3.tar” ...等组成。

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.. 这将仅直接创建父级的tar文件...任何人都可以告诉我如何使mainFolder.tar文件创建并包含其子目录的tar文件...需要一个Shell脚本(例如createTar.sh)做..

while creating tar, only "directories" inside the parent folder "mainFolder" should be considered for tar..and not ".txt " files.. 创建tar时,tar ..文件应仅考虑父文件夹“ mainFolder”中的“目录”,而不应考虑“ .txt”文件。

Here is a POSIX compliant solution for your problem. 这是您的问题的POSIX兼容解决方案。 There may be a simpler way (not using any temporary files) but it would involve special and likely non-portable arguments to tar . 可能有一种更简单的方法(不使用任何临时文件),但是它将涉及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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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