简体   繁体   English

在 bash 中使用通配符移动和备份多个目录

[英]move and backup multiple directories with wildcards in bash

The goal is to move multiple directories including its content to another directory while making in parallel a backup.目标是将包括其内容在内的多个目录移动到另一个目录,同时进行并行备份。

Input="/home/input/folder*/"
Output="/home/output/"
Backup="/home/backup/"

for folder in "$Input" ; do
    cp -rp "$folder" "$Backup" || { echo 'error backup'; exit; }
    cp -rp "$folder" "$Output" || { echo 'error move'; exit; }
    rm -rf "$folder"
done

I get following error message: cp: cannot stat '/home/input/folder*/': file or folder not found我收到以下错误消息: cp: cannot stat '/home/input/folder*/': file or folder not found

Two questions:两个问题:

  • Why does the wildcard not work?为什么通配符不起作用?
  • Is there a more elegant way to do this task?有没有更优雅的方式来完成这项任务?

Compiling all your comments into an answer:将您的所有评论编译成答案:

Input="/home/input/"folder*/
Output="/home/output/"
Backup="/home/backup/"

shopt -s nullglob

for folder in $Input ; do
    cp -rp "$folder" "$Backup" || { echo 'error backup'; exit; }
    cp -rp "$folder" "$Output" || { echo 'error move'; exit; }
    rm -rf "$folder"           || { echo 'error remove'; exit; }
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM