ftp getting last modified file by date - bash

i am connecting remote server via ftp and
i am sending ls -t command but it's outputting files sorted by name
how can i get last modified file via ftp ?
Note: i am connecting windows ftp server from linux machine

ls -t will give you the last modified file on top
You can confirm this by viewing with full timestamps
ls -lt

ftp -n server <<EOF|awk 'END{for(i=9;i<=NF;i++)printf "%s ",$i}'
user username password
ls -ltr
EOF

In most Unix/Linux based ftp servers, the ls command is linked to the actual ls command. This is why all the other answers are saying to use ls -t with maybe a few more parameters thrown in.
However, since you're using a Windows machine as your server, it's much harder to say how exactly the command will work. I don't believe Windows comes with a default FTP server service. I know many sites use third party FTP services on their Windows machines. It's going to depend on the software your Windows machine is using, and how it's been setup:
Try something like this:
ftp> dir /O:D
or
ftp> ls /O:D
These use the Windows parameters for the built in dir command.

try this one, it worked for me.
ls -t1 | head -1

Related

Converting FTP "ls -ltr" to SFTP

I was migrating an FTP functionality to SFTP. In shell script that does FTP, I saw some lines and I can not convert them to SFTP. I searched but couldn't get a result.
With ftp, I can get remote file names to local log file like this:
ftp>
cd remote_folder
ls -ltr local_file.tmp
With sftp (OpenSSH client) this doesn't work. It says "file could not be found" about ls command. And it also says -t or -r options are invalid with ls command. How can I do same thing with sftp?
Thank you.
We do not know what "ftp" client you were using. So it's hard to tell what the ls -ltr was doing. Though my guess is that your "ftp" client was transparently passing the switches to the FTP server, not processing them locally. Again, we do not know what FTP server you were using. But I know of only one FTP server that supports -ltr and that is ProFTPD. In ProFTPD the -ltr means long directory listing sorted reversely by timestamp. The OpenSSH sftp client supports the same switches for ls command with the very same meaning. If it is not working for you, you are probably using a very old version of OpenSSH. The -tr switches seem to be supported since OpenSSH 3.6 (February 2001) and the -l even longer.
OpenSSH sftp does not support writing the listing to a file. But you can redirect whole sftp output to a file.
Something like this:
sftp -b script.txt user#example.com > local_file.tmp
With script.txt containing:
cd remote_folder
ls -ltr

Serverside FTP Batch script

Ive been using a .bat script to download only pdfs from an FTP server.
Up until now its been working perfectly using the mget *pdf function.
I recently moved the script and have it running from a Windows 2012 Server.
And now when script gets to the mget function, it lists the files that would normally transfer except with a ? after each instance, it then fails to actually download them to the lcd.
#ftp -v -n -i -s:C:\Users\LMCT\Desktop\Serversidesage\download-1.txt
This is the FTP function being called.
open xx.xxx.xx.xxx
user xxxxxxxxxxx
=xxxxxxxxxxxx
cd orders
lcd "C:\Users\LMCT\Desktop\Magento-downloaded-orders"
binary
prompt
mget *pdf
mdelete *pdf
quit
And this is the downloading script.
Does anyone know why this might be happening?
Thanks in advance.
Kind Regards, Lewis.
By Default Interactive Prompting is ON. When you used the -I option to launch the FTP command it is turning Prompting off. In your script you are then turning it back on with the PROMPT command which then throws off your MGET command.
So if the PROMPT command is in your FTP script:
open ftp.domain.com
user ftpuser
password
prompt
quit
It is easy test to see what is going on.
C:\BatchFiles\ftp>ftp -v -i -n -s:"script.txt" |find "Interactive"
Interactive mode On .
C:\BatchFiles\ftp>ftp -v -n -s:"script.txt" |find "Interactive"
Interactive mode Off .

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

Ftp command to remove bunch of files

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

Automatically copying files from a Linux machine to a Windows machine

I need to automatically copy files from a linux machine to a windows one every day.
I'm looking for something simple and secure like scp, rsync, sftp. Unfortunately, I'm at a loss of how to set this up on the Windows machine.
Does anyone know how to do this?
You can try mounting the Windows drive as a mount point on the Linux machine, using smbfs; you would then be able to use normal Linux scripting and copying tools such as cron and scp/rsync to do the copying.
You can find rsync for windows in cygwin, with that you can setup a rsync server on the windows box and run a cron job on your linux machine rsync'ing all the files to the windows machine. We used to do that and it worked fine.
"I'm at a loss of how to set this up on the Windows machine." Windows is the client or the server? At a loss means what, specifically? What can't you do?
"linux machine to a windows" can be done two ways.
Linux is client. Windows runs an FTP or SCP or SSH server. Linux has a client and pushes the file to Windows. Look at FileZilla for free windows FTP server. Also, windows often has an FTP service that's turned off. Turn it on.
Windows is client. Windows periodically pulls the file from the linux server. This is easier, since Linux already has all the necessary servers available. You do, howeveevr, need to start them on Linux.
There are scores of sftp, scp clients for Windows. Windows comes with an ftp client. Google for sftp client. You'll find WinSCP, Putty, filezilla, and list free country list of sftp clients.
I haven't used it in years now, but you could try Unison from http://www.cis.upenn.edu/~bcpierce/unison/
It could be done with 'smbclient', which acts much like an FTP client to a Windows share. Check out the manpage: man smbclient and look for ways to script it with the -c option, or man expect to drive it.
Here's how I'd probably do it though:
Pick which user you're going to be
when you sync the files. Log in as
this user and type 'id', and get the
numeric ID. You will use this ID in
step 4
Become 'root'
mkdir /mnt/sharename
Edit your /etc/fstab file and add an entry something like this. Replace the user ID of 500 with your user ID. Replace sharename with your windows share name. Replance WINDOWSHOSTNAME with your host name or IP address. If you don't know the shares, run smbclient -L WINDOWSHOSTNAME.
//WINDOWSHOSTNAME/sharename /mnt/sharename cifs credentials=/root/smblogin,uid=500,noauto,user 0 0
Edit /root/smblogin and put the following two lines in it
username=YOUR_WINDOWS_USERNAME
password=YOUR_WINDOWS_PASSWOD
Log in as the user from step 1.
Try mounting the share: mount /mnt/sharename
If that succeeds, then write a script to do it automatically. Let's call it 'backup.sh':
#!/bin/sh
df | grep -q /mnt/sharename
if test $? -ne 0 ; then
mount /mnt/sharename
fi
cp -r /path/to/dir /mnt/sharename/destination/
Use cron to run the script.
Type crontab -e
Put the following in the file:
PATH=/bin:/usr/bin
# Backup at 2:15 A.M. every day. Run 'man 5 crontab' for help on the time format
15 2 * * * /path/to/backup.sh
You may try WinSCP and its scripting support. And Windows support some kind of cron-like operation in its management stuff, don't they?

Resources