scp all files ending '-123.jpg' - recursion, spaces, wildcards …

1

There's too much going on for me to get my noobie head around here. I'm wanting to download all files that end -123.jpg from a multitude of nested directories on a remote server. Some of these directories have spaces in their names. I'm thinking that the command should be along the lines of:

scp -r user@server:/path/to/parent\ directory/*/*/*123\.jpg ./

… where "parent\ directory" is a directory name with a space, and the specified path goes as deep as it can before it splits off to various sub-directories, for example dir/sub dir/[uniquely_id]-123.jpg file. (Note that these sub-directories often contain spaces too, should that affect the * wildcard)

I'm getting 'no match' returned for this, or 'no such file or directory' if I meddle with the space escaping. I'm thinking therefore that it's the recursion or the wildcard that I've got wrong.

Thanks in advance.

hyppy

Posted 2010-11-01T18:50:07.783

Reputation: 11

Answers

3

Lacking your server layout for testing I don't know if the following command actually works, but I think rsync would be better than scp in that case, as you can specify in- and exclude patterns. Something like:

rsync -arvzSH  --include "*123.jpg" --exclude "*" user@server:/path/to/parent\ directory/ /path/to/target directory/

may work. But you better double check the rsync documentation.

lothar

Posted 2010-11-01T18:50:07.783

Reputation: 131

1

Filename escaping with scp is tricky; your arguments get (re)expanded on the remote side. "Interesting" characters have to be double quoted, to avoid special handing by your local shell and by the remote. I would expect both the following to work for you:

scp user@server:'/path/to/parent\ directory/*/*/*123.jpg' ./
scp user@server:'"/path/to/parent directory"/*/*/*123.jpg' ./

If that's confusing, rsync is pretty easy to use (as lothar mentions), and piping tar or cpio through ssh is pretty easy too.

ssh user@server 'cd /path/to/parent\ directory;
                 find -name "*123.jpg" -print0 | cpio -0 -o' |
    cpio -i -d -v

ephemient

Posted 2010-11-01T18:50:07.783

Reputation: 20 750

0

This properly should be done in two parts:

  1. first find all needed files and copy them to one folder (this does not work if you have the same filenames) - you may do it with one find and -exec
  2. then send a bunch of files using scp

scp is not enough advanced to copy folders structure and there is no single command to make a copy of a folders tree.

Also, if you need all files in folders, as you will not be able to select them by wildcards, and you want to preserve folders structure - you may want to use mc = Midnight Commander and its "Shell link".

pbies

Posted 2010-11-01T18:50:07.783

Reputation: 1 633

-1

A work around for scp with wildcards:

for i in "*-123.jpg"
do 
    scp $i <user>@<server>:/path/to/destination/
done

Refer: https://community.hpe.com/t5/System-Administration/How-to-scp-mutiple-files-using-wild-chars-or-in-a-single-scp/m-p/3639440/highlight/true#M238959

AmitM

Posted 2010-11-01T18:50:07.783

Reputation: 1

What if filename of the file will contain space? It won't work, I think. Maybe find command would be better (with xargs)? – pbies – 2019-01-04T07:28:11.220

Not sure why -1 ? the solution works. If there are other scenarios it can be improved further. But the given solution is NOT wrong. – AmitM – 2019-01-11T06:28:46.787

1For this particular question the answer is ok, but we try to make things universal. There are few bugs in this answer: 1. never for ls 2. $i should be in quotes 3. find should be used – pbies – 2019-01-12T18:42:05.070

>

  • updated the answer to remove 'ls', not required.
  • $i works without quotes
  • works for files with space in names.
  • < – AmitM – 2019-01-14T05:25:32.583

    1I'm not sure what shell you're using, but in de-facto standard sh/bash, an unquoted $i will have its value subject to word-splitting and so values with spaces in them will not work as expected – they'll be passed to scp as multiple arguments, each having part of the file name. – user1686 – 2019-01-14T05:49:53.127

    I am running bash shell. When I put single quote around $i , then error: $i: No such file or directory

    If I put double quote then I get error: *-123.jpg: No such file or directory – AmitM – 2019-01-15T08:52:26.400