You need a Linux machine, or any terminal on macOS — nothing
is installed and nothing is bought. Every command on this page is
read-only or works inside a practice folder you create and can delete
afterwards, and none of them needs sudo. If
you have never opened a terminal at all, start with
Introduction to the
Linux Terminal, which shows you where it is and how to get your
bearings.
Creating Files and Directories
Before you can work with files, you need to know how to create them. Linux provides simple commands for creating both empty files and new directories.
Creating Files with touch
The touch command creates a new, empty file. If the file already exists,
it updates the file's modification timestamp without changing its contents.
# Create a single file
touch notes.txt
# Create multiple files at once
touch file1.txt file2.txt file3.txt
# Verify the files were created
ls -l
Creating Directories with mkdir
The mkdir (make directory) command creates new folders. Use the -p
flag to create nested directories in one step.
# Create a single directory
mkdir projects
# Create nested directories (parent + child)
mkdir -p projects/website/css
# Without -p, this would fail if "projects" didn't exist
Always use mkdir -p when creating nested directories. Without it,
mkdir will fail if any parent directory in the path does not already
exist. With -p, it creates the entire chain silently.
Copying Files and Directories
The cp (copy) command duplicates files and directories. The original
remains untouched, and a new copy is created at the destination.
Copying Files
# Copy a file to a new name in the same directory
cp notes.txt notes-backup.txt
# Copy a file into another directory
cp notes.txt ~/Documents/
# Copy a file into another directory with a new name
cp notes.txt ~/Documents/my-notes.txt
Copying Directories
To copy an entire directory and everything inside it, you must use the -r
(recursive) flag. Without it, cp will refuse to copy a directory.
# Copy a directory and all its contents
cp -r projects projects-backup
# Copy into a specific location
cp -r projects ~/Documents/projects-copy
Use cp -a instead of cp -r when you want to preserve
permissions, timestamps, and ownership. The -a flag is short for
"archive" and is the best choice for making exact copies.
Moving and Renaming
The mv (move) command does double duty: it moves files to a new location
and renames files. Unlike cp, the original is removed -- the file is
relocated, not duplicated.
Moving Files
# Move a file to another directory
mv notes.txt ~/Documents/
# Move multiple files into a directory
mv file1.txt file2.txt file3.txt ~/Documents/
Renaming Files
Renaming is just moving a file to a new name in the same directory. There is no
separate "rename" command in basic Linux -- mv handles it.
# Rename a file
mv old-name.txt new-name.txt
# Rename a directory
mv old-folder new-folder
If the destination file already exists, mv will silently overwrite
it. Use mv -i (interactive mode) to get a confirmation prompt before
overwriting any existing file.
Deleting Files and Directories
The rm (remove) command deletes files, and rmdir removes
empty directories. These operations are permanent on Linux -- there is no trash can
in the terminal.
Deleting Files
# Delete a single file
rm notes.txt
# Delete with confirmation prompt
rm -i notes.txt
# rm: remove regular file 'notes.txt'? y
# Delete multiple files
rm file1.txt file2.txt file3.txt
Deleting Directories
# Remove an empty directory
rmdir empty-folder
# Remove a directory and everything inside it
rm -r projects
# Remove with confirmation for each file
rm -ri projects
The command rm -rf deletes everything recursively without asking
for confirmation. A typo like rm -rf / or rm -rf ~ can
destroy your entire system or home directory in seconds. Always double-check
the path before running any rm command, and prefer rm -ri
when deleting directories until you are confident.
Using Wildcards
Wildcards (also called globs) let you match multiple files at once, making bulk operations much faster. The shell expands the wildcard into a list of matching file names before the command runs.
*.txt matches all text files.
file?.txt matches file1.txt but not file10.txt.
# Copy all .txt files to Documents
cp *.txt ~/Documents/
# Delete all .log files
rm *.log
# Move all files starting with "report"
mv report* ~/Documents/
# List all .jpg and .png files
ls *.jpg *.png
Before running rm *.something, first run ls *.something
to see exactly which files match. This lets you verify the wildcard catches the
right files before you commit to deleting them.
Now Do It Yourself: Five Steps
Copying, renaming and deleting are the commands you will use every day — and two of them can destroy work silently, with no confirmation and no recycle bin. You will use all of them safely on practice files, and see exactly how the destruction happens so it never happens to you by surprise.
Go: open a terminal, run cd, then mkdir -p practice and cd practice.
Do: run these three commands.
mkdir -p docs old
printf 'version 1\n' > docs/notes.txt
ls docs
You should see: notes.txt. Two folders now exist and one holds a file with a line of text in it.
If not: if ls docs shows nothing, the printf wrote somewhere else — check with pwd that you are in practice. Everything below stays inside this folder.
Go: same folder.
Do: run these four commands one at a time.
cp docs/notes.txt docs/notes-backup.txt
ls docs
mv docs/notes-backup.txt docs/archive.txt
ls docs
You should see: two files after the copy, then the same two with the second one renamed to archive.txt. cp leaves the original in place; mv does not, which is why the same command both renames and moves — a rename is just a move to a new name in the same folder.
If not: cp: cannot stat 'docs/notes.txt': No such file or directory means step 1 did not complete. The word stat in that message simply means "look at" — it could not find the source.
Go: same folder. This is the most important step on the page.
Do: run these four commands.
printf 'NEW\n' > a.txt
printf 'OLD\n' > b.txt
mv a.txt b.txt
cat b.txt
You should see: NEW. The file called b.txt that contained OLD is gone — overwritten, with no warning, no prompt and nothing in a bin to recover. cp behaves identically.
If not: if you were asked to confirm, your account has mv set up as mv -i already, which some distributions do. Do not rely on that being true anywhere else. The safe habit is to ask for it yourself: cp -i and mv -i prompt before overwriting, and answering anything other than y cancels.
Go: same folder.
Do: run cp docs docs2, which is meant to fail, then cp -r docs docs2, then ls docs2.
You should see: cp: -r not specified; omitting directory 'docs', then no output, then both files — archive.txt and notes.txt — because docs has held two since step 2 and a recursive copy brings everything. -r means recursive: act on the folder and everything inside it. cp refuses folders without it rather than guessing, which is a kindness — the same flag on rm has no such safety net.
If not: if the first command succeeds silently, your system aliases cp to include -r. Check what a command really is with type cp — aliases are a common reason a command behaves differently on your machine than in a tutorial.
Go: same folder.
Do: run rmdir docs2, which is meant to fail, then rm -r docs2. Then run rm nothere.txt.
You should see: rmdir: failed to remove 'docs2': Directory not empty — it refuses precisely because the copy in step 4 put two files in there, then silence as rm -r removes it, then rm: cannot remove 'nothere.txt': No such file or directory.
If not: the lesson is in the contrast. rmdir only removes an empty folder, so it physically cannot destroy work — reach for it first, and let it refuse. rm -r deletes everything beneath a folder instantly and permanently. ⚠️ Before any rm -r: run pwd, then run ls on the exact target and read what is listed. Never combine rm -r with a wildcard you have not first tested by putting ls in front of it instead.
Without scrolling up: you are about to run rm -r on a folder whose name
you typed from memory. What two commands do you run first, and why?
Answer: pwd to confirm where you are, and ls on the exact
target to see what would be destroyed. There is no undo and no recycle bin.
Now do it without the page: make a backup copy of the whole
docs folder named docs-backup, confirm it contains the same
file, then delete it using the safest command that can do the job. Steps 4 and 5, with
you choosing the tool.
Summary
In this tutorial, you learned how to manage files and directories from the terminal:
- touch -- Create empty files or update timestamps
- mkdir / mkdir -p -- Create directories and nested directory trees
- cp / cp -r -- Copy files and directories
- mv -- Move or rename files and directories
- rm / rm -r -- Delete files and directories (permanently)
- Wildcards -- Match multiple files with *, ?, and []
You now have the fundamental skills to create, organize, and clean up files
from the command line. Next, you will learn how to read and inspect file contents
using commands like cat, less, and tail.