Linux ftp delete files

Deleting files using ftp

I have developed a shell script to copy the files from source to destination and simultaneously to delete the copied files in source. I can copy the files but the files cannot be deleted in source side.

files='ls filename' for file in $files do ftp -vn $hostname  
  • I got errors as 'No such files or directories found'
  • By putting ftp outside the forloop also i got error as 'invalid command'
  • I also tried in ssh but it prompting for username and password

3 Answers 3

Put backticks, not simple quotes around the command to get its output.

I also tried in ssh but it prompting for username and password - check SSH-Login without password.

Scripting FTP commands using input stream directly to ftp is usually a bad idea: it lacks any error handling, it can go totally wrong and you have no chance to control it. If you have any chance to use saner command-line client, such as lftp , curl or a similar scriptable one.

Also, it's a very bad idea to iterate over files using

files=`ls files` for file in $files 

A slightly better solution is:

but it doesn't scale: if * (or ls output) would expand more than command line buffer, it will fail. A fairly scalable solution is something like:

find . | while read file do do_something_with $file done

. and yet it's not probably what you want. In fact, if you just want to transfer files from source to destination and then delete files at source, you can just use lftp with mput command and -E option to delete file after transfer, or something similar with rsync --remove-source-files .

This is because, at that place in the code you are on the ftp console until the EOFD string is reached, so to run any command on local system(source), you need ! to be prefixed.

Best way to test is manually executing the commands. Here's what I have tested:

mtk4@laptop:~$ ftp XXX.XXX.XXX Connected to static-XX-XX-XX-XX.XXXXXX.com. 220---------- Welcome to Pure-FTPd [privsep] [TLS] ---------- 220-You are user number 2 of 50 allowed. 220-Local time is now 07:52. Server port: 21. 220-IPv6 connections are also welcome on this server. 220 You will be disconnected after 15 minutes of inactivity. Name (XXXXXX.XXX:XXX): XXXXXXX 331 User XXXXXXX OK. Password required Password: 230 OK. Current restricted directory is / Remote system type is UNIX. Using binary mode to transfer files. ftp> lcd test/ Local directory now /home/mtk4/test ftp> pwd 257 "/" is your current location ftp> !pwd /home/mtk4/test ftp> !ls sample.txt ftp> !rm sample.txt ftp> !ls ftp> bye 221-Goodbye. You uploaded 0 and downloaded 0 kbytes. 221 Logout. mtk4@laptop:~$ 

use the same for loop again, after the complete ftp is done to iterate over the same set of files and delete them.

Источник

Linux shell script for delete old files from ftp

There is a problem - need to store the database backup on the FTP. On the FTP should not be more than 10 back-ups, ie, After you add backup to FTP should be removed, the oldest files to make the total number of files can not exceed 10 pieces. How can we implement such a removal from the ftp? I'm trying to write a script, but does not work delete:

5 Answers 5

This is a script I wrote to remove any files on a remote ftp site older than 7 days. It works by retrieving a listing of the directory, parsing the modified date, and then re-connecting to delete any files older than ndays.

I suspect that the numbers hard-coded into the loop (element date) may change depending on the setup of your system. The return formatting of the ls command is dependent on the local system settings.

Assuming your backups are every day, then setting ndays to 10 might solve your problem.

#!/bin/bash # get a list of files and dates from ftp and remove files older than ndays ftpsite="ftp.yourserver.com" ftpuser="loginusername" ftppass="password" putdir="/public_ftp/admin/logs" ndays=7 # work out our cutoff date MM=`date --date="$ndays days ago" +%b` DD=`date --date="$ndays days ago" +%d` echo removing files older than $MM $DD # get directory listing from remote source listing=`ftp -i -n $ftpsite ; FNO+=9));do # month (element 5), day (element 6) and filename (element 8) #echo Date $ $ File: $ # check the date stamp if [ $=$MM ]; then if [[ $ -lt $DD ]]; then # Remove this file echo "Removing $" ftp -i -n $ftpsite quit EOMYF2 fi fi done 

Источник

delete files from FTP servers with linux terminal

I'm new here, I would like to ask a question about FTP. I have an FTP server that contains multiple folders and there are PDF files in them. I wonder if there is a way to access this server through the terminal and delete PDF files through the terminal. my OS is ubuntu 19.10 I use this command to download the folders

wget -m --user=XXXXX --password=XXXX ftp://XXXXXXX:XXXX/ 

2 Answers 2

To delete files/folders from an FTP server, follow these steps:

  1. Type ftp and enter to continue.
  2. Type open and enter to continue.
  3. Type the ftp server IP address and enter to connect to ftp server.
  4. If connected, type login name and enter.
  5. Type user password and enter. If the username and password are valid, then you'll be in.
  6. Type help to list all available commands that you can run on an ftp server.
  7. Type ls to list all files and directories.
  8. Type cd to get into a specified folder.
  9. type mdelete to delete a file or multiple files or rmdir -r to delete a folder.
  10. Type y and enter to confirm the delete.

EDIT:
For some security reasons, you have to type y everytime you want to delete a file. To avoid that, you need to run ftp -i in the beginning (instead of ftp ). After that, you can delete files directly without typing y for every file (exp: mdelete folder_name/* will delete all files in folder_name).
Also, you can not delete a folder unless it is empty, so you have to delete all its content using mdelete folder_name/* , then run rmdir folder_name to delete a folder.

To automate this you could use something like this.[1]

#!/bin/bash HOST=ftp.example.com USER=ftpuser PASSWORD=P@ssw0rd ftp -inv $HOST  

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Читайте также:  Linux распаковать все архивы zip
Оцените статью
Adblock
detector