Create directory in local machine(Linux) by using sftp - linux

I have tried the command below to create a directory after I downloaded a file from sftp. But unfortunately the command couldn't run. What's the error between this command?
sftp username#servername:/server/path/xxx.txt lmkdir /my/home/directory/new_path

to create a directory just use
lmkdir dirName
http://www.bic.mni.mcgill.ca/users/kate/Howto/sftp_notes.html
jimmy#moody:~$ sftp localhost
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is 97:52:17:f7:e7:c4:5b:68:00:62:6d:42:2c:ee:5a:60.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
jimmy#localhost's password:
Connected to localhost.
sftp> lmkdir dirName
sftp> exit
jimmy#moody:~$ ls dirName/

lmkdir path
Create local directory specified by path.
Also you might want to use
lcd path
Change local directory to path.
lls [ls-options [path]]
Display local directory listing of either path or current directory if path is not specified. ls-options
may contain any flags supported by the local system's ls(1) command. path may contain glob(3) characters
and may match multiple files.

The command you are invoking is invalid. As others here have mentioned the command to create a local directory while in the sftp shell is lmkdir, however you are not invoking the shell, you are trying to copy the file into a directory while creating the directory.
The way your current command is being interpreted is as a request to download "servername:/server/path/xxx.txt" into a file called "lmkdir" followed by an invalid option of "/my/home/directory/new_path" which sftp doesn't understand.
What you want to do instead is
mkdir /my/home/directory/new_pathsftp
sftp username#servername:/server/path/xxx.txt /my/home/directory/new_pathsftp
or from the sftp shell:
$ sftp username#servername
Connected to username#servername
sftp> lmkdir /my/home/directory/new_pathsftp
sftp> lcd /my/home/directory/new_pathsftp
sftp> get /server/path/xxx.txt
sftp> quit
You can include the three lines executed in the sftp shell as a batch file to be executed when connected, eg
sftp username#servername -b batchfile
where batchfile contains the following
lmkdir /my/home/directory/new_pathsftp
lcd /my/home/directory/new_pathsftp
get /server/path/xxx.txt
quit

Related

How to save FTP session logs in file in Linux

I am using a bash script in Linux to transfer files to a server. My script is running from cron and I have directed output to a file but I cannot know from logs if the file has been transferred to B server or not.
This is the cron:
1>>/home/owais/script_test/logs/res_sim_script.logs 2>>/home/owais/script_test/logs/res_sim.logs
And the FTP is as below:
cd ${dir}
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
lcd $dir
cd $destDir
bin
prompt
put FILENAME
bye
The only thing that I get in the logs is:
Local directory now Directory_Name
Interactive mode off.
Instead of using FTP, there is rsync. Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to or from another host over any remote shell, or to, or from a remote rsync daemon.
More information at the following webpage, https://linux.die.net/man/1/rsync
I have used ftp -inv Host << EOF >> LogFilePath and it worked. Thank you all for the support

How to properly upload a local file to a server using mobaXterm?

I'm trying to upload a file from my local desktop to a server and I'm using this command:
scp myFile.txt cooluser#192.168.10.102:/opt/nicescada/web
following the structure: scp filename user#ip:/remotePath.
But I get "Permission Denied". I tried using sudo , but I get the same message. I'm being able to download from the server to my local machine, so I assume I have all permissions needed.
What can be wrong in that line of code?
In case your /desired/path on your destination machine has write access only for root, and if you have an account on your destination machine with sudo privileges (super user privileges by prefixing a sudo to your command), you could also do it the following way:
Option 1 based on scp:
copy the file to a location on your destination machine where you have write access like /tmp:
scp file user#destinationMachine:/tmp
Login to your destination machine with:
ssh user#destinationMachine
Move the file to your /desired/path with:
sudo mv /tmp/file /desired/path
In case you have a passwordless sudo setup you could also combine step 2. and 3. to
ssh user#destination sudo mv /tmp/file /desired/path
Option 2 based on rsync
Another maybe even simpler option would be to use rsync:
rsync -e "ssh -tt" --rsync-path="sudo rsync" file user#destinationMachine:/desired/path
with -e "ssh -tt" added to run sudo without having a tty.
Try and specify the full destination path:
scp myFile.txt cooluser#192.168.10.102:/opt/nicescada/web/myFile.txt
Of course, double-check cooluser has the right to write (not just read) in that folder: 755, not 644 for the web parent folder.

FTP Script to move files from Suse server to ftp

We need to move all the files from particular folder to ftp server. We wrote a script but getting directory not found exception. Below is the script.
#!/bin/sh
HOST='192.168.1.100'
USER='ramesh'
PASSWD='rameshftp'
ftp -inv $HOST << EOF
user $USER $PASSWD
cd /home/Ramesh
put *.zip
bye
EOF
Our requirement is to copy all the files which resides in some directory in Suse Linux Server and copy to FTP server. for eg: Copy all the contents from "/home/Ramesh" directory and put into ftp server.
You can do this in one line with ncftp:
ncftpput -u username -p password ftp-host-name /path/to/remote/dir /path/to/local/dir/*
See http://linux.die.net/man/1/ncftp for more info

Rsync to Amazon Linux EC2 instance - failed: No such file or directory

I want to upload the content of one directory to my Amazon EC2 with rsync:
rsync -r -t -v --progress -z -s -e "ssh -i /home/mostafa/keyamazon.pem" /home/mostafa/splitfiles ubuntu#ec2-64-274-161-87.compute-1.amazonaws.com:~/splitfiles
but I receive the following error message:
sending incremental file list
rsync: link_stat "/home/mostafa/splitfiles" failed: No such file or directory (2)
rsync: change_dir#3 "/home/ubuntu//~" failed: No such file or directory (2)
rsync error: errors selecting input/output files, dirs (code 3) at main.c(712) [Receiver=3.1.0]
and if I do a dry run with grsync, it works correctly
In rsync the trailing / is very important. Also you rsync usually defaults to ssh when one of the destinations contains a host.
So you if you want to preserver modification times then you can get rid of the -e and -s options.
Your command could be written as /home/mostafa/splitfiles/ ubuntu#ec2-64-274-161-87.compute-1.amazonaws.com:splitfiles/ - notice the trailing /'s provided that you have ssh configured to read the private key from your home directory.
On ubuntu you can add this to the key chain, by going
ssh-add [key-file]
And this will save you having to specify the keyfile everytime you ssh into the AWS machine.
The errors seem to say that on the local machine you don't have a source directory and the destination doesn't exist.
I completed this task with Filezilla instead, easier to use.
You are at home ~ if you cd ../ to root you will be able to run the command.

How can I copy file from local server to remote with creating directories which absent via SSH?

I can copy file via SSH by using SCP like this:
cd /root/dir1/dir2/
scp filename root#192.168.0.19:$PWD/
But if on remote server some directories are absent, in example remote server has only /root/ and havn't dir1 and dir2, then I can't do it and I get an error.
How can I do this - to copy file with creating directories which absent via SSH, and how to make it the easiest way?
The easiest way mean that I can get current path only by $PWD, i.e. script must be light moveable without any changes.
This command will do it:
rsync -ahHv --rsync-path="mkdir -p $PWD && rsync" filename -e "ssh -v" root#192.168.0.19:"$PWD/"
I can make the same directories on the remote servers and copy file to it via SSH by using SCP like this:
cd /root/dir1/dir2/
ssh -n root#192.168.0.19 "mkdir -p '$PWD'"
scp -p filename root#192.168.0.19:$PWD/

Resources