How to recursively exclude files when using the scp command

Source: Internet
Author: User
Tags scp command man rsync

How to recursively exclude files when using the scp command

Q: I need to copy all the folders containing the *. c file from the local notebook named hostA to hostB. I use the followingscpCommand, but do not know how to exclude a specific file (such as *. out ):

  1. $ scp-r ~/projects/ user@hostB:/home/delta/projects/

How to tellscpDoes the command exclude a specific file or directory in the Linux/Unix Command Line?

People can usescpCommands securely copy files between network hosts. It uses ssh for data transmission and authentication. The typical syntax is:

  1. scp file1 user@host:/path/to/dest/
  2. scp-r /path/to/source/ user@host:/path/to/dest/
Scp exclusion File

I don't think you can filter or exclude files when using the scp command. However, there is a good solution to exclude files and use ssh to securely copy them. This page describes how to usescpFilter or exclude files when copying directories recursively.

How to Use the rsync command to exclude files


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


  1. -a: Recursion to the directory, that is, copying all files and subdirectories. In addition, open the archive mode and all other options (equivalent-rlptgoD)
  2. -v: Detailed output
  3. -e ssh: Use ssh as the remote shell so that everything is encrypted.
  4. --exclude='*.out': Exclude matching files, such as *. out or *. c.
Rsync command example

In this example~/virt/Recursively copy all files in the directory, but exclude all *. new files:

  1. $ rsync -av -e ssh--exclude='*.new'~/virt/ root@CentOS7:/tmp

Sample output:

Scp exclude files but using rsync exclude command

If the remote server cannot be foundrsync, ThenrsyncCommand will fail. In this case, try the followingscpCommand, which matches with the bash shell pattern in the current directory (it cannot match-r):

  1. $ ls

Sample output:

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

Copy all contents in the current directory except. new:

  1. $ shopt -s extglob
  2. $ scp!(*.new) root@centos7:/tmp/

Sample output:

  1. centos71.log 100%42621.3MB/s 00:00
  2. centos71.qcow2 100%836MB32.7MB/s 00:25
  3. meta-data 100%4718.5KB/s 00:00
  4. user-data 100%1543569.7KB/s 00:00

For more information, see the following manual page:

  1. $ man rsync
  2. $ manbash
  3. $ manscp

How to exclude file when using scp command recursively

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.