给定文件列表,按目录结构拷贝到新目录中

 

 

复制代码
#!/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

 

posted @   LiuYanYGZ  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2022-09-06 CentOS创建永久网桥
2022-09-06 cherry MX 3850键盘红轴
2018-09-06 CentOS 7 中firewall-cmd命令
2017-09-06 Linux系统VNC配置实践总结
2016-09-06 今日成长笔记2016-09-05
点击右上角即可分享
微信分享提示