Bird
Raised Fist0
Linux CLIscripting~7 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the primary difference between scp and rsync when transferring files?
easy
A. scp requires no authentication, rsync always does.
B. scp is used only for local copies, rsync only for remote.
C. scp compresses files automatically, rsync does not.
D. scp copies files directly, while rsync syncs only changed parts.

Solution

  1. Step 1: Understand scp behavior

    scp copies entire files securely from one machine to another without checking differences.
  2. Step 2: Understand rsync behavior

    rsync compares source and destination and transfers only changed parts, making it efficient for syncing.
  3. Final Answer:

    scp copies files directly, while rsync syncs only changed parts. -> Option D
  4. Quick Check:

    Difference in transfer method = C [OK]
Hint: Remember: rsync syncs changes, scp copies whole files [OK]
Common Mistakes:
  • Thinking scp syncs files like rsync
  • Believing scp works only locally
  • Assuming rsync doesn't need authentication
2. Which of the following is the correct syntax to copy a file named report.txt from your local machine to a remote server using scp?
easy
A. scp report.txt user@remote:/home/user/
B. scp user@remote:/home/user/ report.txt
C. scp -r report.txt user@remote:/home/user/
D. scp /home/user/report.txt user@remote:

Solution

  1. Step 1: Identify source and destination in scp

    Syntax is scp source destination. Here, source is local file report.txt.
  2. Step 2: Check destination format

    Destination must be remote user and path: user@remote:/home/user/. scp report.txt user@remote:/home/user/ matches this.
  3. Final Answer:

    scp report.txt user@remote:/home/user/ -> Option A
  4. Quick Check:

    Local to remote copy syntax = A [OK]
Hint: Remember: scp source destination, remote uses user@host:path [OK]
Common Mistakes:
  • Reversing source and destination
  • Using -r for single file copy
  • Omitting user@host in remote path
3. What will be the output of this command if the directory backup/ exists on both local and remote, but only one file data.txt changed locally?
rsync -avz backup/ user@remote:/home/user/backup/
medium
A. Only the changed data.txt file will be transferred to remote.
B. All files in backup/ will be transferred again.
C. The command will fail due to missing -r option.
D. The remote directory will be deleted and replaced.

Solution

  1. Step 1: Understand rsync -avz flags

    -a is archive (recursive + preserve), -v verbose, -z compress during transfer.
  2. Step 2: Behavior with existing directories

    rsync compares files and transfers only changed files, so only data.txt will be sent.
  3. Final Answer:

    Only the changed data.txt file will be transferred to remote. -> Option A
  4. Quick Check:

    rsync syncs changed files only = D [OK]
Hint: rsync with -a syncs only changed files, not all [OK]
Common Mistakes:
  • Thinking rsync copies all files every time
  • Believing -r is needed separately with -a
  • Assuming remote directory is deleted
4. You run this command to copy a directory:
scp -r myfolder user@remote:/home/user/
But you get an error: scp: command not found. What is the most likely cause?
medium
A. You forgot to add the -v verbose flag.
B. The scp command is not installed on the local machine.
C. The remote path is incorrect and missing a trailing slash.
D. You need to use rsync instead of scp for directories.

Solution

  1. Step 1: Analyze the error message

    scp: command not found means the local system cannot find the scp program.
  2. Step 2: Check other options

    Verbose flag or remote path errors cause different errors; scp supports directories with -r.
  3. Final Answer:

    The scp command is not installed on the local machine. -> Option B
  4. Quick Check:

    Command not found means missing program = A [OK]
Hint: Command not found means program missing locally [OK]
Common Mistakes:
  • Assuming -v fixes command not found
  • Thinking remote path syntax causes this error
  • Believing scp can't copy directories
5. You want to efficiently backup a large folder project/ from your local machine to a remote server daily. Which command best ensures only new or changed files transfer, compresses data, and preserves permissions?
hard
A. scp -r project/ user@remote:/backup/project/
B. scp project/* user@remote:/backup/project/
C. rsync -avz project/ user@remote:/backup/project/
D. rsync -r project/ user@remote:/backup/project/

Solution

  1. Step 1: Identify requirements

    Need to transfer only new/changed files, compress data, and preserve permissions.
  2. Step 2: Match command options

    rsync -avz means archive (preserves permissions, recursive), verbose, and compress during transfer, ideal for backups.
  3. Step 3: Compare other options

    scp -r copies all files every time without compression; rsync -r lacks archive and compression flags.
  4. Final Answer:

    rsync -avz project/ user@remote:/backup/project/ -> Option C
  5. Quick Check:

    Efficient backup with compression and permissions = B [OK]
Hint: Use rsync -avz for efficient, compressed, permission-preserving backups [OK]
Common Mistakes:
  • Using scp for large repeated backups
  • Omitting -a flag in rsync losing permissions
  • Not compressing data during transfer
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard