[英]how to find and copy files in a sub directory from parent directory linux
我有几个父文件夹,如GJ1,GJ2等。这些文件夹中的每一个都包含三个图像,如GJ11_F.jpg ,GJ11_P.jpg。 我只需要显示所有GJ11_F.jpg文件,包括它们各自的父目录。
find . -type f -name "*_F.jpg" | xargs cp -t ~/home/ubuntu/
但是上面的命令将仅复制* _F.jpg文件,而不复制它们各自的父目录GJ1。
xargs不是我应该尝试的一个吗?
我也尝试过-
find . -name "*_F.jpg" -exec sh -c 'rsync -a "${0%/*}" ~/home/ubuntu/' {} \;
一种简单的方法是使用tar
,它将自动处理目录:
find . -type f -name "*_F.jpg" -print0 | tar c --null -T - | tar xC ~/home/ubuntu/
这是一个使用while
循环的解决方案:
find . -type f -name "*_F.jpg" -print0 |
while IFS= read -r -d '' file; do
mkdir -p ~/home/ubuntu/"$(dirname -- "$file")"
cp -ai -- "$file" ~/home/ubuntu/"$file"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.