Deleting files using ftp - shell

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.

Related

E2E tests - removing FTP directory [duplicate]

I am connected to a Unix server and I am trying to, via FTP, delete the directory dir with several files in it. If I use
ftp> delete dir/*
I get
550 Wildcard is ambiguous.
When I use
ftp> prompt off
Interactive mode off.
ftp> mdelete dir/*
I still get
550 Wildcard is ambiguous.
When I try
ftp> glob
Globbing on.
ftp> mdelete dir
I'm prompted for every file.
How can I easily delete/empty-and-delete a directory without getting prompted for every file?
I got it to work in two steps, on a server with restricted access, no SFTP, only FTP through commandline.
Like this :
mdelete folder_name/*
rmdir folder_name
If you've hidden files or folders on your server (for example .folder), you have to set the lftp list-options to "-a".
So this worked for me:
$ lftp -u user,pass server
> set ftp:list-options -a
> cd /folder/to/be/empty/
/folder/to/be/empty/> glob -a rm -r *
Use lftp to log into your server, this supports the rm -r command.
lftp user, password server
then:
rm -r directory
the -r stands for "recursive".
info:
http://en.wikipedia.org/wiki/Lftp
http://en.wikipedia.org/wiki/Rm_%28Unix%29#Options
$ ftp -i ...
will turn off prompting on mdel, which is what you want. It can't be done inside ftp.
rmdir directoryName
this directory must be in the current directory however.
cheatsheet: http://www.cs.colostate.edu/helpdocs/ftp.html
I'm using Filezilla, and it deletes folders recursively. I believe the ftp does not have a command that recursively deletes folders.

Scripting a file move on an FTP Server

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

Bash - uploading multiple files to FTP using real path?

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

ftp shell script breaks up after the lcd-command

I created a shell script to download several files, all starting with "2014" from an ftp server. I use mget for this and the filename 2014*.
To make sure that the files are saved at the right local place I use lcd before.
It looks like this:
#!/bin/sh
HOST='ftpserver.name.de'
USER='user1'
PASSWD='pw1'
FILE='2014*'
LOCDIR='/home/local/data2014/'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
lcd $LOCDIR
mget $FILE
quit
END_SCRIPT
exit 0
when I try this, the script just runs:
lx9000: ftp_get.sh
Connected to ftpserver.name.de.
220 FTP-Server: ftpserver.name.de
331 Password required for user1
230 User user1 logged in
Local directory now: /home/local/data2014/
221 Goodbye.
why dose it stop before the downloading proceeds?
Thanks for help!
Try adding:
prompt off
before the mget.

moving remote files inside ftp in shell script

I want to ftp file to my remote server and then move those files into another directory in the remote server
The ftp happens correctly but the movement is throwing an error like
550 RNFR command failed.
Can you please help?
My script is
#!/bin/sh
echo "Enter the version of the xml (eg:- v17.25)"
read version
HOST_FIRST='un01'
HOST_LAST='01'
USER='someuser'
PASSWD='somepassword'
HOST="$HOST_FIRST$FILE$HOST_LAST"
ftp -n $HOST <<-END_SCRIPT
quote USER $USER
quote PASS $PASSWD
cd /tmp
put myfile.xml
rename myfile.xml /tmp/test_ftp
quit
END_SCRIPT
exit 0
You have put myfile.xml to the tmp dir, why not just
edit
Change your script from
rename myfile.xml /tmp/test_ftp
TO
rename myfile.xml test_ftp
You have already put the file in the /tmp directory, which you have already done a cd /tmp .
That should work.
You can't specify a path in an ftp rename command and expect it to be moved as well.
And sorry, not what you want to here, but there is no move command in ftp. It is that way for security.
IHTH.

Resources