Skip to content
Copy files

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

How to copy files through the secure shell (SSH).

tar

tar cvf - $localdir | ssh someone@somemachine '(cd destdir && tar xBf -)'

rsync

Use a ssh remote as a source or destination folder.

rsync -avh /source/folder/ username@nasip:dest/folder/

Secure FTP (SFTP)

Filezilla and WinSCP can access remote servers via the Secure FTP (SFTP) protocol.

Mount remote directory as a disk via sshfs

https://github.com/libfuse/sshfs mounts a remote machine’s directory as a local disk.

sshfs [user@]hostname:[directory] mount-point

To unmount the directory after file operations are done:

fusermount -u mount-point
Last updated on