We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Script to FTP a modified file

Hello,

I am fairly new to shell scripting. I see a lot of examples out there of how to find if a file has been modified within a certain period of time. What I'm looking for help with is a script that will run and I'm thinking check for the last 24 hours but if not just check at runtime to see if an HTML file has been modified.

Then once it determines if the file has changed (I've read this can be done with either checksum or the md5 or md5sum commands) then it will FTP the file. I already have an example of how to connect to an FTP and transfer a file but again since I'm new to shell scripting I'd like some help with this.

On another forum I found an example of checking for if a file has been modified and printing things out. Since this will run in the background of a website I don't need it to print but I'm going to post the code here:

#!/bin/sh
#
#
# MD5FILE-parameter specifies where we want to save our md5print for
# later use.
MD5FILE=/tmp/.md5savefile

# The FILE_TO_CHECK-parameter specifies the file we want to monitor
# changes
FILE_TO_CHECK=/tmp/filetocheck

if [ ! -f $FILE_TO_CHECK ]
then
echo "ERROR Couldnt locate file to check:$FILE_TO_CHECK"
exit 1
fi

echo "Taking a print on $FILE_TO_CHECK with md5sum"
MD5PRINT=`md5sum $FILE_TO_CHECK | cut -d " " -f2`

if [ -z $MD5PRINT ]
then
echo "ERROR Recived an empty MD5PRINT thats not valid, aborting"
exit 1
else
echo "MD5PRINT we got was:$MD5PRINT"
fi

if [ -f $MD5FILE ]
then
echo "Found an old savefile:$MD5FILE we trying to match prints"
OLDMD5PRINT=`cat $MD5FILE`

if [ -z $OLDMD5PRINT ]
then
echo "Got an empty string from the oldfile, aborting"
exit 1
fi

if [ "$OLDMD5PRINT" = "$MD5PRINT" ]
then
echo "New and old md5print are identical, the file hasnt been changed"
else
echo "WARNING the old and new md5print doesnt match, the file has been changed"
fi

fi

echo "Saving to new md5print in logfile:$MD5FILE for later checks"
echo $MD5PRINT > $MD5FILE

if [ $? = 0 ]
then
echo "Wrote to file OK"
else
echo "Writing to file failed...why??"
exit 1
fi

I'm looking to modify this code to fit my needs. Any help would be greatly appreciated.

Thanks

3
Contributors
2
Replies
3 Days
Discussion Span
1 Year Ago
Last Updated
3
Views
PyroPlasm
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This might be more efficient. Te find command will do what you want all in one line.

find . -type f -mtime -1 -exec ls -la {} \;

This command looks for files in the current (.) directory and then runs the ls -la command using the file name as a parameter (puts the file name where the French Braces are). The backslash - semi colon ends the line for the command entered. You could logically replace it with your ftp command using the braces where the file name should be.

rch1231
Veteran Poster
1,040 posts since Sep 2009
Reputation Points: 142
Solved Threads: 154
Skill Endorsements: 12

Have a look at wput.

shibblez
Junior Poster in Training
73 posts since Oct 2010
Reputation Points: 15
Solved Threads: 6
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.5921 seconds using 2.69MB