Ftp command to remove bunch of files - bash

I can download files using wget "ftp://user:pass#host/prefix*, but I cannot remove downloaded files from FTP. Any easy solution to do this in bash script?

As WhoSayln and Skilldrick said, you should use ftp to download files, and remove files from the server (if you have the permission to).
But in your question you're saying "I cannot remove downloaded files from FTP". Do you want to remove the local files from your computer (the ones you just downloaded from ftp server) or the files on remote server?
If is local, then just a rm -f file will do it :p
But if it's remote, and this is running on a script (a typical job in a batch) so try something like:
jyzuz#dev:/jean> ftp -n -i remoteserver.com << EOF
> user $username $password
> cd /remote/directory/
> rm filename.txt
> bye
> EOF
More or less? =P

If you need to script some operation on a FTP server, I would point you to lftp.
Main website
Tutorial

You want to use ftp for that.

wget is not the command you are lookin for. you can use ftp command instead. here is a large documentation about this;
http://linux.about.com/od/commands/l/blcmdl1_ftp.htm

Related

Automating ftp downloads with shell script

I want download a bunch of .txt.gz files by ftp. I've written this shell script. How do I get all the files on the sever with out specifying each file?
Some code..
#!/bin/bash
ftp -i -n <<Here
open ftplink.com
user Username password
bin
get XXX_xxxx_mp.txt.gz
get XXX_xxxx_mp.txt.gz
close
quit
Here
Use wget instead:
wget ftp://user:pass#example.com/dir/*_mp.txt.gz

Copy website from server to local in terminal

I've had a look on google and here on stack but can't find a good example on how to do this.
All I basically want to do is SSH into a server copy all the site files and paste them into a folder on my computer?
I normally use git but this is an old site which has not been setup with git so I just wanted to know a quick way to copy from the server as FTP sucks!
A simple process with commands for terminal would be great!
Check out rsync. It has the capability to operate over ssh. You might also want to look into ssh aliases (which it also honors) when copying files over, and it's what git uses to only sync the differences between two repositories.
The advantage of rsync over SCP or SFTP is that it can resume download if interrupted, takes little bandwidth to sync since it sends change sets instead of entire files (unless the file doesn't yet exist on one side), and can do one- or two-way sync depending on your preference.
ssh USER#SERVER "tar zcvf - /DUMP_DIR" | cat > /OUT_DIR/FILE_NAME_OF_ARCH
or
(rsync -avz --delete /DUMP_DIR USER#SERVER:/OUT_DIR &)
Look at SCP.
scp username#remotehost.com:/directoryname/* /some/local/directory
Use scp
scp -P 2222 json-serde-1.1.8-SNAPSHOT-jar-with-dependencies.jar root#127.0.0.1:
For Example.
Hope that helps!

Send files to other directories using ftp

I am new to FTP configuration. What I am trying to do is as follows:
I am running a shell script on my localhost and downloading some files to my machine. Now I want a functionality where the files which I downloaded should be stored in a temporary directory, and then it should be transferred to a location(other directory) which I specify. I feel this mechanism is achievable by FTP communication and will be helpful when I host this on a domain, but I am not getting resources from where I can teach myself how to set this up.
OK, having visited many sites, here are some resources you might find handy:
For configuring vsftpd, here's a manual of how to install, configure and use.
About receiving many files recursively via FTP, you can use wget (extracted from this site):
cd /tmp/ftptransfer
wget --mirror --username=foo --password=bar ftp://ftp.originsite.com/path/to/folder
About sending many files recursively, many people find the only way of doing so by tar-n-send; the only problem is that the files will remain tarred until you extract them by going to the other machine (remotely or via ssh) to extract the manually. There is an alternative, not using FTP, but using ssh and pipes which lets you have files extracted on target machine:
tar -cf - /tmp/ftptransfer | ssh geek#targetsite "cd target_dir; tar -xf -"
Explained:
tar is the application to make tar files
-c: create file
-f -: file name is "stdout"
/tmp/ftptransfer: include this folder and all subdirectories in the tar
|: Make a pipe to the next program (connect stdout to stdin)
ssh: Secure Shell program
geek#targetsite: username # machinename where you want to connect to
"..." command to send to the remote host
cd target_dir: changes the dir of output
tar -xf -: extracts the file received by "stdin"
For configuring SSH on Ubuntu, have a look here.
If you need more help, don't be afraid to ask! :)

Transfer files from Linux to Windows using pscp or some other tool

Problem Statement- I want to copy some files from remote machine (linux) to my windows machine. I know I can do it using pscp.
I tried looking on the internet, I found several articles, but in those articles I was not able to understand and I was having lot of problems in copying the files from Linx box to Windows.
Can anyone provide me step by step method here, so that I can follow that to transfer files. That will be of great help to me.
I am connected to host cli.vip.host.com using putty and that is linux with username- rkost and password as- password. And I want to copy file a.txt from linux to windows.
Download PSCP from below link
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
Run PSCP
Got to command prompt
Use the below code
Copy single file
pscp user#host:remote_path/file_name host_path\file_name
eg: pscp user1#192.168.1.10:/home/user2/a.txt c:\Desktop\a.txt
Copy all files in a folder
pscp user#host:remote_path/* host_path\
eg: pscp user1#192.168.1.10:/home/user2/* c:\Desktop\test\
Copy all files & folders in a folder
pscp -r user#host:remote_path/ host_path\
eg: pscp -r user1#192.168.1.10:/home/user2/ c:\Desktop\test\
For this kind of problem I use all the time netcat. First, you start netcat as server on a machine with an ip IP_address, and afterwards you send the file from the other machine.
nc -l -p <port-number> > out_file
will start it as server in "listen" state, and will save what you send to it in the file "out_file".(check the man page of your version for more parameters.)
From the other machine you send the file something like this:
< file_to_send nc IP_address
(If you want to send an whole directory, you use tar )
I never used it under Windows (because I work as linux engineer). But you can find nc for windows, that work the same as in linux...
if you want to use pscp, you can do this:
pscp -pw password rkost#cli.vip.host.com:/path/to/file c:\path\
if this doesn't work try to add enviroment variable:
set PATH=C:\path\to\putty\directory;%PATH%
After installing POWERSHELL
wow64_microsoft-windows-powershell-exe
you can open the terminal and execute this command line
pscp -r -P Port user#IP:path WINDOWS path
example:
pscp -r -P 2222 user#MyDommain.com:/var/www/html C:\2023\HTML
Make sure you are connected to your vpn server, (i.e. cli.vip.host.com)
use following command from your windows machine
pscp -v rkost#remote_ip_addr:/path/to/file/a.txt c:/some_location/
you can see the verbose with -v flag.
If you wants to copy directory from remote linux machine to your windows
just refer my answer in this
PSCP copy files from godaddy to my windows machine

How to CD inside a SFTP connection where the connection is established using - Shell script

In my script - i create a sftp connection.
I read some directory value from user earlier and once the sftp connection is established, i try to cd to that dir which i got from the user.
But its not working, probably bec the prompt goes inside the server to which the SFTP connection was established.
In this case how to make it work ?
I also faced this problem and was able to find the solution. The solution is right there in the man page of sftp. In the man page, you will find where it is written the format of using sftp like this:
sftp [options] [user#]host[:dir[/]]
Actually, two formats are given there but this is the one I wanted to use and it worked.
So, what do you do? You simply supply the username#host as seen there, then, without any space followed by : and the path you want to change to in the client/remote server in your script and that's all. Here is a practical example:
sftp user#host:/path/
If your script does, as you state somewhere in this page,
sftp $user#$host cd $directory
and then tries to do something else, like:
sftp $user#$host FOO
That command FOO will not be executed in the same directory $directory since you're executing a new command, which will create a new connection to the SFTP server.
What you can do is use the "batchfile" option of sftp, i.e. construct a file which contains all the commands you'd like sftp to do over one connection, for example:
$ cat commands.txt
cd foo/bar
put foo.tgz
lcd /tmp/
get foo.tgz
Then, you will be able to tell sftp to execute those commands in one connection, by executing:
sftp -b commands.txt $user#$host
So, I propose your solution to be:
With user's input, create a temporary text file which contains all the commands to be executed over one SFTP connection, then
Execute sftp using that temporary text file as "batch file"
Your script would do something like:
echo "Directory in which to go:"
read directory
temp=$( mktemp /tmp/FOOXXX )
echo "" > $temp
echo "cd $directory" >> $temp
# other commands
sftp -b $temp $user#$host
rm $temp
If you are trying to change the directory of the machine, try lcd
In what way is it not working? To change directories on the remote server, you use the "cd" command. To change directories on the local server, you use the "lcd" command. Read "man sftp".

Resources