I would like to create a cron job for deleting the oldest file in a directory which is on FTP server. I have created my first shell script
#!/bin/sh # connect to backup FTP server... lftp -u (username),password backup.contabo.net << EOF cd /backups ls EOF
This is what I get http://image.prntscr.com/image/72a86bf453c849a8af4a3340e4936172.jpeg
I did some searching and the following line worked when I tested it in the terminal inside a test folder on VPS (with some newly created .txt files):
rm "$(ls -t | tail -1)"
...but when I place it inside the shell script and when I run it with
bash testing.sh I get
rm: Access failed: 550 index.php: No such file or directory
Why is the script trying to delete index.php? I don't even see it inside file listing.
It should delete
EDIT Ok I finally succeeded to delete file but with manually writing the filename.
So I was thinking to first create a variable with the filename so I can pass it to
rm /backups/$FILENAME.tar or similar, I haven't done this because the following code is giving me the error -
Unknown command 'FILENAME="testing.sh"
This is basically the same thing, it means I get the latest file on local directory and not in the FTP directory (even though single
ls worked OK for retrieving all files)
FILENAME="$(ls -t | tail -1)" echo $FILENAME