Managing Files and Folders: mkdir, cp, mv and rm
Create folders and files, copy, move and rename them, delete safely, and select many files at once with wildcards.
On this page 11 sections
Introduction
Now that you can move around the file system, it is time to change it. Five commands cover almost everything you do with files in a file manager — new folder, new file, copy, move or rename, and delete — and they work the same on every Linux system.
- How to create folders, including nested ones, with mkdir -p.
- How to copy files and whole folders with cp and cp -r.
- How mv both moves and renames.
- How to delete safely with rm, and select many files with wildcards.
Creating Folders: mkdir
mkdir noteslsClick Run to see what this code prints.
To create several levels at once, add -p (parents). Without it, mkdir refuses if a parent folder is missing:
mkdir projects/shop/productsClick Run to see what this code prints.
mkdir -p projects/blog/poststree projectsClick Run to see what this code prints.
Creating Files: touch
touch creates an empty file (or, if it already exists, updates its modification time). You can name several at once:
cd notestouch ideas.txt todo.txt meeting-2026-10-24.txtlsClick Run to see what this code prints.
Copying: cp
cp source destination copies a file:
cp todo.txt todo-backup.txtlsClick Run to see what this code prints.
Copying a folder needs -r (recursive), so the folder and everything inside it are copied. Back in her home folder, Priya tries without it first:
cp notes notes-copyClick Run to see what this code prints.
cp -r notes notes-copylsClick Run to see what this code prints.
If the destination file already exists, cp and mv replace it without asking. Add -i (interactive) to be asked first, or -n to never overwrite.
Moving and Renaming: mv
Linux has no separate rename command: renaming is moving a file to a new name in the same folder.
mv ideas.txt project-ideas.txtlsClick Run to see what this code prints.
Give mv a folder as the destination to move a file into it:
mv meeting-2026-10-24.txt ../Documents/lsls ../DocumentsClick Run to see what this code prints.
Unlike cp, mv works on folders without any extra option, because moving a folder just changes where it lives.
You run mv report.txt final-report.txt and no file called final-report.txt existed. What happens?
Wildcards
Wildcards let one command work on many files. The shell expands them into the matching names before the command runs:
| Pattern | Matches |
|---|---|
| * | Any characters (including none): *.log matches every .log file |
| ? | Exactly one character: report-?.txt matches report-1.txt but not report-10.txt |
| [abc] | One of the listed characters: file[12].txt matches file1.txt and file2.txt |
touch a.log b.log c.logls *.logls *backup*Click Run to see what this code prints.
Before running rm *.log, run ls *.log. You will see exactly which files the pattern matches, without deleting anything.
Deleting: rm and rmdir
Files deleted with rm are gone immediately. They do not go to a trash folder and normally cannot be recovered. Read every rm command twice before pressing Enter.
rm a.log b.log c.loglsClick Run to see what this code prints.
Like cp, rm needs -r for folders. Without it, it refuses:
rm notes-copyClick Run to see what this code prints.
rm -r notes-copylsClick Run to see what this code prints.
-i asks before each deletion — answer y to delete or n to keep the file. rmdir deletes only empty folders, which makes it a safe choice when you expect a folder to be empty:
mkdir empty-folderrmdir empty-folderrmdir projectsClick Run to see what this code prints.
Priya wants to delete the old log files in her current folder and types rm -r * .log (with a space before .log). What is the problem?
Common Mistakes
- Running rm -rf on a path you have not checked — there is no undo.
- Overwriting a file with cp or mv because the destination name already existed.
- Forgetting -r when copying a folder, then wondering why only files were copied.
- Leaving a space inside a wildcard pattern by accident.
Frequently Asked Questions
-r deletes folders and everything in them, and -f (force) never asks and ignores missing files. It is powerful and dangerous; use it only when you are certain of the path.
Tools such as trash-cli add a trash command that moves files to your desktop’s trash instead of deleting them, but they are not standard. Good backups (lesson 12 and the backup project) are the real safety net.
With scp or rsync over SSH, covered in lesson 13.
Key Takeaways
- mkdir creates folders; mkdir -p creates missing parents too. touch creates empty files.
- cp copies (cp -r for folders); mv moves and renames.
- cp and mv overwrite without asking unless you add -i or -n.
- Wildcards (*, ?, [ ]) select many files — test them with ls first.
- rm deletes permanently; rm -r deletes folders; rmdir deletes only empty folders.
Summary
You can now organise files entirely from the terminal. So far the files have been empty — next you will look inside them, write text into them and edit them with a terminal text editor.