linux - 如何使用 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 。文本
- 任务是将特定文件从文件夹路径复制到备份路径。
总结一下:
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
推荐阅读
- c# - Asp.net core MVC 在路由中使动作可选
- openvino - 未找到 OpenVino 推理引擎 C API
- woocommerce - 如何在 WooCommerce REST API 中按 date_modified 过滤订单?
- sql - 计数累计不同
- azure-active-directory - Azure AD 用户预配自定义属性
- seo - 谷歌元描述使用
- r - Mac上的Rcplex无法编译
- python - 使用 LBP、深度学习和 OpenCV 进行实时人脸识别
- python - 如何为我的注册表单和模型用户实施合适的登录表单?
- c++ - 我正在尝试在 Vscode 上执行此代码,但每当我运行此代码时,它不会获取字符的值,而只是显示一个随机数