Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move or copy a file if that file exists?

Tags:

linux

mv

centos

I am trying to run a command

mv /var/www/my_folder/reports.html /tmp/

it is running properly. But I want to put a condition like if that file exists then only run the command. Is there anything like that?

I can put a shell file instead. for shell a tried below thing

if [ -e /var/www/my_folder/reports.html ]
  then
  mv /var/www/my_folder/reports.html /tmp/
fi

But I need a command. Can some one help me with this?

like image 230
Maharjun M Avatar asked Nov 07 '17 09:11

Maharjun M


1 Answers

Moving the file /var/www/my_folder/reports.html only if it exists and regular file:

[ -f "/var/www/my_folder/reports.html" ] && mv "/var/www/my_folder/reports.html" /tmp/
  • -f - returns true value if file exists and regular file
like image 119
RomanPerekhrest Avatar answered Oct 05 '22 04:10

RomanPerekhrest