I am looking for a shell script which move .300 extension files from /home/sofia/ to /home/sofia/test and sends me an email with the list of files moved like this :-
The following files has been moved from /home/sofia/ to /home/sofia/test
1. alpha.300
2. beta.300
Email only if the file exists and moved and also sends an error message if some reason it cannot move(errors).Please help.Appreciate your help.
---------- Post updated at 01:49 PM ---------- Previous update was at 01:25 PM ----------
#!/usr/bin/env bash
FROM_DIR='/home/zaree'
TO_DIR='/home/zaree/test'
FILE_FOUND=0
BODY=$(printf "$(date)\n\n")
BODY+=$(printf "The following files have been moved from\n")
BODY+=$(printf "%s\nto\n%s\n\n" "$FROM_DIR" "$TO_DIR")
for FILE in /home/zaree/*.beta; do
FILE_FOUND=1
mv $FILE /home/zaree/test
BODY+=$(printf "%s moved\n" "$FILE");
done
{
if (( $FILE_FOUND==1 )); then
printf "$BODY"
fi
} | mailx -s "Dev-Script" zaree@xyz.com
Hi Sofia and welcome to the forums.
For future posts, please use the code tags (the icon with the letters code) for multiline code blocks, as you agreed by the forum rules.
How do the files look like? - What is their name? {alpha,beta}.300 or 300.{alpha,beta} ?
when i run the command i donot get the format i wanted.
I am looking for this format
The following files has been moved from /home/zaree to /home/zaree/test
1. a.beta
2. b.beta
sea-->the file are in /home/zaree directory like this /home/zaree/a.beta
---------- Post updated at 02:13 PM ---------- Previous update was at 02:07 PM ----------
i tried another code which worked on one server but other -E flag erroring out.
#!/usr/bin/env bash
#using "shopt -s nullglob" so that an empty directory won't give you a literal '*'
shopt -s nullglob #to make `("$src_dir"*.271)` works
src_dir="/home/zaree/ "
dest_dir="/home/zaree/test/"
#manually remove both /tmp/error.txt and /tmp/moved.log which will append from time to time.
err_f="/tmp/error.txt"
mv_f="/tmp/moved.log" #record moved file in case network down
email="zaree@xyz.com"
export replyto="<noreply@xyz.com>"
touch "$err_f" #bcoz we use >> apppend
touch "$mv_f" #bcoz we use tee -a append
#mailx 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.
if [ ! -d "$src_dir" ]; then echo|mailx -s "Error: directory $src_dir not exist" "$email" 2>>"$err_f"; exit 1; fi
if [ ! -d "$dest_dir" ]; then echo|mailx -s "Error: directory $dest_dir not exist" "$email" 2>>"$err_f"; exit 1; fi
{
f=("$src_dir"*.beta)
for ((i=0; i < ${#f[@]}; i+=1)); do
mv -f "${f}" "$dest_dir" 2>>"$err_f"; #-f do not prompt
#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.
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}")" ; echo;
else
echo| mailx -s "Error: $(<"$err_f")" "$email" 2>>"$err_f"; break
fi
done
#mailx -E to avoid empty body, so it wouldn't sent if no file moved
} | tee -a "$mv_f" | mailx -E -s "File move Script" "$email" 2>>"$err_f"
---------- Post updated at 22:08 ---------- Previous update was at 21:21 ----------
So it is about catching multiple items of with beta extension, and why did you name it 300 extension?
As already been mentioned by wport, make the name-sheme of the actual files, and what is used in the code, the same.
How about:
#!/usr/bin/env bash
#
# Variables
#
shopt -s nullglob #to make `("$src_dir"*.271)` works
# Dirs
src_dir="/home/zaree/ "
dest_dir="/home/zaree/test/"
# Files
err_f="/tmp/error.txt"
mv_f="/tmp/moved.log" #record moved file in case network down
# email
email="zaree@xyz.com"
replyto="<noreply@xyz.com>"
BODY=""
date_string="$(date +'%Y-%m-%d %H:%M:%S')"
# bools
dont_move=false
#
# Preparations
#
touch "$err_f" "$mv_f"
if [ ! -d "$src_dir" ] || [ ! -d "$dest_dir" ]
then BODY="Error: One or both directory $src_dir/$dest_dir does not exist"
echo "$BODY" | tee -a "$err_f"
dont_move=true
BODY+"\n"
#exit 1
fi
#
# Get files
#
if ! $dont_move
then files_array=("$src_dir"*.beta)
for thisFile in "${files_array[@]}"
do
mv -f "thisFile" "$dest_dir" 2>>"$err_f"
if [ 0 -eq $? ]
then msg="Successfully moved"
else msg="Failed to move"
fi
msg="$msg $thisFile"
echo "$msg" | tee -f $mv_f
BODY+="$msg\n"
done
fi
#
# Send the mail
#
echo| mailx -s "$BODY" "$email" 2>>"$err_f"