[英]Find and copy files contained in pathnames in a txt file
我們有一個path.txt文件,其中包含文件夾中特定文件的路徑名。 asd/fold2/some.txt
asd/fold2/some/tr6/ok.txtasd/fold2/some.txt
並且我們需要將path.txt中列出的所有文件復制到一個新文件夾中。 我的問題不是如何復制或移動這些文件,而是我能如何准確地找到搜索並最終獲得移動或復制它們的方法。
asd/fold2/some/tr6/ok.txt
您可以使用xargs
的-a
和-I
選項來執行此操作:
xargs -a files.txt -I __FILENAME__ cp __FILENAME__ dest
說明:
-a
:從文件而不是標准輸入中讀取項目。 -I
:將初始參數中出現的replace-str(在本例中為__FILENAME__
)替換為從標准輸入中讀取的名稱。 你想要那個 :
#!/bin/sh
# usage : script.sh paths.txt destination
[ -f $1 ] || exit
[ -d $2 ] || exit
while read file
do
cp $file $2
echo "cp $file $2"
done < $1
無需檢查重復的名稱,它可以很簡單
cp $(<paths.txt) new_folder/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.