We have a situation where on sftp server we have older files available , but since the…
- User
We have a situation where on sftp server we have older files available , but since the file names have creation date and creation time which is varied , we only search with wildcard the first part of file name and then ending extension and copy it down. Now there are multiple files matching this and thus we are not able to find out in our script which one is newest
Assistant
You can solve this by modifying your script to compare file timestamps or parse the dates from filenames to identify the newest file. Here are a few approaches:
Option 1: Use SFTP file timestamps Most SFTP clients can retrieve file modification times. You can list files with timestamps and sort by date:
bash# Using sftp command with ls -lt (if supported) sftp user@server << EOF ls -lt /path/to/files/prefix*extension EOFOption 2: Parse dates from filenames If the creation date/time is embedded in the filename in a consistent format, you can extract and compare these dates:
bash# Example for filenames like "report_20240615_143022.txt" newest_file=$(sftp user@server << EOF | grep "prefix.*extension" | \ sed 's/.*\(prefix.*extension\).*/\1/' | \ sort -t_ -k2,2 -k3,3 | tail -1) ls /remote/path/prefix*extension EOF- User
What do you suggest in this case , Should we try to reduce frequency since We will be taking backup on our side ?
Or
Be more precise with the file name that We seek to download , which is partly not possible since file name contains Creation Date and Time and Date can be T-1 or T depending on when its generated,
Do we have a possibility to do something like checking in SFTP which one is the latest file and only then triggering the copy ? or maybe having the list of files on SFTP server , comparing with ones on our end and then Checking if the file was already copied so that We can avoid copying multiple files. this is a question make it grammatically correct
Assistant
Here's a grammatically corrected version of your question:
"What do you suggest in this case? Should we:
- Reduce the frequency since we will be taking backups on our side?
OR
- Be more precise with the file name that we seek to download? (This is partly not possible since the file name contains creation date and time, and the date can be T-1 or T depending on when it's generated.)
Do we have the possibility to do something like checking in SFTP which file is the latest and only then triggering the copy? Or maybe maintaining a list of files on the SFTP server, comparing them with the ones on our end, and then checking if the file was already copied so that we can avoid copying multiple files?"
My recommendation: Go with option 2 combined with intelligent file detection. Here's why and how: