In *nix, I can create an empty file using cp:
cp /dev/null ~/emptyfile
I'd like to know if it's possible to do something similar using scp (instead of ssh + touch). If I try to run:
scp /dev/null remoteserver:~/emptyfile
it returns an error /dev/null: not a regular file
EDIT:
Just to clarify, I don't wanna run any command at the remoteserver (i.e. No ssh command should be invoked).
So it's ok to run some command at localhost (echo, cat, dd or any other trivial command) and copy the resulting file to remoteserver.
It's preferable not leaving the resulting file at localhost. It's also good if the resulting command is an one-liner solution.
EDIT2:
It's impossible to use /dev/null approach as in cp command, because scp only works with regular files and directories:
https://github.com/openssh/openssh-portable/blob/8a85f5458d1c802471ca899c97f89946f6666e61/scp.c#L838-L850
So it's mandatory to use another command (touch, cat, dd etc) to create a regular empty file (either in a previous command, pipe or a subshell).
As #willh99 commented, creating an empty file locally, and then performing scp is the most feasible solution.
So far I came up with this:
filename=$(mktemp) && scp $filename remoteserver:~/emptyfile; rm $filename
It creates an empty file in a subshell, and copies it to remoteserver as emptyfile.
Any refactor/improvements are welcome.
EDIT: remove $filename whether scp succeeding or not, as stated by #Friek.
If you're just trying to create an empty file, you can use ssh and run the touch command like this:
ssh username#remoteserver touch anemptyfile
Related
I am trying to compare two files one local and other one on remote. I used
meld a.txt user#host:/home/user/john/b.txt
This was not possible because file could not be detected. However, I could copy the same file from the same location to local via scp and do the comparison afterwards. How to access the file directly on cluster for example like:
vim user#host:/home/user/john/b.txt
In bash, you can create a file from a process with <(...):
meld a.txt <(ssh user#host cat /path/to/file/b.txt)
If you want to modify the remote file, you'll have to use some mounting.
One way to do it is to use sshfs
# sshfs setup
mkdir ~/remote
sshfs user#host:/path/to/file ~/remote
# meld invocation
meld a.txt ~/remote/b.txt
Check the path in your example, I think it's incorrect. Try directly:
vim john#server:b.text
That should work.
Try save the remote file to your linux system and simply use diff -y file1 file2 command. You will see the diff between.
diffutils - packagre required for it
I'm trying to zip up all .log files in a my log directory. I want to zip each log file individually and keep it in the same directory, then delete the original. I'm somewhat new to using Linux and for loops in Linux. Here is the for loop I'm trying to run
ssh user#SERVER "for i in *.log; do zip -m \"${i%.*}.zip\" \"${i%.*}\".*; done"
What ended up happening is all of my hidden files got zipped up. Like I said, I'm kinda new so, whatever syntax error I made isn't jumping out at me. Any help would be appreciated
Try this (you were near)
ssh user#SERVER 'for i in *.log; do echo zip -m "\${i/.log/.zip}" "\${i}"; done'
If the output seems correct, remove the echo
I am trying to SCP a file from a remote host onto local host.
The file on the remote host would be, KMST_DataFile_[MMDDYY]T[HHMM].kms
I have come up with 2 SCP commands, but I was wondering if there's a way to combine these, to only SCP file that match both the file name pattern above and the extension .kms
scp -v user#remotehost:/location/KMST_DataFile_*
scp -v user#remotehost:/location/{*.kms}
This will do your job:
scp -v user#remotehost:/location/KMST_DataFile_*.kms
As #manu mentioned in the comment, on Ubuntu or Mac, you may need to escape the asterisk:
scp -v user#remotehost:/location/KMST_DataFile_\*.kms
The main thing here is to use recursive mode -r even if you copy files and not directories. It works.
If you want to copy files that start with "val" and contain also the string "v2" then use:
scp -r makis#server.gr:/media/Data/results/val*v2* /Users/makis/Desktop/
Here, vecs*v2* will expand and get only files that start with val and also contain v2 string.
Similarly, if the files end with .png for example use:
scp -r makis#server.gr:/media/Data/results/val*.png /Users/makis/Desktop/
You should use \* instead of using *
scp -v user#remotehost:/location/KMST_DataFile_\*
ssh user#host 'tar cf - /location/KMST_DataFile_* /location/{*.kms}' | tar tvpf -
Note that these taroptions only give you a table of contents. You'll want to check before you extract, and almost certainly remove the absolute path.
I am using the following command remotely from a code and get the complete list of files from AIX box under /web/sample folder
ssh username#10.10.10.10 ls -lLtp /web/sample
Now I need to tweek this command, in such a way where I want to specify to get the list of file names starting with g*
I tried like the following it didnot work
ssh username#10.10.10.10 ls -lLtp /web/sample g*
I am getting the complete list as above command(1st command)
Please suggest or give me the correct format to make it work
Quote the command so that it is not expanded locally, and change directories on the remote end:
ssh username#10.10.10.10 'cd /web/sample; ls -lLtp g*'
You could do:
ssh username#10.10.10.10 -- 'cd /web/sample && ls -lLtp g*'
Also, you might need to add the "d" option to ls, if g* give only one result and if the result is a directory, you'll get the content of this directory.
The proper syntax is
ssh username#10.10.10.10 "ls -lLtp /web/sample/g\*"
hi as a continue to may previous question (ls command error via SFTP in Linux shell script) i have a question:
How can i get the name (or enter) of the latest created directory via SFTP connection ?
As i was told here the function ls -tr | tail -1 option won't work here, as parameters as -tr are not recognized in SFTP.
for example the script after SFTP connection:
cd temp_dir
?????????
assuming that the temp_dir containing several directories, i need to enter the last created directory in it (in order to download the files from it).
how can i do that ?
Thanks.
While sftp use ssh, the better solution is to ssh to the server and :
cd $(ls -t | sed q)
Your previous question contains the essential fact that you use lftp; therewith using cls instead of ls will help.
cls -1t|sed -n 1s/^/cd\\ /p>/tmp/cd
source /tmp/cd
Beware, this uses the file /tmp/cd and is not suited for concurrent operation.