【问题标题】:Rename file while using SFTP to append dateSFTP 重命名文件以附加日期
【发布时间】:2023-03-14 13:48:01
【问题描述】:

我正在使用 sftp 从大型机主机向 linux ftp 服务器发送文件。

一旦文件驻留在 linux 机器上,我想将日期附加到文件名。 (例如:filename.txt 变成 filename122308.txt)

我已经尝试使用 'date +%m%d%y' 的 'rename' 命令 - 文件已重命名但未执行标志(文件名变为 filename'date +%m%d%y'.txt

“cp”和“mv”命令不起作用...有什么想法吗?

谢谢。

【问题讨论】:

    标签: linux ssh sftp mainframe


    【解决方案1】:

    命令正在通过 JCL 控制卡发送。我认为这种方法行不通。

    【讨论】:

    • 如何在 JCL 中提交命令?您是否通过 BPX* 命令从 JCL 执行 shell 命令?
    【解决方案2】:

    由于 sftp 没有运行 shell,所以没有什么可以执行 date 命令。您可能必须在发送方评估您想要的新名称,然后执行 sftp 重命名。

    另一种选择是将文件发送到队列区域(例如带有您的日期字符串的文件夹),并在 linux 框上设置一个脚本相应地移动/重命名接收到的文件。

    【讨论】:

      【解决方案3】:

      你可以通过命令行来做吗?存在执行 sftp 的选项...

      sftp [[user@]host[:file [file]]]
      

      ...所以你可以执行...

      export WHEN=`date +%m%d%y`
      sftp theUser@theHost:filename$WHEN.txt filename.txt <<-!
      thePassword
      !
      

      【讨论】:

      • 命令正在通过JCL控制卡发送。我认为这种方法行不通。
      【解决方案4】:

      您可以访问 Linux 服务器吗?在这种情况下,您可以在那里重命名文件。例如,您可以使用 inotify 来监视目录,然后使用脚本在该目录中创建新文件时将日期添加到文件中。

      Here's Python 中的一个简单示例(尽管大多数语言都有 inotify 绑定)。您要监听的事件是 IN_CREATE。

      【讨论】:

        【解决方案5】:
        #Establish a local variable to store the LOGINID to be used
        export USERID=xxxxx                                                  
        #Establish a local variable to store the Path/file location on Remote machine
        export REMOTEPATH=/some/path/    
        #Establish a local variable to store the NAME of the remote Server
        export TARGET=192.168.0.xx
        #Establish a local variable to store the new component of the file name (in
        #this case, a modification of Date)
        export WHEN=`date +%m%d%y`                                                  
        #Demonstrate that the USERID variable is set properly by echoing it out to the
        #StandardOut
        echo "User "$USERID                                               
        #Demonstrate that the TARGET variable is set properly by echoing it out to the
        #StandardOut
        echo "Target Server: "$TARGET                              
        #Demonstrate that the REMOTEPATH / server variable is set properly by echoing
        #it out to the StandardOut
        echo "Target Path "$REMOTEPATH                                    
        #Demonstrate that the WHEN / date name modication variable is set properly by
        #echoing it out to the StandardOut
        echo "Date component of file "$WHEN                                  
        #Just echo out that we're about to do something useful
        echo "Sending file to REMOTE"                                        
        #Simulate the user typing out a command by using the "echo" command.  We use
        #the $REMOTEPATH and $WHEN variables to modify "what the user types" but when
        #we're done, echo passes information into SFTP just like the user were sitting
        #there typing in the #commands.  In this case, it should send in 
        #"put /local/path/file /some/path/fileName09092009.txt"
        #That is provided as the command list to sftp because of the single "-" that 
        #says "get my list of commands from standard-input"  The -vvv is for verbose 
        #(lots of diagnostics) and then the $USERID@$TARGET uses the USERID and TARGET
        #variables to connect as user-at-server for the exchange.
        #A passwordless SSH authentication is already in place, so no password 
        #is needed.
        echo "put  /local/path/file $REMOTEPATH/fileName$WHEN.txt " | \
            sftp -b - -vvv $USERID@$TARGET
        #Just echo out that we're about to do the next step and change file
        #permissions.
        echo "Changing file Permissions on REMOTE"                
        echo "Done"                                          
        

        【讨论】:

          【解决方案6】:

          不确定您的大型机 SFTP 客户端,但许多 SFTP 客户端支持使用 !运行本地操作系统命令的前缀。因此您可以在发送前将文件复制到新名称,然后发送,然后删除副本。

          例如:

          !cp filename.txt filename122308.txt
          put filename122308.txt
          !rm filename122308.txt
          exit
          

          如果空间很重要,请使用两次 mv 而不是 cp & rm。

          【讨论】:

            猜你喜欢
            • 2013-07-18
            • 1970-01-01
            • 2020-08-29
            • 2014-09-28
            • 1970-01-01
            • 2019-09-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多