In Linux we can use 2 commands to move files or directoryes to a remote server or from a remote server
- scp - secure copy
- rsync - remote sync
From the scp and rsync the better choice is to use rsync as it’s offering more options and a straight forward way to exclude what you want easely. So without making this longer let’s see the way to use both rsync and scp to exclude files.
Excluding Files or Directories with Rsync
When using Rsync, you can easily exclude specific directories or files from the synchronization process. This allows you to fine-tune your sync to only include the data that is relevant to you. Here’s how you can exclude files with Rsync:
--excludeoption: Rsync provides the
--excludeoption, followed by the path of the file or directory you want to exclude. You can use this option multiple times to exclude multiple files or directories. For example:
rsync --exclude 'dir1' --exclude 'dir2' source/ destination/
This command will exclude
dir2from the synchronization process.
Exclude files based on patterns: Rsync also allows you to exclude files based on specific patterns. For instance, you can exclude all files with a certain extension or exclude files with a particular naming pattern. Here’s an example:
rsync --exclude '*.txt' --exclude 'backup*' source/ destination/
In this case, all files with the
.txtextension and files starting with
backupwill be excluded.
Exclude directories recursively: If you want to exclude directories and their subdirectories, you can use the
--exclude-diroption. This option works similarly to
--excludebut applies to directories instead. Here’s an example:
rsync --exclude-dir 'dir1' --exclude-dir 'dir2' source/ destination/
This command will exclude
dir2along with their subdirectories.
To exclude multiple directories or files, you can specify them using a comma-separated list. For example, to exclude both “docs” and “tmp” directories, you can use the command:
rsync -av --exclude="docs,tmp" source_directory destination_directory
Note: Remember, when using Rsync, the order of the
--exclude-diroptions matters. Rsync processes them from left to right, so place more specific exclusions before more general ones.
Excluding Files with SCP
SCP (Secure Copy) is a popular choice but is not the best one if you would like to exclude files. SCP is not offering a default command that you can use and exclude any file or directory you want.
However you can try excluding files or directoryes with SCP with extglob, this works in bash so enable extglob you do:
shopt -s extglob
After you can run your SCP command:
scp !(*.txt) root@centos7:/tmp/
This will copy everything in the current directory except the txt files to the remote server /tmp locations.
The extglob extended pattern matching list is the below one:
|Matches zero or one occurrence of the given patterns.|
|Matches zero or more occurrences of the given patterns.|
|Matches one or more occurrences of the given patterns.|
|Matches one of the given patterns.|
|Matches anything except the given patterns.|
If you want more linux related articles you should check: