Bird
Raised Fist0
Linux CLIscripting~10 mins

SCP for file transfer in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - SCP for file transfer
Start SCP command
↓
Check source file exists
↓
Establish SSH connection
↓
Transfer file data
↓
Write file on destination
↓
Close connection
↓
End with success or error
This flow shows how SCP checks the source, connects securely, transfers the file, and finishes.
Execution Sample
Linux CLI
scp /home/user/file.txt user@remote:/home/user/
This command copies file.txt from local to remote user's home directory.
Execution Table
StepActionEvaluationResult
1Start SCP commandscp /home/user/file.txt user@remote:/home/user/Command initiated
2Check source file/home/user/file.txt exists?Yes, file found
3Establish SSH connectionConnect to user@remoteConnection successful
4Transfer file dataSend file.txt bytesData sent successfully
5Write file on destinationWrite to /home/user/file.txtFile written successfully
6Close connectionEnd SSH sessionConnection closed
7EndTransfer completeSuccess
💡 Transfer ends after file is written and connection closes successfully
Variable Tracker
VariableStartAfter Step 2After Step 4Final
source_file/home/user/file.txtExistsData readTransfer complete
ssh_connectionNoneNoneEstablishedClosed
transfer_statusNot startedNot startedIn progressSuccess
Key Moments - 3 Insights
What happens if the source file does not exist?
SCP stops at Step 2 with an error because it cannot find the file to send.
Why is SSH connection important in SCP?
At Step 3, SCP uses SSH to securely connect and transfer files safely.
When does SCP close the connection?
After the file is fully transferred and written (Step 6), SCP closes the SSH connection.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the file data actually sent?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Check the 'Action' column for when data transfer happens.
At which step does SCP check if the source file exists?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look for the step mentioning source file check.
If the SSH connection fails, which step would SCP not reach?
AStep 3
BStep 4
CStep 6
DStep 7
💡 Hint
Without connection, data transfer cannot start.
Concept Snapshot
scp [source] [user@host:destination]

- Copies files securely over SSH
- Checks source file before transfer
- Establishes SSH connection
- Transfers file data
- Writes file on remote
- Closes connection after transfer
Full Transcript
SCP is a command to copy files securely between computers using SSH. First, it checks if the source file exists locally. Then, it connects to the remote computer using SSH. After connection, it sends the file data over the secure channel. The remote side writes the file to the destination path. Finally, SCP closes the connection and ends with success or error. This process ensures safe and reliable file transfer.

Practice

(1/5)
1. What does the scp command do in Linux?
easy
A. Changes file permissions
B. Compresses files into a zip archive
C. Securely copies files between local and remote machines
D. Displays the contents of a file

Solution

  1. Step 1: Understand the purpose of scp

    The scp command is designed to securely transfer files between computers over a network.
  2. Step 2: Compare with other options

    Options A, B, and C describe different commands: permission changes, compression, viewing files, which are unrelated to scp.
  3. Final Answer:

    Securely copies files between local and remote machines -> Option C
  4. Quick Check:

    scp = Secure copy [OK]
Hint: Remember: scp = secure copy files remotely [OK]
Common Mistakes:
  • Confusing scp with compression commands
  • Thinking scp changes file permissions
  • Assuming scp only works locally
2. Which of the following is the correct syntax to copy a file file.txt from your local machine to a remote server server.com under user alice in the directory /home/alice/?
easy
A. scp file.txt alice@server.com:/home/alice/
B. scp alice@server.com:/home/alice/ file.txt
C. scp /home/alice/file.txt alice@server.com:
D. scp file.txt /home/alice/alice@server.com

Solution

  1. Step 1: Identify source and destination in scp syntax

    The syntax is scp source destination. Here, file.txt is local source, and alice@server.com:/home/alice/ is remote destination.
  2. Step 2: Check each option

    scp file.txt alice@server.com:/home/alice/ correctly places local file first and remote path second. scp alice@server.com:/home/alice/ file.txt reverses source and destination. scp /home/alice/file.txt alice@server.com: uses remote path as source incorrectly. scp file.txt /home/alice/alice@server.com has wrong path format.
  3. Final Answer:

    scp file.txt alice@server.com:/home/alice/ -> Option A
  4. Quick Check:

    scp source destination = scp file.txt alice@server.com:/home/alice/ [OK]
Hint: Local file first, remote user@host:path second [OK]
Common Mistakes:
  • Swapping source and destination
  • Missing colon after remote host
  • Incorrect path order
3. What will be the output or result of this command?
scp -r alice@server.com:/var/logs ./backup_logs
Assuming you have access and the remote directory exists.
medium
A. Copies the remote /var/logs directory recursively to local ./backup_logs folder
B. Copies only files inside /var/logs but not subdirectories
C. Throws an error because -r cannot be used with remote paths
D. Deletes the local ./backup_logs directory

Solution

  1. Step 1: Understand the -r option in scp

    The -r flag means recursive copy, so it copies directories and their contents.
  2. Step 2: Analyze the command effect

    The command copies the remote directory /var/logs from user alice on server.com to the local folder ./backup_logs. This is valid and copies all files and subfolders.
  3. Final Answer:

    Copies the remote /var/logs directory recursively to local ./backup_logs folder -> Option A
  4. Quick Check:

    scp -r remote:/path local copies folders recursively [OK]
Hint: Use -r to copy folders recursively from remote [OK]
Common Mistakes:
  • Thinking -r only copies files, not folders
  • Believing -r is invalid with remote paths
  • Confusing copy direction
4. You run this command to copy a file from remote to local:
scp alice@server.com/home/alice/file.txt ./
But it fails with an error. What is the likely problem?
medium
A. scp cannot copy files from remote to local
B. Missing colon after the remote host before the path
C. Local path ./ is invalid
D. User alice does not have permission to copy files

Solution

  1. Step 1: Check the remote path syntax

    Remote paths must be specified as user@host:/path with a colon after the host. The command misses the colon after server.com.
  2. Step 2: Understand the error cause

    Without the colon, scp treats the whole string as a local path, causing failure.
  3. Final Answer:

    Missing colon after the remote host before the path -> Option B
  4. Quick Check:

    Remote path needs colon after host [OK]
Hint: Always put colon after remote host in scp paths [OK]
Common Mistakes:
  • Omitting colon after remote host
  • Assuming local path is wrong
  • Thinking scp can't copy remote files
5. You want to copy multiple files file1.txt and file2.txt from your local machine to the remote directory /data/uploads on server example.com as user bob. Which command correctly does this in one line?
hard
A. scp file1.txt,bob@example.com:/data/uploads/ file2.txt
B. scp file1.txt file2.txt -r bob@example.com:/data/uploads/
C. scp bob@example.com:/data/uploads/ file1.txt file2.txt
D. scp file1.txt file2.txt bob@example.com:/data/uploads/

Solution

  1. Step 1: Understand copying multiple files with scp

    scp supports copying multiple files by listing them before the destination path.
  2. Step 2: Analyze each option

    scp file1.txt file2.txt bob@example.com:/data/uploads/ correctly lists both files followed by remote destination. scp file1.txt file2.txt -r bob@example.com:/data/uploads/ misplaces the -r flag after the sources, treating "-r" as a non-existent source file. scp bob@example.com:/data/uploads/ file1.txt file2.txt reverses source and destination. scp file1.txt,bob@example.com:/data/uploads/ file2.txt has invalid syntax with comma.
  3. Final Answer:

    scp file1.txt file2.txt bob@example.com:/data/uploads/ -> Option D
  4. Quick Check:

    List files then remote destination [OK]
Hint: List all files first, then remote destination [OK]
Common Mistakes:
  • Misplacing the -r flag after source files
  • Swapping source and destination
  • Incorrect file list syntax
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