Linux: How to find files with specific extensions and scp them to another host

One very common task of Sysadmins / Devops is to transfer files from one server to another, the most common way is to use scp, a tool that…

Linux: How to find files with specific extensions and scp them to another host
Photo by Austin Distel on Unsplash

One very common task of Sysadmins / Devops is to transfer files from one server to another, the most common way is to use scp, a tool that allows us to transfer files, its syntax is very straightforward

scp <filename> <user>@<host>:/destination/path

But how we can copy a selection of files based on their extension? let's see how!

The find command

Let's talk a bit about the find command, find allows us to find files based on different options, one option is to filter by name

find /source/path -type f -name '*.csv'

This command allows us to find only files under /source/path than end with *.csv

How find connects its output with other commands

The find command has a very handy option named -exec which allows you to pipe each found result to another command, in our case we want to scp each result to a host, to do this enter

find /source/path -type f -name "*.csv" -exec scp {} remote_user@remote_host:/remote_directory \;

It might be a bit confusing but let's break down the most important parts

  • {} This is a “holder” and is replaced with each found file
  • scp is the command that transfers files
  • remote_user the user that will login to remote_host
  • \; just remember to use it, find needs it

Conclusion

In this short article we saw how we can use find and scp to transfer files, which is a very essential task of any sysadmin / devops engineer, I hope you found this article interesting and easy to read