给定文件列表,按目录结构拷贝到新目录中
#!/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