How to exclude file when using scp command recursively

I need to copy all the *.c files from local laptop named hostA to hostB including all directories. I am using the following scp command but do not know how to exclude specific files (such as *.out):
$ scp -r ~/projects/ user@hostB:/home/delta/projects/
How do I tell scp command to exclude particular file or directory at the Linux/Unix command line?

One can use scp command to securely copy files between hosts on a network. It uses ssh for data transfer and authentication purpose. Typical syntax is:
scp file1 user@host:/path/to/dest/
scp -r /path/to/source/ user@host:/path/to/dest/

Scp exclude files

I don’t think so you can filter or exclude files when using scp command. However, there is a great workaround to exclude files and copy it securely using ssh. This page explains how to filter or excludes files when using scp to copy a directory recursively.

How to use rsync command to exclude files

The syntax is:
rsync av -e ssh --exclude='*.out' /path/to/source/ user@hostB:/path/to/dest/

  1. -a : Recurse into directories i.e. copy all files and subdirectories. Also, turn on archive mode and all other options (-rlptgoD)
  2. -v : Verbose output
  3. -e ssh : Use ssh for remote shell so everything gets encrypted
  4. --exclude='*.out' : exclude files matching PATTERN e.g. *.out or *.c and so on.

Example of rsync command

In this example copy all file recursively from ~/virt/ directory but exclude all *.new files:
$ rsync -av -e ssh --exclude='*.new' ~/virt/ root@centos7:/tmp
Sample outputs:

rsync command in action

Rsync command will fail if rsync not found on the remote server. In that case try the following scp command that uses bash shell pattern matching in the current directory (it won’t work with the -r option):
$ ls
Sample outputs:

centos71.log  centos71.qcow2 meta-data  user-data

centos71.log centos71.qcow2 meta-data user-data

Copy everything in the current directory except .new file(s):
$ shopt -s extglob
$ scp !(*.new) root@centos7:/tmp/

Sample outputs:

centos71.log                                  100% 4262     1.3MB/s   00:00    
centos71.qcow2                                100%  836MB  32.7MB/s   00:25    
meta-data                                     100%   47    18.5KB/s   00:00    
user-data                                     100% 1543   569.7KB/s   00:00

centos71.log 100% 4262 1.3MB/s 00:00 centos71.qcow2 100% 836MB 32.7MB/s 00:25 meta-data 100% 47 18.5KB/s 00:00 user-data 100% 1543 569.7KB/s 00:00

See the following man pages for more info:
$ man rsync
$ man bash
$ man scp

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...