Pretty new to shell script,want to write script which checks if any file .300 extension exists on /exports/files on  server 10.xx.xx.xx.  If *.300 exists, move file to /exports/files/arch and send an email to support@abc.com.  If *.300 isn't found then do nothing.I tried below suggestion:#! /bin/bash    for FILE in `ls /exports/files/*.300`    do       mv $FILE /exports/files/arch       echo $FILE moved | mailx -s $FILE moved support@abc.com     doneWhen there is no files its erroring out saying ls: cannot access /exports/files/*.300: No such file or directory.  Is there any way to suppress this...
Script to check if file exists and copy
linux;bash;shell script
[UPDATE]#!/usr/bin/env bashshopt -s nullglob #to make `($src_dir*.300)` workssrc_dir=/exports/files/ #don't forget trailing slash /dest_dir=/exports/files/arch/ #don't forget trailing slash /err_f=/tmp/error.txtmv_f=/tmp/moved.log #record moved file in case network downemail=support@abc.comtouch $err_f #bcoz we use >> apppendtouch $mv_f #bcoz we use tee -a appendif [ ! -d $src_dir ]; then echo|mailx -s Error: directory $src_dir not exist $email 2>>$err_f; exit 1; fiif [ ! -d $dest_dir ]; then echo|mailx -s Error: directory $dest_dir not exist $email 2>>$err_f; exit 1; fi{f=($src_dir*.300)for ((i=0; i < ${#f[@]}; i+=1)); do        mv -f ${f[i]} $dest_dir  2>>$err_f; #-f do not prompt        if [ $? -eq 0 ]; then                if [ $i -eq 0 ]; then echo $(date +%Y-%m-%d %H:%M:%S); echo The following files has been moved from $src_dir to $dest_dir; echo; fi                echo $((i+1)). $(basename ${f[i]}) 'moved'; echo;        else                 echo| mailx -s Error:  $(<$err_f) $email 2>>$err_f; break        fidone} | tee -a $mv_f | mailx -E -s The following files has been moved $email 2>>$err_fExample mail inbox screeshoot:Received mail when mv error:Received mail when mv success:/tmp/moved.log to log files moved and /tmp/error.txt to log all erros, both files used to keep log even network down:Note:You shouldn't parse the output oflsmailx -E to avoid empty body, so it wouldn't sent if no file movedmailx only one if mv encounter error, by using break to break early instead of exit 1 because we still want to know which file has been moved before error occur.Advantage of cmd; if [ $? -eq 0 ] compare to implicitly if cmd is you can easily modify it to some other command which might require different return code.You should manually remove both /tmp/error.txt and /tmp/moved.log which will append from time to time.Reference: http://mywiki.wooledge.org/ParsingLshttp://lcorg.blogspot.com/2009/04/tip-redirecting-multiple-command.htmlhttps://serverfault.com/questions/349989/sending-mail-from-command-line-if-body-not-emptyhttps://stackoverflow.com/questions/16036763/get-formatted-date-from-timestamp-with-rounded-milliseconds-bash-shell-script