Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Script to move .300 extension file from 1 dir to other and email

$
0
0
Hi,

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 :-

Code:

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 ----------

Code:

#!/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

Moderator's Comments:
Please use CODE tags for sample input, output, and code segments.

Viewing all articles
Browse latest Browse all 16232

Trending Articles