![](/img/trans.png)
[英]Bash script find and copy files with multiple extensions including files with 2 or 3 extensions
[英]Script to find and copy files
我想編寫一個腳本,查找文件列表並將其復制到另一個目錄:
for file in `cat ~/fileNames.txt`; do cp $(find $PWD -name $file) $TARGET_DIR; done
打印$file
可以按預期工作,但是在以下用於調試的腳本中,find不會返回任何輸出:
for file in `cat ~/fileNames.txt`; do echo $file; echo $(find $PWD -name $file); done
我知道文件位於$ PWD下的子目錄之一中
更新:
使用-exec選項僅復制列表中的最后一個文件。
for file in `cat ~/fileNames.txt`; do find $PWD -name "$file" -exec cp -f {} $TARGET_DIR \;; done
使用while循環似乎並沒有做任何不同的事情:
cat ~/fileNames.txt | while read file; do echo $file; find $PWD -name "$file" -exec cp -f {} $TARGET_DIR \;; done
有任何想法嗎?
試試這個腳本:
while read -r file; do
find . -name "$file"
done < ~/fileNames.txt
並復制文件:
while read -r file; do
find . -name "$file" -exec cp {} "$TARGET_DIR" \;
done < ~/fileNames.txt
永遠不要這樣做 :
for file in `cat ~/fileNames.txt`; do
它受制於單詞拆分和路徑名擴展。 相反,您將需要使用while
循環讀取文件:
while IFS= read -r file; do
# stuff
done < ~/fileNames.txt
然后,可以使用find
及其-exec
操作:
find . -name "$file" -exec cp {} "$target_dir" \;
這樣最后:
while IFS= read -r file; do
find . -name "$file" -exec cp {} "$target_dir" \;
done < ~/fileNames.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.