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.
Related
I am new to linux commands use pattern matching. In my local folder, I have many .txt files with certain endings.
foobar_type01.txt
foobar_type02.txt
foobar_type03.txt
baz_type01.txt
baz_type02.txt
baz_type03.txt
My goal is to scp all of the files from my directory to a server that end with ...type03.txt so in this case only the following would be copied:
foobar_type03.txt
baz_type03.txt
What is the correct command to scp filename matches as opposed to extensions? All I have been able to do is use the extension pattern
scp -C -r /my_folder/*.txt server#10.10.55.28:
scp -C -r /my_folder/*type03.txt server#10.10.55.28:
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
I'm trying to download multiple files trough SFTP on a linux server using
sftp -o IdentityFile=key <user>#<server><<END
get -r folder
exit
END
which will download all contents on a folder. It appears that find and grep are invalid commands, so are for loops.
I need to download files having a name containing a string e.g.
test_0.txt
test_1.txt
but no file.txt
Do you really need the -r switch? Are there really any subdirectories in the folder? You do not mention that.
If there are no subdirectories, you can use a simple get with a file mask:
cd folder
get *test*
Are you required to use sftp? A tool like rsync that operates over ssh has flexible include/exclude options. For example:
rsync -a <user>#<server>:folder/ folder/ \
--include='test_*.txt' --exclude='*.txt'
This requires rsync to be installed on the remote system, but that's very common these days. If rsync isn't available, you could do something similar using tar:
ssh <user>#<server> tar -cf- folder/ | tar -xvf- --wildcards '*/test_*.txt'
This tars up all the files remotely, but then only extracts files matching your target pattern on the receiving side.
I log into server_a and run .sh file, which has the following script:
scp user#server_b:/my_folder/my_file.xml user#server_b:/my_new_folder/
to copy files from my_folder to my_new_folder at server_b. It doesn't throw an error, but no files are copied.
Notes:
server_b is accessed by the pre-set rsa_keys.
server_a: unix
server_b: ubuntu
can SCP files from/to these servers without any issues
The end goal is to move or copy/remove files.
There are two possibilities:
Connect from server_a to server_b and do local copy:
ssh user#server_b "cp /my_folder/my_file.xml /my_new_folder/"
Do copy over the server_a. Your method would require the server_b to be able to authenticate to itself, which is probably not the case:
scp -3 user#server_b:/my_folder/my_file.xml user#server_b:/my_new_folder/
Also note that your code copies only one file and not files as you write in the title.
If you are logged on to the server, why are you authenticating again:
scp user#server_b:/my_folder/my_file.xml user#server_b:/my_new_folder/
You should be in the directory of file or simply use scp and use -v parameter to see the debug information.
Run as follows:
scp -v /my_folder/my_file.xml user#server_b:/my_new_folder/
It is not a directory nor it is recursive, so you do not need to -r parameter.
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.