How to copy a file without using scp inside an ssh session?

60

28

I have logged on to a system with ssh and there is no scp present on both the systems. How to copy a file without using the scp program.

Talespin_Kit

Posted 2011-06-01T08:57:30.073

Reputation: 1 724

@n.m. quotes required (for me, ubuntu server) – Ivan Black – 2014-09-19T10:12:33.207

@n.m. Doesn't work for me - file is corrupted (I guess the login message breaks it). – monnef – 2017-06-25T12:10:51.177

Is netcat (nc) present on both systems? If it is, use your ssh session to tunnel a TCP port and use nc on that port. – None – 2011-06-01T09:06:27.323

Do you have rsync? – slhck – 2011-06-01T09:08:16.470

3you can also do this: ssh user@remotehost cat /path/to/remote/file > /path/to/local/file – n. 'pronouns' m. – 2011-06-01T09:11:17.353

Answers

102

To send a file:

cat file | ssh ajw@dogmatix "cat > remote"

Or:

ssh ajw@dogmatix "cat > remote" < file

To receive a file:

ssh ajw@dogmatix "cat remote" > copy

Flexo

Posted 2011-06-01T08:57:30.073

Reputation: 1 897

One implication of what ggg and Flexo say above is that you can't use the "-t" option to ssh. – mjg123 – 2014-11-05T13:43:54.707

Why do you use cat? – PyRulez – 2015-06-11T22:47:06.647

@pyrulez - on the local end it's habbit - I normally use a pipe from some more complex process and don't just transfer files. On the remote end there needs to be a process of some sort spawned, cat is a good choice. – Flexo – 2015-06-11T23:16:33.943

This is brilliant, and was necessary on the tiny embedded device I was using that didn't have scp. – moodboom – 2016-11-24T01:51:58.397

Neat - this even works to copy from windows to linux if you use "type" instead of cat on the windows end (using windows 10's built in beta ssh client). (text files, haven't tried binary) – Frederik – 2017-12-24T17:40:02.677

1Nice! You can also use pv instead of the first cat so you get a progressbar, eg pv file | ssh ajw@dogmatix "cat > remote" – Theolodus – 2018-05-18T09:14:13.173

I knew there had to be a way to do this - thanks for the details. It's going into my personal unix tips knowlegebase, because there's no way I'll remember this the next time I'll need it. – Michael Burr – 2018-12-12T02:17:03.430

You. The. Man. . – Jonas Byström – 2019-07-08T11:06:15.697

20@ggg that's not true at all. cd /tmp; cat /bin/bash > test; chmod a+x test; diff test /bin/bash; ./test all works fine. There's nothing inherently "magic" about binary files. Both files in my example compared identical and have the same checksum. It's true that copy and pasting from a terminal window won't work because of things like control sequences and unprintable characters, but using pipes like this these never go near a terminal. – Flexo – 2012-09-23T10:35:12.597

@Flexo I need something like this, the only exception is, I need to pipe in all jpg from a folder. How could iterate through /storage/sdcard1/*jpg and > to files with the same name ? – George Profenza – 2013-07-03T11:58:51.843

2@GeorgeProfenza you'll need to add tar into the mix. tar cvf - /path/*.jpg | ssh foo@bar.com "tar xvf -" or something similar ought to work. – Flexo – 2013-07-03T20:36:28.987

4

Try this:

cat myfile.txt | ssh me@otherhost 'cat - > myfile.txt' 

Keith

Posted 2011-06-01T08:57:30.073

Reputation: 7 263

2no need for that many cat calls at all – Flexo – 2011-06-01T09:13:38.727

2

You can use xxd and some ugly quoting to copy over multiple files as well as run commands on them and execute them:

ssh -t foo@bar.com "
echo $'"$(cat somefile | xxd -ps)"' | xxd -ps -r > "'somefile'"
chmod +x somefile
echo $'"$(cat someotherfile | xxd -ps)"' | xxd -ps -r > "'someotherfile'"
chmod +x someotherfile
./somefile
./someotherfile
"

Aric

Posted 2011-06-01T08:57:30.073

Reputation: 21

ah, my answer is clearly too l33t – Aric – 2016-01-08T20:07:21.590

-1

python3 -m http.server in the same directory with desired file - after that you can curl or wget or download a file with your browser. Note that with that running command all your files from current directory will be publicly available, until you press Ctrl+C.

Vitaly Zdanevich

Posted 2011-06-01T08:57:30.073

Reputation: 93

-1

Besides piping the file to a remote cat, you may also be able to use some SFTP client to transfer the files.

salva

Posted 2011-06-01T08:57:30.073

Reputation: 181

+1 but it should be noted that the OP is attempting to avoid scp because it does not exist on the systems. Given this constraint it's also probable that an FTP server needs to exist on the receiving end which makes the copying process dependent on software other than what is usually there by default. – Paul Sasik – 2015-05-05T16:50:52.640