Skip to content

Copy files

How to copy files with command-line tools.

Windows-specific

See robocopy

tar

tar and pipes can copy file system trees directly, preserving permissions.

tar cf - $src | tar xvf - -C $dst

rsync

rsync -avh --info=progress2 sourcefolder/ destinationfolder/

Note

Without a trailing slash on the source (sourcefolder), rsync copies the entire source directory, including its name, into the destination. The result is destinationfolder/sourcefolder.

With a trailing slash on the source (sourcefolder/), rsync copies the innards (content of the sourcefolder), into the destination. The result is destinationfolder/README.txt if there is sourcefolder/README.txt.

Copy a directory tree with exclusions

How to exclude some files/folders while copying a folder tree.

tar

A filter file .tarignore excludes stuff similar to .gitignore.12

tar -c -X .tarignore -f - srcfolder | tar xvf - -C dstfolder

The syntax is similar to .gitignore:

.tarignore
.DS_Store
.git
.gitignore

rsync

Use --exclude flag in rsync to exclude certain folder(s). 3

rsync -avh --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude

Warning

The excluded directory paths are relative to the sourcefolder.

Copy files over SSH

See SSH

Last updated on