What you'll learn
- Create, copy, move and delete files safely
- View files with cat, less, head and tail
- Preview wildcard matches with ls before acting on them
- Check the path before rm, because the terminal has no recycle bin
Create, view, copy, move, rename and delete files and folders from the terminal without losing anything by accident.
Why it matters
Setting up a project, organising data files, cleaning up build output, moving logs on a server: all of it is file management, and the terminal does it faster and more repeatably than clicking. It is also less forgiving. The terminal has no recycle bin: a deleted file is gone. This lesson covers the commands and the habits that keep you safe.
Safety rule for this course: practise only inside ~/cli-practice. Never use sudo for these commands, and never aim rm at your home folder or /.
Making folders and files
$ cd ~/cli-practice
$ mkdir project # one folder
$ mkdir -p project/data/raw # -p creates the parent folders too
$ touch project/notes.txt # empty file (or updates its timestamp)
$ echo "first line" > project/todo.txt # file with content (more on > next lesson)Looking at files
$ cat project/todo.txt # print the whole file
first line
$ less bigfile.txt # page through: Space = next page, q = quit
$ head -n 5 data.csv # first 5 lines
$ tail -n 5 app.log # last 5 linescat is fine for short files; use less or head for long ones.
Copying: cp
$ cp project/todo.txt project/todo-backup.txt # copy a file
$ cp project/todo.txt project/data/ # copy into a folder (keeps the name)
$ cp -r project project-copy # -r copies a folder and everything in itMoving and renaming: mv
mv does both. Moving to a new name in the same folder is renaming:
$ mv project/notes.txt project/meeting-notes.txt # rename
$ mv project/todo-backup.txt project/data/ # move into a folderCareful: if the destination file already exists, cp and mv overwrite it without asking. Add -i (interactive) to be asked first: mv -i a.txt b.txt.
Deleting: rm and rmdir
$ rm project/data/todo-backup.txt # delete a file, permanently
$ rmdir project/data/raw # delete an EMPTY folder
$ rm -r project-copy # delete a folder and everything inside
$ rm -i *.tmp # ask before each deletionrm -r is the most dangerous everyday command. Before running it:
- Run
pwdto confirm where you are. - Run
lswith the same path or pattern first (ls project-copy) to see exactly what would go. - Prefer specific paths over wildcardsPatterns such as *.csv or data?.txt that the shell expands to matching file names before the command runs.; use
-iwhen unsure.
You will see rm -rf (force, no questions) in tutorials. Avoid it while learning.
Wildcards
The shell expands patterns before the command runs:
| Pattern | Matches |
|---|---|
*.csv | every name ending in .csv |
report-* | names starting with report- |
data?.txt | data1.txt, dataA.txt (exactly one character) |
$ ls *.csv # preview first
$ mv *.csv data/ # then actWorked example: organise a messy folder
$ cd ~/cli-practice
$ mkdir -p tidy && cd tidy
$ touch jan.csv feb.csv mar.csv notes.txt draft.tmp old.tmp
$ mkdir data
$ ls *.csv
feb.csv jan.csv mar.csv
$ mv *.csv data/
$ ls *.tmp
draft.tmp old.tmp
$ rm -i *.tmp
remove draft.tmp? y
remove old.tmp? y
$ ls -R
.:
data notes.txt
./data:
feb.csv jan.csv mar.csv&& runs the second command only if the first succeeds, so cd tidy only happens if mkdir worked. ls -R lists folders recursively.
Common mistakes
rmin the wrong folder. Checkpwdfirst.- A space in a path:
rm my file.txttries to delete two files,myandfile.txt. Quote it:rm "my file.txt". - Overwriting with
mv/cpby accident. Use-i. - Copying a folder without
-r(you get "is a directory" or "omitting directory").
Quick recap
mkdir -p,touchandecho ... >create;cat,less,head,tailview.cpcopies (-rfor folders);mvmoves and renames.rmdeletes permanently;rm -rdeletes folders. Preview withls, checkpwd, use-i.- Wildcards (
*,?) select many files: always preview them first.
Try it
Open the Do tab to build and reorganise a small project folder.
Key terms (5)
- mkdir
- The command that makes a folder; -p also creates any missing parent folders.
- cp
- The command that copies files; -r copies a folder and everything inside it.
- mv
- The command that moves a file or folder, or renames it when the destination is a new name.
- rm
- The command that deletes files permanently; -r deletes folders, and -i asks before each deletion.
- wildcards
- Patterns such as *.csv or data?.txt that the shell expands to matching file names before the command runs.