Reputation: 77
I need to download a file from a ftp server and delete it after is transferred (on remote server) It's possibly in a single command line?
curl ftp://host/testfile.txt -X 'GET testfile.txt' --user user:password -o local.txt
curl ftp://host/testfile.txt -X 'DELE testfile.txt' --user user:password
Thanks
Upvotes: 2
Views: 5834
Reputation: 26
WinSCP has an option -delete
that you can combine either with put
or get
commands, to delete the source file after a succesfull change.
This option lead me to move from curl to WinSCP, because now I can write simpler "transactional" scripts, and I'm sure that when I delete the original file from the "transfer" folder, it is because it was successfully delivered.
https://winscp.net/eng/docs/scriptcommand_get
https://winscp.net/eng/docs/scriptcommand_put
However, curl is quite powerful for other types of scripts and URL management, nowadays I use a mix of both utilities depending on the task to be done.
Upvotes: 1
Reputation: 4518
Yes, it is possible to do this in a single command:
curl -Q '-DELE testfile.txt' --user user:password -o local.txt ftp://host/testfile.txt
From the man page:
-Q, --quote
(FTP SFTP) Send an arbitrary command to the remote FTP or SFTP
server. Quote commands are sent BEFORE the transfer takes place
(just after the initial PWD command in an FTP transfer, to be
exact). To make commands take place after a successful transfer,
prefix them with a dash '-'. To make commands be sent after
curl has changed the working directory, just before the transfer
command(s), prefix the command with a '+' (this is only sup‐
ported for FTP). You may specify any number of commands.
Upvotes: 2
Reputation: 345
I recommend creating a script of your FTP commands, and piping them into the built-in ftp
client. Here's an example (replace the ls
command in the test.ftpscript
with your GET/DEL
commands) that worked on my machine:
[user@host ~] cat > test.ftpscript
user anonymous [email protected]
ls
bye
[user@host ~] ftp -inv ftp.swfwmd.state.fl.us < test.ftpscript
Connected to ftp.swfwmd.state.fl.us (204.76.241.31).
220
331 Please specify the password.
230 Login successful.
227 Entering Passive Mode (204,76,241,31,191,167).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 7478 Dec 05 09:59 README.txt
drwx------ 2 0 0 16384 Dec 04 13:40 lost+found
drwxr-xr-x 20 0 0 4096 Dec 18 14:42 pub
lrwxrwxrwx 1 0 0 3 Dec 05 10:07 public -> pub
drwxr-xr-x 3 0 0 4096 Dec 04 15:26 pvt
226 Directory send OK.
221 Goodbye.
[user@host ~]
Upvotes: 0