Ineed 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/ [email protected]:/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 [email protected]:/path/to/dest/
scp -r /path/to/source/ [email protected]:/path/to/dest/
Scp exclude files
How to use rsync command to exclude files
rsync av -e ssh --exclude='*.out' /path/to/source/ [email protected]:/path/to/dest/
Where,
- -a : Recurse into directories i.e. copy all files and subdirectories. Also, turn on archive mode and all other options (-rlptgoD)
- -v : Verbose output
- -e ssh : Use ssh for remote shell so everything gets encrypted
- --exclude='*.out' : exclude files matching PATTERN e.g. *.out or *.c and so on.
Example of rsync command
$ rsync -av -e ssh --exclude='*.new' ~/virt/ [email protected]:/tmp
Sample outputs:

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:
$ shopt -s extglob
$ scp !(.new)* [email protected]:/tmp/
Sample outputs:
$ man rsync
$ man bash
$ man scp