[英]Append file name with source folder using the FIND command
我需要從許多具有相同文件名 a.txt 的目錄中刪除文件。 區別來自於父文件夾所以
示例 1\a.txt 示例 2\a.txt...
所以我希望運行一個 FIND 命令,該命令將捕獲 a.txt 但不會在文件從一個文件夾移動到另一個文件夾時覆蓋該文件。 所以 output 將是
example1_a.txt example2_a.txt
所以在另一篇文章中,我想要的 FIND 命令如下
find . -name "a.txt" -execdir echo cp -v {} /path/to/dest/ \;
所以我想以某種方式修改 append 源文件夾到文件。 所以我的猜測是以某種方式操縱 {} 來做到這一點。
提前致謝
一個班輪可能是可能的,但你可以使用這個:
#!/bin/bash
targetprefix="targetdir"
find . -name "a.txt" -print0 | while read -r -d '' line
do
path=$(dirname "$line")
newpath=$(echo "${path#./}" | tr '/' '_')
target="$targetprefix/$newpath"
filename=$(basename "$line")
cp -v $line $target/$filename
done
find
與-print0
while
來自https://mywiki.wooledge.org/BashFAQ/001find
的結果全部以“./”開頭,因此我使用"${path#./}"
刪除該前綴。tr
用下划線替換所有后續的“/”。 這將處理子目錄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.