move or copy a file if that file exists? move or copy a file if that file exists? linux linux

move or copy a file if that file exists?


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


if exist file and then move or echo messages through standard error output

test -e /var/www/my_folder/reports.html && mv /var/www/my_folder/reports.html /tmp/ || echo "not existing the file" >&2


You can do it simply in a shell script

#!/bin/bash# Check for the filels /var/www/my_folder/ | grep reports.html > /dev/null# check output of the previous commandif [ $? -eq 0 ]then    # echo -e "Found file"    mv /var/www/my_folder/reports.html /tmp/else    # echo -e "File is not in there"fi

Hope it helps