0

I have a scenario where I am receiving multiple files in a directory and I need to transfer those files received in 60 mins to some other server. So for that I use below command.

{ find -maxdepth 1 -type f -mmin -60| cut -d'/' -f2 | sed 's/^/put /' ; echo bye ; } | \
    /usr/local/bin/sshpass -p password sftp -oBatchMode=no username@xx.xxx.xxx.xxx:/folder

Now, I need to modify the command and make a log file where i need to print the filenames of files being transferred. I made below script but somehow not convinced with the script as the logfile is also getting transferred and also running find command twice can result in some data discrepancy as might be by the time log file is getting generated a new file is received and gets transferred. Please suggest how to fix this.

#!/bin/bash

file=/root/test/file_$(date +%Y-%m-%d_%H%M%S)

find -maxdepth 1 -type f -mmin -60| cut -d'/' -f2 > $file

{ find -maxdepth 1 -type f -mmin -60| cut -d'/' -f2 | sed 's/^/put /' ; echo bye ; } | \
    /usr/local/bin/sshpass -p password sftp -oBatchMode=no username@xx.xxx.xxx.xxx:/folder
0

1 Answer 1

0

You can use tee to store the file names to a file as part of the pipe.

find -maxdepth 1 -type f -mmin -60| cut -d'/' -f2 | tee -a /path/to/log.file | sed 's/^/put /' 

-a will append if the file already exists.

1
  • Thank you @nohillside I used the tee command and works as expected.
    – Learner
    May 19, 2020 at 10:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .