Bash – delete file on ftp server using bash commands

bashftpscripting

using bash commands and ftp connection i have to perform two tasks:

  1. delete oldest file in some directory on ftp server. name of file that have to be deleted starts with backup_
  2. delete oldest subdirectory (on ftp server) in some directory

can you help me how to achieve?

i have to integrate those functions in existing file, so i will have no problems how to create ftp connection etc.

Best Answer

You can use lftp as a non interactive ftp client from within the bash scripts.

Or assuming you have a fairly sane and modern distribution you could use curlftpfs FUSE filesystem to mount the ftp server folder as a local folder then use bash to do the logical bits you need to to, then unmount the ftp server.

This effectively takes away the complexity of having the files only available over ftp as the script will see the files as available locally.

There are plenty of examples available on how to delete files based on name or date, and there is an example on the curlftpfs site on how to mount and unmount file systems in scripts.