CodeTheRobot

Ubuntu Linux for Robotics · Lesson 05 of 96

Files and folders: mkdir, cp, mv and rm

Creating, copying, moving and deleting safely, with -r, -i and -p.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain files and folders: mkdir, cp, mv and rm 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

mkdir creates folders (-p creates the whole path), cp copies (-r for folders), mv moves or renames, and rm deletes. Linux has no recycle bin: rm is permanent, so use rm -i to be asked first, list with ls before deleting, and never run rm -rf with a path you have not checked.

Wildcards let you act on many files: *.txt matches every text file. The shell expands the pattern before running the command, so run ls with the same pattern first to see exactly what it matches.

Guided lesson

Work through these steps

  1. 1.

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

  2. 2.

    Read the files and folders: mkdir, cp, mv and rm 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

mkdir -p ~/robot_ws/src
# Create the workspace and its src folder in one command.
cd ~/robot_ws
# Move into it.
echo 'notes' > a.txt
# Create a small file.
cp a.txt src/b.txt
# Copy it into src under a new name.
mv a.txt old.txt
# Rename the original (mv also moves files between folders).
ls *.txt
# Check what the pattern matches before deleting.
rm -i old.txt
# Delete it, asking for confirmation first.

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 Files and folders: mkdir, cp, mv and rm. Expected result: ws/old.txt ws/src/b.txt

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.

d=$(mktemp -d) && cd "$d"
mkdir -p ws/src
echo hi > a.txt
# TODO: copy a.txt to ws/src/b.txt, then move a.txt to ws/old.txt
find ws -type f | sort

Expected output

ws/old.txt
ws/src/b.txt
Stuck? Show the full solution (try it yourself first)
d=$(mktemp -d) && cd "$d"
mkdir -p ws/src
echo hi > a.txt
cp a.txt ws/src/b.txt
mv a.txt ws/old.txt
find ws -type f | sort

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

Create a folder tree ws/src/pkg1 and ws/src/pkg2, copy a file into both, rename one of them, delete the other with rm -i, and print the final tree with find.

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.