0
0
Linux CLIscripting~7 mins

scp and rsync for file transfer in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to move files between computers safely and efficiently. scp and rsync are tools that help you copy files over a network, making sure your data gets where it needs to go.
When you want to copy a file from your laptop to a remote server quickly and securely.
When you need to back up a folder from one computer to another and keep the files in sync.
When you want to transfer files but only update the parts that changed to save time.
When you want to copy files over SSH without setting up extra services.
When you want to resume a file transfer if it was interrupted.
Commands
This command copies the file 'document.txt' from your local computer to the remote computer at IP 192.168.1.10 in the user's home directory. It uses SSH to transfer securely.
Terminal
scp /home/user/document.txt user@192.168.1.10:/home/user/
Expected OutputExpected
document.txt 100% 12KB 12.0KB/s 00:01
This command copies the entire 'photos' folder recursively to the remote user's home directory. The -r flag means copy folders and their contents.
Terminal
scp -r /home/user/photos user@192.168.1.10:/home/user/
Expected OutputExpected
photos/file1.jpg 100% 500KB 500.0KB/s 00:01 photos/file2.jpg 100% 450KB 450.0KB/s 00:01
-r - Copy directories recursively
This command syncs the 'project' folder to the remote 'project_backup' folder. It copies only changed files, preserves permissions, and compresses data during transfer.
Terminal
rsync -avz /home/user/project/ user@192.168.1.10:/home/user/project_backup/
Expected OutputExpected
sending incremental file list file1.txt file2.txt sent 1.2M bytes received 45 bytes 800.00K bytes/sec total size is 1.1M speedup is 0.92
-a - Archive mode to preserve permissions and timestamps
-v - Verbose output to see progress
-z - Compress data during transfer
This command copies a large file showing progress. It helps track transfer status for big files.
Terminal
rsync -avz --progress /home/user/largefile.iso user@192.168.1.10:/home/user/
Expected OutputExpected
largefile.iso 100% 700MB 10.0MB/s 00:01
--progress - Show progress during transfer
Key Concept

If you remember nothing else, remember: scp copies files securely and simply, while rsync efficiently syncs files by copying only what changed.

Common Mistakes
Forgetting the -r flag when copying folders with scp
scp will fail or only copy the folder itself without contents
Always add -r to copy directories recursively with scp
Using rsync without a trailing slash on the source folder
rsync copies the folder itself instead of its contents, causing unexpected folder nesting
Add a trailing slash to the source path to copy only contents, e.g., /home/user/project/
Not specifying the remote username in scp or rsync commands
The command may fail or try to connect with the wrong user
Always include user@ before the remote IP or hostname
Summary
scp copies files or folders securely over SSH with simple commands.
rsync syncs files efficiently by copying only changes and can compress data.
Use -r with scp for folders and trailing slashes with rsync to control folder copying.