Categories
Linux script Tool

LFTP/sftp automation script

I am currently writing a Linux shell script. The script requires to download some files from a sftp server and then move those files to a backup folder on the same server. Eventually the script will be scheduled as a cron job. Everything works fine for one file, but not multiple files. There is no recursive delete kind of commands can be used. One of the easy ways I found is using LFTP.

The theory is easy, use a lftp command to generate a file list from remote server, and then generate the move command for each file. You can refer back to the origin post from here. For whatever reason, my LFTP version doesn’t support the command mentioned in the post, I end up use another command to achive the same goal. It’s always a good idea to know different ways of doing the same thing and pick which way works for you. Here is the script I used :

recls -1 remote.dir1/ | “sed ‘s/\(.*\)/mv \”\1\” \”remote.dir2\/\”/'” > list && source list && !rm list