Bash:循环、复制、备份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2458128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:49:37  来源:igfitidea点击:

Bash: for loop, copy, backup

bashloopsbackupfor-loopcp

提问by sixtyfootersdude

Hey, not 100% sure what this error means.

嘿,不是 100% 确定这个错误意味着什么。

% for f in "*" ; do cp $f ../backup/backup$f ; done
cp: ../backup/backup* not found

The purpose is to copy all the files into a folder into a backup folder and rename the files to backup.

目的是将所有文件复制到一个文件夹中,然后将其重命名为备份文件夹。

回答by Mark Byers

The *shouldn't be in quotes:

*不应该在报价:

for f in * ; do cp $f ../backup/$f ; done

When you use quotes this prevents the shell from expanding it, so it is looking for a file called *, not all files in the directory which is what you meant.

当您使用引号时,这会阻止 shell 扩展它,因此它正在寻找一个名为 的文件*,而不是目录中的所有文件,这就是您的意思。

回答by janmoesen

You are quoting the wrong things: quote the variables, not the wildcards!

您引用了错误的内容:引用变量,而不是通配符!

% for f in *; do cp "$f" "../backup/$f" ; done

% for f in *; do cp "$f" "../backup/$f" ; done

BTW, in this case, you can simply do:

顺便说一句,在这种情况下,您可以简单地执行以下操作:

% cp * ../backup/

% cp * ../backup/

回答by sud03r

Or may be this:

或者可能是这样的:

cp -b * ../backup 

If you want them to be renamed:

如果要重命名它们:

% for f in * ; do cp "$f" "../backup/${f}-backup" ; done