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
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.
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/
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.
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 anonymous#gmail.com
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 ~]
Related
I want to be able to connect to a ftp server automatically using a single file. This input file contains commands to run in ftp session.
I am able to connect and login to a ftp server via terminal using:
ftp <IP>
Connected to <IP>.
220 ProFTPD Server (ProFTPD) [<IP>]
Name (<IP>:current_user): USER
331 Password required for USER
Password:
I enter password, the output is:
230 User USER logged in
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
200 PORT command successful
150 Opening BINARY mode data connection for file list
drwxr-xr-x 4 USER psacln 4096 Aug 11 2020 backups
drwxr-xr-x 2 USER psacln 4096 Mar 2 2019 error_docs
drwxr-xr-x 53 USER psacln 4096 Jul 10 03:18 httpdocs
drwx------ 2 USER root 4096 Aug 4 06:32 logs
drwxrwxr-x 12 USER psacln 4096 Aug 10 2020 moodledata
226 Transfer complete
ftp>
Suppose a single file named cmd includes the command "ls" and also username & password:
#!/bin/bash
HOST=<IP>
USER=USER
PASSWORD=<PASS>
ftp $HOST <<EOF
user $USER $PASSWORD
Using above file as input to:
ftp <ip> < cmd
How to use this file to auto connect to this ftp server?
I've just solved it.
I just use encoded password in the bach script:
#!/bin/bash
HOST=<ip>
USER=USER
PASSWORD=$(echo <encoded password> | base64 --decode)
ftp -inv $HOST <<EOF
user $USER $PASSWORD
ls
bye
EOF
My password contains char &ersand&, so it has error in connecting to ftp server.
Note that <encoded password> is obtained by:
echo '<PASS>' | base64
Just this!
I'm attempting to move multiple files on an FTP server to a different directory on the same server. So far, I've written a bash script that will login and retrieve any new files in the remote directory but the ftp command doesn't support a 'mv' command. Essentially the script would download the new file(s) and then once downloaded move the file(s) to a different directory on the same server. Please Note that the filenames will be different every time so the use of wildcards is important here.
Before you answer please note that this needs to be automated so using a GUI like Filezilla wouldn't help me as I would have to login to various ftp sites and move the files manually, also, keep in mind that I'm unable to ssh into any of the servers as they are managed by other company's and ftp access is all I'm able to get. Last thing, I won't know what the file names are so using a wildcard would be helpful.
Any help or guidance is truly appreciated.
Thank you!
Perhaps the rename command in ftp could work for you?
rename [from [to]]
Rename the file from on the remote machine, to the file to.
I gave it a bash with an old file I had sitting on a server and it seemed to do what you want:
ftp> ls tmp/test*
229 Entering Extended Passive Mode (|||55572|)
150 Accepted data connection
-rw-r--r-- 1 sinasohn sinasohn 21 Mar 31 16:37 tmp/testfile01
226-Options: -a -l
226 1 matches total
ftp> ls tmp2/test*
229 Entering Extended Passive Mode (|||64715|)
150 Accepted data connection
226-Options: -a -l
226 0 matches total
ftp> rename tmp/testfile01 tmp2/testfile01
350 RNFR accepted - file exists, ready for destination
250 File successfully renamed or moved
ftp> ls tmp/test*
229 Entering Extended Passive Mode (|||56698|)
150 Accepted data connection
226-Options: -a -l
226 0 matches total
ftp> ls tmp2/test*
229 Entering Extended Passive Mode (|||50239|)
150 Accepted data connection
-rw-r--r-- 1 sinasohn sinasohn 21 Mar 31 16:37 tmp2/testfile01
226-Options: -a -l
226 1 matches total
ftp>
I put blank lines in between commands here for clarity.
Hope this helps!
full script to achieve move more than one file
1. get file list from ftp server with mls command
2. generate to do list file
2.1 get file
2.2 rename (move file)
3. execute ftp command with to do list file
#!/bin/sh
clear
# change local directory
cd [local-directory]
#collect file names
ftp -ni ftp.abccompany.com <<EOF
user [user] [password]
cd /OUT
mls abc*.* list.txt
quit
EOF
# create ftp action list
echo >>todo.lst user [user] [password]
while read N
do
echo >>todo.lst cd /OUT
echo >>todo.lst get $N
echo >>todo.lst rename $N ARCHIVE/$N
done <list.txt
echo >>todo.lst quit
# ftp transfer process
ftp -nv ftp.abccompany.com <todo.lst
# cleanup
rm todo.lst
ftp rstatus $remotefile
is giving '?Invalid command' error on solaris. I came to know that there is no ftp command like rstatus on Solaris 10 unlike HP-UX. Basically on HP-UX, rstatus was used to get status of remote file. output like i.e.
-rw-r--r-- 1 dearshady users 20 Aug 2 10:28 remoteFile
On solaris ftp has ls command, but that is only for directory not file. Can anyone suggest a solution to get status of remote file on Solaris ftp?
You can use dir instead of rstatus
ftp> rstatus z
?Invalid command
ftp> dir z
200 PORT command successful.
150 Opening ASCII mode data connection for /bin/ls.
-rw-r--r-- 1 root root 34 mars 16 23:05 z
226 Transfer complete.
remote: z
58 bytes received in 0,00014 seconds (402,98 Kbytes/s)
How could I use grep and ls in FTP client...
I mean if I want to find some specific file I could use:
ls -l | grep pattern
With the usual Unix commandline interactive ftp, one approach is:
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> dir . foobar
output to local-file: foobar [anpqy?]? y
500 Unknown command
227 Entering Passive Mode (62,149,140,15,195,159)
150 Accepted data connection
11966 5.26 KB/s
226-Options: -a -l
226 156 matches total
ftp> !grep con foobar
-rwxr-xr-x 1 11050207 users 911007 Sep 13 2007 accu_pyconc.pdf
-rwxr-xr-x 1 11050207 users 9805405 Mar 25 2009 pycon_abst.pdf
i.e., get the dir results into a local file first, then run grep locally. Incidentally, this lets you run multiple greps after paying for just one dir data transfer;-).
lftp can, exactly the way you typed.
On Windows, you can do this with WinSCP scripting for any protocol, including the FTP:
winscp.com /command ^
"open ftp://username:password#example.com" ^
"ls /path/*.txt" ^
"exit"
References:
https://winscp.net/eng/docs/scripting
https://winscp.net/eng/docs/scriptcommand_ls
You can mount an sftp folder to a host where you have permissions to use grep or any tool.
After downloading files from a remote UNIX FTP server, you want to verify that you have downloaded all the files correctly. Minimal you will get information similar to "dir /s" command in Windows command prompt. The FTP client runs on Windows.
Sadly this was written for Unix/Linux users :/
Personally, I would install CYGWIN just to get Linux binaries of LFTP/RSYNC to work on windows, as there appears not to be anything that competes with it.
As #zadok.myopenid.com
mentioned rsync, this appears to be a windows build for it using CYGWIN ( if you manage to be able to get ssh access to the box eventually )
http://www.aboutmyip.com/AboutMyXApp/DeltaCopy.jsp
Rsync is handy in that it will compare everything with check sums, and optimally transfer partial change blocks.
If you get CYGWIN/Linux:
http://lftp.yar.ru/ is my favorite exploration tool for this.
It can do almost everything bash can do, albeit remotely.
Example:
$ lftp mirror.3fl.net.au
lftp mirror.3fl.net.au:~> ls
drwxr-xr-x 14 root root 4096 Nov 27 2007 games
drwx------ 2 root root 16384 Apr 13 2006 lost+found
drwxr-xr-x 15 mirror mirror 4096 Jul 15 05:20 pub
lftp mirror.3fl.net.au:/> cd games/misc
lftp mirror.3fl.net.au:/games/misc>find
./
./dreamchess/
./dreamchess/full_game/
./dreamchess/full_game/dreamchess-0.2.0-win32.exe
./frets_on_fire/
./frets_on_fire/full_game/
./frets_on_fire/full_game/FretsOnFire-1.2.451-macosx.zip
./frets_on_fire/full_game/FretsOnFire-1.2.512-win32.zip
./frets_on_fire/full_game/FretsOnFire_ghc_mod.zip
./gametap_setup.exe
......
lftp mirror.3fl.net.au:/games/misc> du gametap_setup.exe
32442 gametap_setup.exe
lftp mirror.3fl.net.au:/games/misc> du -sh gametap_setup.exe
32M gametap_setup.exe
lftp mirror.3fl.net.au:/games/misc>
Do this :
ls -lR
..................
If you have ssh access, use rsync instead. It is a far better data transfer app.
Grab fuse for your OS and load ftpfs. This will let you mount the remote ftp directory locally and you can use dir /s or any other application you want on it.
Assuming you are using simple ftp via command line,
Use dir command with -Rl option to search recursively and copy it to a file and then search the file using grep, find or whatever way is supported on your OS.
ftp> dir -Rl education.txt
output to local-file: education.txt? y
227 Entering Passive Mode (9,62,119,15,138,239)
150 Opening ASCII mode data connection for file list
226 Transfer complete
You can use ftp.listFiles("directory") from apache-commons-net and can write your own BFS or DFS to fetch all the files recursively.