Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was connected my OpenSSH, but What's the command for ssh to find all files which are *.png and get them at the same time, recursively

share|improve this question
up vote 6 down vote accepted

You can list files of your interest with ssh, then download them with scp:

ssh user@host find /remote_path -iname '*.png' | 
  while read pngfile; do 
    scp user@host:"$pngfile" /local_path

All *.png files under /remote_path will be downloaded to /local_path, but the remote directory structure will not be created (all files will be put in the same directory), so you may have problems if you have two files with the same name in different directories.


The remote find command line should be corrected in this way:

find /remote_path -iname "'*.png'"

where the internal pair of single quotes are needed to avoid local shell pathname expansion of *, wheras the external pair of double quotes are needed to avoid remote shell pathname expansion of *.

The two types of quotes could be exchanges, and other syntaxes could be used, like \''*.png'\', \""*.png"\" or "\"*.png\"".

If you don't have .png in remote home directory, there is no difference between the two commands, but it is better to be safe.

share|improve this answer
Thanks, What's programming language at your "while" statement? – Smile.Hunter Aug 8 '11 at 10:08
It is a bash statement, present also in other shells, see – enzotib Aug 8 '11 at 10:16
1. unless you have keys, you will have to provide password for each file. 2. this is very slow solution. 3. will not work if file name has space – Michał Šrajer Aug 8 '11 at 12:41
if you want to transfer multiple files, you can user "-r" for scp, but solution with tar will be faster anyway. – Michał Šrajer Aug 8 '11 at 12:43
@Michał Šrajer: corrected the spaces problem. Moreover, -r will get whole directories, I think, not only desired file types. – enzotib Aug 8 '11 at 12:48

The fastest way I know is to create tar.gz remotely, transfer it via SSH and untar locally:

ssh user@remotehost 'find /remote/path/ -name "*.png" -print0 2>/dev/null | xargs -0 tar cfz - ' | tar xvfz -

This will work with filenames with spaces and is faster than scp -r and much faster than scp for each file.

share|improve this answer
bash: syntax error near unexpected token `)' – enzotib Aug 8 '11 at 12:42
also when syntactically correct, it breaks on filenames with spaces. – enzotib Aug 8 '11 at 12:50
fixed. now it deals with filenames with spaces and it's faster than the other solution. – Michał Šrajer Aug 8 '11 at 13:02
It could be shortened to: ssh user@remotehost 'find /remote/path/ -name "*.png" | tar -T- cfz - ' | tar xvfz - – enzotib Aug 8 '11 at 13:03
@enzotib: I didn't know -T-. Nice trick. However, it does not work: tar: You must specify one of the -Acdtrux' or --test-label' options – Michał Šrajer Aug 8 '11 at 13:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.