CodeTheRobot

Ubuntu Linux for Robotics · Lesson 14 of 96

SCP and SFTP: file transfer

Copying files and folders to and from a robot with scp, sftp and rsync.

Estimated time · 30–45 min

Learning goals

What you will understand

  • Explain scp and sftp: file transfer in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

scp copies files over SSH: scp file user@host:/path uploads and scp user@host:/path file downloads (-r for folders). sftp gives an interactive session with get and put commands. Both use the same login and keys as SSH.

For repeated transfers use rsync -avz, which only sends what changed and can resume. Never copy your whole workspace including build/ and install/: send only the source (or the installed result for deployment), and check file ownership after copying.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the scp and sftp: file transfer commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

scp robot_bringup.tar.gz robot@192.168.1.50:/home/robot/
# Upload a file to the robot's home folder.
scp robot@192.168.1.50:/home/robot/log.txt .
# Download a file to the current folder.
scp -r ~/robot_ws/src robot@192.168.1.50:~/robot_ws/
# Upload a whole folder.
sftp robot@192.168.1.50
# Start an interactive file-transfer session (use put, get, ls, exit).
rsync -avz --exclude build --exclude install --exclude log ~/robot_ws/ robot@192.168.1.50:~/robot_ws/
# Sync the workspace, skipping generated folders.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · The robot's computer

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

robot computercpumemdiskdriverstoppedplannerstoppedloggerstoppedsafetystoppedTHE ROBOT'S COMPUTER

Demo for SCP and SFTP: file transfer. Expected result: The workspace source is copied to the robot without build, install and log

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: rsync the folder ~/robot_ws/ to robot@192.168.1.50:~/robot_ws/ with -avz, excluding build, install and log

Expected output

The workspace source is copied to the robot without build, install and log
Stuck? Show the full solution (try it yourself first)
rsync -avz --exclude build --exclude install --exclude log ~/robot_ws/ robot@192.168.1.50:~/robot_ws/

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Copy a log file from a machine to your laptop with scp, sync a small workspace with rsync excluding build folders, and time both. Run the rsync twice and compare the second run.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.