给定文件列表,按目录结构拷贝到新目录中
  nj1FYstZ6MDA 2023年11月02日 32 0

 

 

#!/bin/bash  
# mycopyTree.sh文件内容如下  
function print_usage()
{
    echo "Usage: ${1} <src_list_file> <dest_dir>"
}

function mycopy_tree()
{
    # 输入源文件列表目录  
    src_list_file=${1}
    # 输入目标目录  
    dest_dir=${2}
  
    # 遍历源文件列表目录中的所有文件  
    for file in $(cat ${src_list_file}); do  
        # 如果是文件而不是目录  
        if [ -f "$file" ]; then  
            # 获取文件名(不包含路径)  
            filename=$(basename "$file")  
            dir_of_file=$(dirname "$file")  
  
            # 创建目标目录结构  
            dest_path="$dest_dir/${dir_of_file}"  
            mkdir -p "$dest_path"  
  
            # 拷贝文件到目标目录  
            cp "$file" "$dest_path"  
        fi  
    done
}

if [ $# -ne 2 ] ; then
    print_usage $0
    exit
fi


mycopy_tree $1 $2

 

my_filelist.txt文件内容如下

/home/a/b/c/d.txt
/home/a/b/c1/d1.txt
/home/a/b/c2/d2.txt
/home/a1/b1/c3/d3.txt

 

 

./mycopyTree.sh    my_filelist.txt   ./store_here

 

拷贝后的目录结构为

./store_here/home/a/b/c/d.txt
./store_here/home/a/b/c1/d1.txt
./store_here/home/a/b/c2/d2.txt
./store_here/home/a1/b1/c3/d3.txt

 



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
nj1FYstZ6MDA