Visit Our UNIX and Linux User Community


Need help creating a script to FTP files to a server and then delete the files that were transfered.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help creating a script to FTP files to a server and then delete the files that were transfered.
# 1  
Old 01-17-2012
Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? Smilie Please be gentle, I'm fairly new to this scripting thing.



#Will move audio files from <Linux> to <Windows>
#Sets the current directory to the audio storage area on the server.
Code:
#!/bin/bash
 
cd /var/spool/asterisk/monitor
 
!#Start FTP Process
HOST='<host address>'
USER='anonymous'
PASSWD='12345'
 
ftp -i -n $HOST <<auto_move_ftp.log
user ${USER} ${PASSWD}
 
bin
!#Moves q5000 audio files into the admin folder
cd /Admin
mput q5000*
 
!#Moves q5002 and q5003 audio files into the <City> folder
cd /<City>
mput q5002*
mput q5003*
 
!#Moves q5007, 5008 and 5010 audio files into the <Department> folder
cd /<Department>
mput q5007*
mput q5010*
mput q5008*
 
!#Moves q5200 audio files into the <City 2> folder
cd /<City 2>
mput q5200*
 
!#Moves q5001 audio files into the <Crazy> folder
cd /<Crazy>
mput q5001*
bye
 
rm /var/spool/asterisk/monitor/q5000*
 
rm /var/spool/asterisk/monitor/q5002*
 
rm /var/spool/asterisk/monitor/q5003*
 
rm /var/spool/asterisk/monitor/q5007*
 
rm /var/spool/asterisk/monitor/q5008*
 
rm /var/spool/asterisk/monitor/q5010*
 
rm /var/spool/asterisk/monitor/q5200*
 
rm /var/spool/asterisk/monitor/q5001*


Last edited by methyl; 01-17-2012 at 08:55 PM.. Reason: please use code tags
# 2  
Old 01-17-2012
"#!/bin/bash" must be the very first line. Not the second or third. If it's not the first line, you may not get the shell you want, or it may not run at all.

What are the lines beginning with "!" for? If you want a comment, the line should begin with "#", not "!".

I think you're confused on what << is for. It doesn't read a file name. It defines text inside the script to feed into something else -- it acts sort of like file redirection in that programs read data on standard input from it, but it's not really a file.

Try this:

Code:
cat <<EOF
This is a line of text
EOF

That should cause cat to print "this is a line of text". It's like cat < filename except it's reading data that your script gives it instead of reading from a file that was redirected into it.

You're feeding commands into FTP the same way, but haven't got it quite right. Try this:

Code:
ftp <<SOMESTRING
        commands I want to feed into FTP
SOMESTRING

commands I need to do in the shell

Note that the ending "SOMESTRING" is right at the beginning of the line. This isn't optional. If it's not at the beginning of the line, the here document may just never end!
# 3  
Old 01-17-2012
The !'s in the FTP portion were to comment out those lines, # didn't work and generated "invalid command" when it ran.

So to clarify, this goes at the top:
Code:
#!/bin/bash
#Will move audio files from <Linux> to <Windows>
#Sets the current directory to the audio storage area on the server.

and for the FTP:
Code:
ftp -i -n $HOST <<auto_move
     ftp commands
auto_move
 
shell command <delete file transfered>


Last edited by methyl; 01-17-2012 at 08:59 PM.. Reason: please use code tags
# 4  
Old 01-18-2012
here's how i've always done it...

Code:
#!/bin/bash

cd /var/spool/asterisk/monitor

#Start FTP Process

ftp -i -n ftpserver << EOF
quote user anonymous
quote pass 12345
bin
cd /Admin
mput q5000*
bye
EOF

rm /var/spool/asterisk/monitor/q5000*


Last edited by crimso; 01-18-2012 at 10:08 AM.. Reason: forgot code tags
# 5  
Old 01-18-2012
Thanks!

Worked Perfectly! Thanks for the help!

Previous Thread | Next Thread
Test Your Knowledge in Computers #753
Difficulty: Medium
The BIOS of the original IBM PC and XT had an interactive user interface.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help with creating script to delete log files/folders

Hi I am new to Linux / scripting language. I need to improve our Linux servers at work and looking to claim some space my deleting log files/ folders on a 5 day basis. Can someone help me with creating a script to do so. Any sample script will be helpful.:b: Regards (2 Replies)
Discussion started by: sachinksl
2 Replies

2. Shell Programming and Scripting

Unix shell script to delete files on windows server

Hi experts, can anyone suggest me on the below: how to write a shell script to search and delete files on windows server. -script runs on unix box -it should search for specific files on windows server and delete them periodically. (2 Replies)
Discussion started by: chpradeepch
2 Replies

3. Shell Programming and Scripting

Automated script to look for files in FTP Server location.

suppose one file comes in one sever location on MOnday.we have to write a script to automatically get that files and put it in different server location. ---------- Post updated at 10:28 AM ---------- Previous update was at 10:27 AM ---------- Please help me on this (2 Replies)
Discussion started by: sonam273
2 Replies

4. Emergency UNIX and Linux Support

Shell script to get all the files from FTP server

Hi Guru's, I am new to UNIX. my requirement is to log on to FTP server and get all the .txt files. i have developed one script by searching some forums but getting error and not able to fix them. pls see below code. ftp -i-n<<EOF open $FTP_HOST... (30 Replies)
Discussion started by: arund_01
30 Replies

5. Shell Programming and Scripting

how to delete 3 days old files using ftp in a shell script (Solaris OS)

I need help in writng a script to delete last three days files from a remote server using only FTP / SFTP. I was using find command which is not working and i cant use ssh Immediate response is highly appreciated . Thank in advance! ---------- Post updated 12-05-10 at 09:16 PM ----------... (5 Replies)
Discussion started by: Olivia
5 Replies

6. Shell Programming and Scripting

script for to take files from FTP server to UNIX server and Unzipped that files

script for to take files from FTP server to UNIX server and Unzipped that files (1 Reply)
Discussion started by: sunilamarnadh
1 Replies

7. UNIX for Dummies Questions & Answers

ftp files from one server to another using shell script

Hi Guys Any Help I have created a spool file that i need to copy onto another server using FTP in a shell script both servers are linux (3 Replies)
Discussion started by: itai
3 Replies

8. Shell Programming and Scripting

shell script to ftp the files from windows to unix server

Hi, I need to ftp some input files from windows to unix server.All the files will be saved in the C drive in my machine. Currently all these files are transferring manually to the unix server.I need to write a shell script which ftp the files from windows to unix box.When I searched in the... (10 Replies)
Discussion started by: kavithakuttyk
10 Replies

9. Shell Programming and Scripting

delete old files from FTP server

Hi I just started learning Unix scripts. I need to access an FTP server, delete all the files except the latest 6 files and the latest 6 files should be downloaded to my local server. Could some body help me to code this logic in KSH ? Thanks Athena (1 Reply)
Discussion started by: Athena
1 Replies

10. Shell Programming and Scripting

Moving Completely Transfered files

I have a driectoty called TEMP on server 1.There is a process running on another server which is pushing files in this directory of server 1. I want to write a script which will move the files from TEMP directory to TEMP2 directory on server 1 only but make sure that it will only copy those files... (2 Replies)
Discussion started by: 33junaid
2 Replies

Featured Tech Videos