Efficient File Copying with Rsync: Including Only Certain File Types

When it comes to efficient and reliable file synchronization and copying, Rsync stands out as a powerful tool. Not only does it allow you to transfer files between local and remote locations, but it also offers the flexibility to include or exclude specific files based on their types.

Here is the Rsync command for including certain file ypes while excluding the others:

rsync -zarv --include="*/" --include="*.sh" --exclude="*" "$from" "$to"

Explanation of the Rsync Options:

  • -z: Compress the data during the transfer, reducing the network bandwidth usage.
  • -a: Archive mode that preserves various file attributes such as permissions, timestamps, etc.
  • -r: Recursively copy directories.
  • -v: Verbose mode to display detailed information about the copying process.
  • –include=”*/”: This option includes all directories in the copying process. Without this, Rsync would skip directories as we are using specific includes.
  • –include=”*.sh”: This option includes all files with the “.sh” extension (shell script files). You can replace “*.sh” with any other file pattern you desire.
  • –exclude=”*”: This option excludes all files that do not match the patterns specified by the “–include” options.

Using the “-m” Option to Ignore Empty Directories:

The “-m” option in Rsync is used to prune empty directories during the file copying process. When you include the “-m” option, Rsync will remove any empty directories from the destination after the files have been copied. This is especially useful when you want to keep your destination directory clean and clutter-free, devoid of any empty folders that may have been present in the source.

Please follow and like us:
This entry was posted in Linux and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *