簡體   English   中英

如何使用 bash 將多個版本號不同的文件從一個目錄復制到另一個目錄?

[英]How to copy multiple files with varying version numbers from one directory to another using bash?

我有一個文件夾/home/user/Document/filepath ,其中有三個文件,即 file1-1.1.0.txt、file2-1.1.1.txt、file3-1.1.2.txt

和另一個名為/home/user/Document/backuppath的文件夾,我必須從/home/user/Document/folderpath移動文件,其中包含 file1-1.0.0.txt、file2-1.0.1.txt 和 file3-1.0.2 。TXT

  1. 任務是將特定文件從文件夾路徑復制到備份路徑。

總結一下:

the below is the files.txt where I listed the files which has to be copied:

file1-*.txt
file2-*.txt
The below is the move.sh script that execute the movements
for file in `cat files.txt`; do cp "/home/user/Document/folderpath/$file" "/home/user/Documents/backuppath/" ; done

對於上面的腳本,我收到類似的錯誤

cp: cannot stat '/home/user/Document/folderpath/file1-*.txt': No such file or directory found
cp: cannot stat '/home/user/Document/folderpath/file2-*.txt': No such file or directory found

我想完成的是我想使用腳本復制特定文件,使用 * 代替版本號。,因為版本號將來可能會有所不同。

您的 files.txt 中有通配符。 在您的cp命令中,您使用的是引號。 這些引號阻止了通配符的擴展,您可以從錯誤消息中清楚地看到這一點。

一種明顯的可能性是不使用引號:

 cp /home/user/Document/folderpath/$file /home/user/Documents/backuppath/

或者根本不使用循環:

 cp $(<files.txt) /home/user/Documents/backuppath/

但是,如果files.txt中的一行是包含空格的文件名模式,這當然會中斷。 因此,我建議在擴展模式上進行第二個循環:

while read file # Puts the next line into 'file'
do
  for f in $file # This expands the pattern in 'file'
  do
     cp "/home/user/Document/folderpath/$f" /home/user/Documents/backuppath
  done
done < files.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM