[英]Copy files between folders on linux
I have to copy many files from different folders. 我必须从不同的文件夹中复制许多文件。
Example of the source Folder structure 源文件夹结构的示例
Source folder has the following structure 源文件夹具有以下结构
root
- folder1
- folder1.txt
- folder2
- folder2.csv
- folder3
- folder3.txt
Example of the destination Folder structure 目标文件夹结构的示例
Destination folder should be like this following structure 目标文件夹应该类似于以下结构
root
- folder1
- folder1.txt
- folder3
- folder3.txt
To accomplish the generic copy and recreating the folder structure I have used the following script: 为了完成通用副本并重新创建文件夹结构,我使用了以下脚本:
cp src/**/*.txt dest/
for file in $(ls *.txt);
do mkdir -p source/${file%.*}/ && mv $file dest/${file%.*}/;
done
First of all I copy all the file in the destination folder. 首先,我复制目标文件夹中的所有文件。 Based on the assumption that every file is inside a folder which has the same name then I am moving the files recreating the original structure.
基于每个文件都在同名文件夹内的假设,我移动文件重新创建原始结构。 This script effectively works very well.
这个脚本非常有效。
Now the requirement has changed to support multiple level folder structure. 现在需求已更改为支持多级文件夹结构。 Eg
例如
root
- folder1
- folder11
- folder11.txt
- folder2
- folder2.csv
- folder3
- folder3.txt
How can I adapt the script to remain generic? 如何调整脚本以保持通用?
This might work for you: 这可能对你有用:
#!/usr/bin/env bash
shopt -s globstar
src=some/src/path
dest=some/dest/path
for f in "$src"/**/*.txt; do
d=${f#"$src"} d=$dest/${d%/*}
mkdir -p -- "$d" || continue
cp -- "$f" "$d"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.