Skip to content

Copy files

How to copy files with style.

Use tar

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

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

Use rsync

rsync -avh --info=progress2 sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude

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).

scp

scp works similar to the regular copy (cp) command.4

scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2

tar, pipe, and ssh commands

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

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

Windows CLI tools for copying

Comments