如何在 shell 脚本中使用 ftp 删除 3 天前的文件 (Solaris OS)

发布于 2024-08-19 22:44:55 字数 130 浏览 5 评论 0原文

我正在编写一个脚本来创建文件,基本上是一些表的副本,并将这些文件通过 ftp 传输到远程计算机。在转储这些文件之前,还需要删除远程计算机上 3 天的旧文件。

我需要帮助编写 ksh 以使用 ftp 删除远程计算机上 3 天前的文件

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2024-08-26 22:44:55

通常,您会使用:

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

或类似的东西(即,可能有其他限制子句,例如用于常规文件的 -type f 或仅执行当前目录的 -maxdepth 0 ,没有子目录)。 -mtime +3 仅获取修改日期为 3 天或以上的文件。

在您的系统上执行 man find 以获取完整详细信息。我不知道Solaris 是否具有与GNU 相同的功能。它可能更有限(或更好)。


更新:请以您崇拜的任何神的名义,首先使用 echo 而不是 rm 测试该命令。如果您相信“网络上某个随机的人可能会或可能不会考虑您的最大利益”的建议,我对您的文件的破坏不承担任何责任:-)


并且,在有人跳出来斥责我不使用之前xargs (或者,更好的是,find -print0xargs -0 如果可用),我知道。但这与当前的具体问题无关。如果以及何时 find -exec 的性能出现问题,OP 可以询问另一个问题。


如果您有一个包含日期的特定文件格式(如您在评论中指出的那样),您实际上可以在 ftp 下使用 mdel。考虑以下脚本:

# 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

除了可能会在月份边界上留下最多六天的文件的轻微烦恼之外,这可以满足您的需要。如果这真的很重要,您当然可以使处理月份边界的代码更加智能。

只需每天在您的盒子上运行此脚本,它就会使用标准 ftp 工具清除目标盒子上的文件。我仍然认为在服务器上运行 find 更容易,但如果该途径不可用,我将提供此选项。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文