[英]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.