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
Related
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 ~]
I have this script to upload files to FTP (I know FTP is not secure, though client insists of using FTP..). It works fine, but the problem with it is does not recognize path provided when doing upload, even though message says it uploaded successfully, but nothing is uploaded.
So the script looks like this:
#!/bin/bash
HOST=host
USER=user
PASS=pass
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ftp -inv $HOST << EOF
user $USER $PASS
get
cd /path/in/ftp/
prompt
mput $DIR/*.csv
# End FTP Connection
bye
EOF
rm $DIR/*.csv
Here what is outputted:
Connected to host.
220 You have connected to Client's FTP server.
?Invalid command
331 User name okay, need password.
230-Welcome user from ip. You are now logged in to the server.
230 User logged in, proceed.
Remote system type is UNIX.
Using binary mode to transfer files.
?Invalid command
250 Directory changed to "/path/in/ftp/"
?Invalid command
Interactive mode on.
mput /path/inv_numbers_2016-11-21_12_09.csv? 200 PORT command successful.
150 File status okay; about to open data connection.
226 Closing data connection. Transferred 140 bytes in 1 seconds. 0KB/second.
140 bytes sent in 0.00 secs (1395.1 kB/s)
?Invalid command
221 Session Ended. Downloaded 0KB, Uploaded 1KB. Goodbye user from ip.
Now if I change mput $DIR/*.csv to mput *.csv, then it works (I get same log output like with previous one, except it shows path as being directly in directory where script is). But this only works if I would run script directly from directory it is placed in.
Any ideas?
Replace
ftp -inv $HOST << EOF
by
cd "$DIR" && ftp -inv $HOST << EOF
or by
cd "$DIR" || exit 1
ftp -inv $HOST << EOF
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 <<EOFD
quote USER $username
quote PASS $password
binary
cd $start_dir
rm -rf $file
quit
EOFD
done
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
files=`ls filename`
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:
for file in *
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.
Full-proof solution:
Replace the line
`rm -rf $file`
with
`!rm -rf $file`
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:~$
Or another solution,
use the same for loop again, after the complete ftp is done to iterate over the same set of files and delete them.
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.