Book 2 — The Magic World of the Terminal

Python for All

Chapter Three — Managing Files and Folders

Thanasis Troboukis  ·  All Books

Book Two · Chapter Three

Managing Files and Folders

Create, move, copy, and delete — the four operations that let you organise your entire workspace from the command line.

Creating — touch and mkdir

You already know mkdir from Chapter 1. Let us look at both creation commands together.

mkdir (make directory) creates a new, empty folder. Give it the name you want:

mkdir data_journalism
mkdir sources
mkdir 2026-03-04_city_hall_documents

If you try to create a directory that already exists, mkdir will give you an error. That is intentional — the terminal never overwrites silently.

touch creates an empty file. If the file already exists, it updates the file's timestamp without changing its content. It is often used to create placeholder files before filling them in a text editor:

touch notes.txt
touch interview_2026-03-04.txt
touch analysis.py

After touch, the file exists but is empty. You then open it in VS Code (or any editor) and fill in the content.

Terminal · Creating Files and Folders

          

$

Try: mkdir data_journalism → ls → cd data_journalism → touch notes.txt → mkdir sources → ls

mv — Moving and Renaming

mv stands for move. It has two uses that at first seem different but are actually the same operation:

Moving a file to another directory:

mv Downloads/interview_notes.txt Files/data_journalism/

This takes interview_notes.txt from Downloads and places it inside Files/data_journalism. The original is gone — mv does not make a copy.

Renaming a file:

mv notes.txt vocabulary.txt

This renames notes.txt to vocabulary.txt in the same directory. Moving a file within the same folder is renaming it. That is why mv handles both.

mv works on directories too:

mv old_folder new_folder_name
Important: if the destination already exists as a file, mv will overwrite it without warning. Double-check your paths before moving. Use ls to verify source and destination before running mv.
Terminal · mv Lab

          

$

Try: ls Downloads → mv Downloads/interview_notes.txt Files/data_journalism/ → ls Files/data_journalism

cp — Copying Files

cp stands for copy. It works just like mv, except the original file stays where it was. After cp, you have two copies: the original and the new one at the destination.

cp Downloads/data_raw.csv Files/data_journalism/data_raw.csv

This copies data_raw.csv from Downloads into data_journalism. The original in Downloads is untouched.

You can also copy and rename in the same step by giving the destination a different name:

cp data_raw.csv data_clean.csv

This creates a copy called data_clean.csv in the same directory. A common pattern: copy the original before cleaning it, so you always have the raw data to fall back on.

When to use cp vs mv: use cp when you want to keep the original in place — for example, backing up raw data before processing it. Use mv when you want to reorganise or rename without duplication.
Terminal · cp Lab

          

$

Try: cp Downloads/data_raw.csv Files/data_journalism/data_raw.csv → ls Files/data_journalism → ls Downloads (original still there?)

rm and rmdir — Deleting

rm stands for remove. It deletes a file permanently. There is no Trash, no undo. The file is gone.

rm old_draft.txt

rmdir removes a directory — but only if it is completely empty. This is a safety feature: the terminal will refuse to silently delete a folder that still has content.

rmdir empty_folder

If the directory is not empty, you will get an error: Directory not empty. You must first remove the files inside before you can remove the folder.

The most dangerous command: rm -rf (remove, recursive, force) deletes a directory and everything inside it — instantly and permanently. Never run it unless you are absolutely certain. There are countless stories of developers accidentally running rm -rf / and wiping an entire system. In the sandbox it is disabled for safety. In real life, treat it with great caution.
Terminal · rm and rmdir Lab

          

$

Try: ls → rm draft_one.txt → rm draft_two.txt → rmdir empty_folder → ls · Try: rmdir sources (will it work?)

Practice — A Journalism Workflow

You have just received two files in your Downloads folder: a raw CSV dataset and a set of interview notes. Your task is to organise them into a proper project structure using only terminal commands.

The target structure you are building:

Files/data_journalism/
├── data/
│   └── air_quality.csv
└── notes/
    └── interview_2026-03-04.txt

Steps to complete:

  1. Navigate into Files/data_journalism
  2. Create two new directories: data and notes
  3. Copy data_raw.csv from Downloads into data/ and rename it air_quality.csv
  4. Move interview_notes.txt from Downloads into notes/ and rename it interview_2026-03-04.txt
  5. Verify the structure with ls data and ls notes
Terminal · Journalism Workflow Challenge

          

$

Goal: build Files/data_journalism/data/ and Files/data_journalism/notes/ with the right files inside

What you have learned in Chapter 3: touch creates empty files; mkdir creates directories; mv moves and renames files and folders; cp copies files while leaving the original intact; rm deletes files permanently and rmdir removes empty directories. In Chapter 4 you will learn to read and search inside files without ever opening a text editor.

Chapter Navigation

Move between chapters.