Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to exclude directories when copying files to a remote machine

Amaaz Ahmad

In Linux, two of the commands which we can use to transfer data from one machine to another are:

  • scp
  • rsync

The scp command stands for secure copy and the rsync command stands for remote sync.

While copying the data from one machine to another, we might need to exclude certain directories or files. In this case, using the rsync command is a better choice, as the scp command does not provide such a functionality.

Syntax of rsync command:

The following snippet provides the general syntax of the rsync command:


In the above snippet, OPTIONS can be replaced by multiple flags to alter the default functionality of the rsync command. SRC is replaced by the path of the source directory or file. DEST is replaced by the path of the destination directory.

If data is being copied within the same machine, relative paths for the source and destination are given. If any of the sources or destinations is a remote machine, it needs to be identified by its unique identifier.

Excluding directories

If we want to exclude a specific directory, e.g., a directory named “xyz”, we can use the following syntax:

rsync -a --exclude 'xyz' src_directory/ dst_directory/

In the above snippet of code, the -a flag tells the rsync command to sync folders recursively. The --exclude option tells the rsync command which directory to exclude.

We can also use the same --exclude option to exclude files from copying. The following snippet stops a text file named “abc.txt” from being copied:

rsync -a --exclude 'abc.txt' src_directory/ dst_directory/




Amaaz Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring