Go2Linux | Linux Operating System

A site dedicated to: Linux Operating System

scp + find -Search files in a remote server and copy them to your machine-

Date: 2009-04-10 00:00:00 -0400

Some of the most popular posts here in Go2Linux are:

How to transfer files with ssh and 10 useful examples of the Linux find command

So, I have decided to merge them in a good tip to look for files in a remote server and then copy them to your local machine, I haven't already needed something like this, but it may be useful for someone (I hope).

I will write a bash script, that will do the work for you, if you want to run this as a script it will be handy not to need to enter the password for each single file to copy so you better use this post to enable login with no password on the remote server

This is the script, feel free to copy, use and modify according to your needs, if you can improve it, please repost it with your improvements, you may use the forums to post your changes or the comments here.

#!/bin/bash

#########################################
## Bash Script to find files on remote ##
## server and copy them to your local  ##
## machine, using find and scp         ##
## Licenced under the GNU GPLv3 licence##
## Author: Guillermo Garron            ##
## Date: 2009-04-10                    ##
#########################################

usage()
{
echo "usage copy-remote.sh [user@server] [path/to/look/in] ['file-pattern-to-look-for']"
echo "Remeber to use the ('') in the pattern"
}

if [ $# != 3 ] 
then
    usage
    exit 1
fi
ssh $1 "find $2 -name '$3'" > /tmp/file-list.txt
while read filename; do
        scp $1:$filename /tmp/;
done < /tmp/file-list.txt

If you liked this article please share it.

powered by TinyLetter

If you want to contact me in any other way, please use the contact page.