Script to attach latest files of directories in a mail.

Hello Folks,

I am looking for the script which will go to directory and check for the latest 5 files and send a mail to attaches these files to user.

Kindly guide.

Regards

See last sentence of my post of 4 days ago. Please show some efforts of your own.

1 Like
#!/bin/sh
loc=/test/DPI_INVESTIG/Sadiq/work_area
cd $loc
SUBJECT="TEST for attachment mail"
FILE=`find . | tail -5`
echo "$FILE" |mailx -s "$SUBJECT" -a $FILE abc@gmail.com

I am getting 1 file as attachment, Kinldy guide.

Not sure if you want file names or file contents attached, but try to enclose $FILE in double quotes.

1 Like
cd ABC
ls -ltr | tail -5 >>/ABC/text.txt

/usr/sbin/sendmail -t abcattheratexyz.com < /ABC/text.txt
1 Like

Dear RudiC

i need files to be attached. like 5 files of the directory . thats need to be attached and send to user mail id.

Not sure and can't test:
Try to

  • create a comma separated attachment list
  • use several -a options, one for each of the to be attached files.

A good practice is to use && after cd command.
This will stop your script from executing further if a directory is not accessible by means of permissions, problems with hardware etc.

Take the following example, where 2 bytes of code (&&) handle a dozen of possible error scenarios.

root@machine:~# cat somescript.sh 
#!/bin/sh
loc=/my/files/dumpp # this location does not exist due to human error inputing additional 'p'
cd $loc
printf "%s\n" "my start dir is $PWD which is not expected but i will happily execute relative find.." 
FILE=`find . | tail -5` 
root@machine:~# ./somescript.sh 
./somescript.sh : 3: cd: can't cd to /my/files/dumpp
my start dir is /root which is not expected, but i will happily execute relative find..
root@machine:~# cat handle.sh 
#!/bin/sh
loc=/my/files/dumpp # this location does not exist due to human error inputing additional 'p'
cd $loc &&
printf "%s\n" "my start dir is $PWD which is not expected but i never reached this line" 
FILE=`find . | tail -5` 
root@machine:~# ./handle.sh 
./handle.sh: 3: cd: can't cd to /my/files/dumpp

Hope that helps
Regards
Peasant.