Unix – how to delete 3 days old files using ftp in a shell script (Solaris OS)

ftpshellsolarisunix

I am working on a script to create files basically replica of some tables and ftp those files to the remote machine.there is one more requirement to delete the 3days old files on the remote machine before dumping these files.

I need help in writing the ksh for deleting 3 days old files on a remote machine using ftp

Best Answer

Normally, you would use:

find . -mtime +3 -exec rm {} ';'

or something similar (i.e., there may be other limiting clauses like -type f for regular files or -maxdepth 0 to do current directory only, no subdirectories). The -mtime +3 only gets those files whose modification date is 3 days age or more.

Execute man find on your system for full details. Whether Solaris has the same features as GNU find I don't know. It may be more limited (or better).


Update: Please, in the name of whatever gods you worship, please test the command first with echo instead of rm. I take no responsibility for the destruction of your files if you trust the advice of "some random guy on the net who may or may not have your best interests at heart" :-)


And, before anyone jumps in and berates me for not using xargs (or, better yet, find -print0 with xargs -0 where available), I know. But it's not relevant to the specific question at hand. The OP can ask another question if and when the performance of the find -exec is a problem.


If you have a specific file format with the date in it (as you indicate in your comment), you can actually use mdel under ftp. Consider the following script:

# The prefix and suffix of files to delete.
prefix='*_'
suffix='-i.tbl'

# Create FTP script file.
rm -rf temp.ftp
echo "user pax pax_password" >>temp.ftp
echo "cd /my/directory" >>temp.ftp
echo "prompt" >>temp.ftp

# Get current date.
y=$(date +%Y)
m=$(date +%m)
d=$(date +%d)
((lasty = y - 1))
((lastm = m - 1))

# If past Jan 3, delete all of previous year.
if [[ $m -gt 1 || $d -gt 3 ]] ; then
    echo "mdel ${prefix}${lasty}????${suffix}" >>temp.ftp
fi

# If past Jan and past the third, delete all of previous month.
if [[ $m -gt 1 && $d -gt 3 ]] ; then
    if [[ ${lastm} -lt 10 ]] ; then
        echo "mdel ${prefix}${y}0${lastm}??${suffix}" >>temp.ftp
    else
        echo "mdel ${prefix}${y}${lastm}??${suffix}" >>temp.ftp
    fi
fi

# If past the third, delete current month more than three days old.
if [[ $d -gt 3 ]] ; then
    ((d = d - 3))
    if [[ ${m} -lt 10 ]] ; then
        m="0${m}"
    fi
    while [[ ${d} -gt 0 ]] ; do
        if [[ ${d} -lt 10 ]] ; then
            echo "mdel ${prefix}${y}${m}0${d}${suffix}" >>temp.ftp
        else
            echo "mdel ${prefix}${y}${m}${d}${suffix}" >>temp.ftp
        fi
        ((d = d - 1))
    done
fi

# Finalise script and run it.
echo "bye" >>temp.ftp
ftp -n mymachine.com <temp.ftp
rm -rf temp.ftp

Other than a slight annoyance where you may have up to six days of files left there on month boundaries, this does what you need. You could of course make the code handling the month boundaries a little more intelligent if that's really important.

Just run this script on your box each day and it will clear out files on the target box by using standard ftp tooling. I still think it's easier to run a find on the server box but I'll present this option if that avenue is not available.