简体   繁体   English

如何使用 bash 将多个版本号不同的文件从一个目录复制到另一个目录?

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

I have a folder /home/user/Document/filepath where I have three files namely file1-1.1.0.txt, file2-1.1.1.txt, file3-1.1.2.txt我有一个文件夹/home/user/Document/filepath ,其中有三个文件,即 file1-1.1.0.txt、file2-1.1.1.txt、file3-1.1.2.txt

and another folder named /home/user/Document/backuppath where I have to move files from /home/user/Document/folderpath which has file1-1.0.0.txt, file2-1.0.1.txt and file3-1.0.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. task is to copy the specific files from folder path to backup path.任务是将特定文件从文件夹路径复制到备份路径。

To summarize:总结一下:

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

for the above script I am getting the error like对于上面的脚本,我收到类似的错误

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

what I would like to accomplish is that I would like to use the script to copy specific files using * in the place of version numbers., since the version number may vary in the future.我想完成的是我想使用脚本复制特定文件,使用 * 代替版本号。,因为版本号将来可能会有所不同。

You have wildcard characters in your files.txt.您的 files.txt 中有通配符。 In your cp command, you are using quotes.在您的cp命令中,您使用的是引号。 These quotes prevent the wildcards to be expanded, as you can clearly see from the error message.这些引号阻止了通配符的扩展,您可以从错误消息中清楚地看到这一点。

One obvious possibility is to not use quotes:一种明显的可能性是不使用引号:

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

Or not use a loop at all:或者根本不使用循环:

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

However, this would of course break if one line in your files.txt is a filename pattern which contains white spaces.但是,如果files.txt中的一行是包含空格的文件名模式,这当然会中断。 Therefore, I would recommend a second loop over the expanded pattern:因此,我建议在扩展模式上进行第二个循环:

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.

相关问题 通过bash将文件从一个目录排序并复制到另一个目录 - Sort and copy files from one directory to another via bash 如何在Linux中将具有特定扩展名的文件从一个目录复制到另一个目录 - How to copy files with a particular extension from one directory to another in linux 使用bash脚本将两个最新文件复制到另一个目录 - Copy two most recent files to another directory using bash script 如何使用 C 语言将目录从一个地方复制到另一个地方? - How to copy the directory from one place to another using C language? 如何使用cp从不同的目录复制多个文件? - How to copy multiple files from a different directory using cp? 寻找一种将丢失的文件从一个目录复制到另一个目录的方法 - Looking for a way to copy missing files from one directory to another Bash:从一个目录中的脚本中打开多个具有不同名称的文件 - Bash: opening multiple files with diferent names in script from one directory 使用 bash 命令将文件从子文件夹复制到另一个 - Using bash command to copy files from a subfolder to another 如何将不同的内容文件从一个目录复制到另一个目录? - How do I copy differing content files from one directory to another? 如何将前10个最新文件从一个目录复制到另一个目录? - How to copy the top 10 most recent files from one directory to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM