[英]move only if file exists in a shell script
As part of a backup script I want to call mv
on a file to rename it:作为备份脚本的一部分,我想对文件调用mv
以重命名它:
mv example.txt example2.txt
If the file doesn't exist, I am receiving the error:如果文件不存在,我会收到错误消息:
mv: cannot stat ‘example.txt’: No such file or directory
How do I call the mv only if the file already exists?仅当文件已存在时如何调用 mv?
I don't really want to redirect stderr to dev/null
as I'd quite like to keep any other errors that occur.我真的不想将stderr重定向到dev/null
因为我很想保留发生的任何其他错误。
即使找不到文件,这一行也会成功返回:
[ ! -f src ] || mv src dest
一内胆:
[ -f old ] && mv old nu
You should test if the file exists 您应该测试该文件是否存在
if [ -f blah ]; then
mv blah destination
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.