如果该文件存在,则移动或复制文件?

Mah*_*n M 10 linux centos mv

我正在尝试运行命令

mv /var/www/my_folder/reports.html /tmp/
Run Code Online (Sandbox Code Playgroud)

它运行正常。但是我想设置一个条件,例如该文件是否存在,然后只运行该命令。有这样的吗?

我可以放一个shell文件。对于外壳,尝试过下面的事情

if [ -e /var/www/my_folder/reports.html ]
  then
  mv /var/www/my_folder/reports.html /tmp/
fi
Run Code Online (Sandbox Code Playgroud)

但我需要一个命令。有人可以帮我弄这个吗?

Rom*_*est 9

/var/www/my_folder/reports.html仅当文件存在且常规文件时才移动文件:

[ -f "/var/www/my_folder/reports.html" ] && mv "/var/www/my_folder/reports.html" /tmp/
Run Code Online (Sandbox Code Playgroud)
  • -f-true如果文件存在和常规文件,则返回值


Dae*_*ark 7

如果存在文件,然后通过标准错误输出移动或回显消息

test -e /var/www/my_folder/reports.html && mv /var/www/my_folder/reports.html /tmp/ || echo "not existing the file" >&2
Run Code Online (Sandbox Code Playgroud)