13

I'm a novice linux user and I am trying to send a long list of files from one computer to another. The argument list is too long, so I am using find. I am having trouble setting up the expression, though. Can someone help?

Here is what I would normally type for a short argument list.

scp ./* phogan@computer/directory...

Here's I think this might translate into with find.

scp find . -name "*" phogan@computer/directory...

Maybe I could use piping? Any suggestions would help. Thanks in advance.

4 Answers 4

17
find . -name "*" -exec scp '{}' phogan@computer:/directory ';'

Normally I would 'tar' all the files together into one huge blob and call 'scp' just once. Something like this:

tar czfv - file1 file2 dir1 dir2 | ssh phogan@computer/ tar xvzf - -C directory
  • One could play around with the --exclude= or --include= parameters of tar.
  • Another option would be to use rsync.
3
  • if using passwords with ssh/scp, wouldn't the solution with find ask for a password on every file? Oct 5, 2009 at 21:47
  • lets just assume he knows how to use ssh-keys :) the problem with 1000 password questions is also the reason for the tar-approach
    – akira
    Oct 5, 2009 at 23:17
  • @akira Does your second solution involving tar looks in any way for files? how do you suggest to adapt it to include find in it and not just a bunch of predefined files like 'file1 file2 dir1 dir2'?
    – haroldmoma
    Oct 15, 2020 at 5:13
19

You can do it with just one command scp.

  • for newer versions of scp:
scp `find <path> -name <expression>` user@host:<path_where_to_copy>
  • for older versions:
scp --exec=`find <path> -name <expression>` user@host:<path_where_to_copy>

Make sure to encapsulate the find command in between backticks ` and not single quotes '.

5
  • Which scp version, which linux distribution it that? E.g. scp in Ubuntu 14.04 does not have this option.
    – geekQ
    Aug 19, 2015 at 9:47
  • 2
    Hi, older versions support it, the new versions support a simpler command: scp `find <path> -name <expression>` user@host:<path_where_to_copy> Aug 28, 2015 at 16:48
  • 1
    Doesn't work on multiple files
    – Amruta
    Jul 8, 2016 at 20:33
  • @Amruta use -r
    – slybloty
    Apr 19, 2018 at 18:10
  • @slybloty scp -r is for recursion on a single (directory) entry, not for multiple entries (multiple files).
    – AndrewF
    Apr 3, 2019 at 22:20
2
for f in `find . -name "*"`;do scp $f phogan@computer/directory;done
3
  • yea he wants a single command anyway
    – user1931
    Oct 5, 2009 at 18:42
  • useful, used it with grep -l pattern *
    – Tanj
    Oct 16, 2009 at 17:39
  • Does not seem to work on files with spaces in them. Jul 18, 2017 at 7:17
1

I would suggest

find . -print0 | tar --null --files-from=/dev/stdin -cf - | ssh phogan@computer tar -xf - -C /directory

Note, that this solution avoids having the filenames on the command line where they might be interpreted as command line arguments.

Another thing to watch out for is that filenames might contain spaces. This means that a for loop in bash might have difficulties with a list of filenames.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .