Linux下复制目录结构

yanchao90 发布于 2013/12/18 18:30
阅读 1K+
收藏 0

根据当前项目复制个一样的目录结构,新目录结构中所有文件都是空的

大概是这个意思
ls -R xxxxx | touch xxxxxx

这命令该咋写?

加载中
0
杨同学
杨同学

把aaa目录结构拷贝到bbb

find aaa -type d | {
    while read dir_path
    do
        full_path=bbb/$dir_path
        if [ ! -d "$full_path" ]; then
            mkdir -p "$full_path"
        fi
    done
}
木有测试过,撸主试试看
0
LianyouCQ
LianyouCQ

才学shell,练习了一下,还能达到效果

不过,sourceDir和destDir要写全路径,不然会陷入死循环。自己修改一下就可以修复了。

#!/bin/bash

USAGES="Usages: `basename $0` sourceDir destDir"

# check the count of the arguments
if [ $# -ne 2 ]; then
    echo -e "\a"
    echo $USAGES
    exit 1
fi

# check the sourceDir 
cd $1 || {
    echo -e "\a"
    echo "$1 doesn't exist"
    echo $USAGES
    exit 1;
}

# check the destDir
if [ -d "$2" ]; then
    echo -e "\a"
    echo "$2 is already existed"
    echo $USAGES
    exit 1;
fi

DestPrefix=$2

mkdir $2

if [ $? -ne 0 ]; then
    echo -e "\a"
    echo "mkdir $2 is failed, Please check the premissions and so on."
fi

# invoked Reversively
function robot(){
    echo "Step into robot function, and \$1 is $1 " 
    if [ -f $1 ]; then
            touch $DestPrefix/$1
            # echo "Touch $1 is done"
    elif [ -d $1 ]; then
        cd $1
        DEST=$DestPrefix/$1
        # echo "the status of cd $1 is $?"
        if [ $? -eq 0 ]; then
            mkdir -p $DEST
            for subfile in `find . -maxdepth 1 -type f`; do
                touch $DEST/$subfile
            done

            # echo "$DestPrefix/$1 is created"
            for subdir in `find . -maxdepth 1 -type d`; do
                if [ $subdir != "." ];then
                    mkdir -p $DEST/$subdir
                    robot $subdir
                fi
            done
        cd $OLDPWD
        fi
    fi
}

for  i in  `ls` ; do
    echo $i
    robot $i
done



返回顶部
顶部