Linux File and Directory Commands Cheat Sheet: ls, cd, cp, mv, rm, mkdir with Examples
These seven commands are the first thing you learn and the last thing you forget: ls, cd, pwd, mkdir, touch, cp, mv and rm. You will type them thousands of times. Here you learn each one, its useful options, and the mistakes that delete real data.
What you'll learn in this chapter
- Move around with pwd, cd and ls
- Create files and folders with touch and mkdir -p
- Copy with cp -r, -a, -i, -u
- Move and rename with mv
- Delete safely with rm, rm -r, rmdir
- Wildcards, brace expansion and a printable cheat sheet
1. An everyday example: arranging a cupboard
Why do we need this?
Pooja is sorting her office cupboard in Thane. She checks where she is standing, opens a shelf to look, makes new boxes, photocopies a file, moves a folder to another shelf, and throws away old papers.
What is it?
Where am I = pwd. Look in the shelf = ls. Walk to another shelf = cd. New box = mkdir. Blank sheet = touch. Photocopy = cp. Move to another shelf = mv. Throw away = rm. Linux has no dustbin, so rm is final.
How to do it (step by step)
2. pwd and cd: where am I and how to move
Why do we need this?
Every command works on the current folder unless you give a path. Running rm in the wrong folder is a classic disaster.
What is it?
pwd prints the working directory. cd changes it. cd alone or cd ~ goes home. cd - goes back to the previous folder. cd .. goes one up.
| Command | Goes to |
|---|---|
| cd /var/log | Absolute path |
| cd logs | Sub folder from here |
| cd .. | Parent |
| cd ../.. | Two up |
| cd ~ or cd | Home |
| cd - | Previous folder |
How to do it (step by step)
- Print where you are.
pwd
Output:
/home/arjun
What it means: You are in Arjun's home.
- Jump to logs and back.
cd /var/log && pwd && cd -
Output:
/var/log
/home/arjun
What it means: cd - returns and prints the old folder.
- Go two levels up.
cd /etc/nginx/sites-enabled && cd ../.. && pwd
Output:
/etc
What it means: Each .. removes one part of the path.
- Names with spaces need quotes.
cd "Project Files"
What it means: Without quotes, cd sees two words and fails.
cd in depth: cd ~, cd - and cd ..
Why do we need this?
You move between folders all day. Knowing the short forms saves time and stops you from working in the wrong place.
What is it?
cd means change directory. It takes one path. With no path it goes to your home folder.
Common options
| Option | What it does |
|---|---|
cd ~ | Go to your home folder (same as plain cd). |
cd ~priya | Go to the home of user priya. |
cd - | Go back to the folder you were in before, and print it. |
cd .. | Go one folder up (the parent). |
cd ../.. | Go two folders up. |
cd / | Go to the root of the disk tree. |
How to do it (step by step)
- Start in a deep folder.
cd /var/www/shop/images && pwd
Output:
/var/www/shop/images
What it means: You are inside images.
- Go one up with ..
cd .. && pwd
Output:
/var/www/shop
What it means: .. is the parent of the current folder.
- Jump home with ~
cd ~ && pwd
Output:
/home/arjun
What it means: ~ is short for your home path.
- Go back with -
cd -
Output:
/var/www/shop
What it means: cd - swaps between the last two folders.
Practise this in the tasks below: Practice tasks
3. ls: see what is in a folder
Why do we need this?
You need to see names, sizes, owners, dates and hidden files before you act.
What is it?
ls lists files. Useful options: -l long format, -a show hidden (names starting with .), -h human sizes, -t newest first, -r reverse, -S biggest first, -R recursive, -d show the folder itself, -i inode numbers.
How to do it (step by step)
- Long list with human sizes and hidden files.
ls -lah
Output:
total 24K
drwxr-x--- 4 arjun arjun 4.0K Oct 25 10:00 .
drwxr-xr-x 5 root root 4.0K Oct 20 09:00 ..
-rw-r--r-- 1 arjun arjun 220 Oct 20 09:00 .bashrc
drwxrwxr-x 2 arjun arjun 4.0K Oct 25 10:00 app
-rw-rw-r-- 1 arjun arjun 1.2M Oct 25 09:58 data.csv
What it means: Type+permissions, links, owner, group, size, date, name.
- Newest files first (find what just changed).
ls -lt /var/log | head -4
Output:
total 2140
-rw-r----- 1 syslog adm 81234 Oct 25 10:52 syslog
-rw-r----- 1 syslog adm 20211 Oct 25 10:51 auth.log
drwxr-xr-x 2 root root 4096 Oct 25 10:40 nginx
What it means: syslog changed last.
- Biggest files first.
ls -lhS ~/downloads | head -3
Output:
total 3.1G
-rw-r--r-- 1 arjun arjun 2.8G Oct 21 ubuntu.iso
-rw-r--r-- 1 arjun arjun 210M Oct 22 video.mp4
What it means: The ISO is taking most of the space.
- Show info about the folder itself.
ls -ld /etc
Output:
drwxr-xr-x 142 root root 12288 Oct 25 08:00 /etc
What it means: Without -d, ls would list what is inside.
- Match with a wildcard.
ls *.log
Output:
app.log error.log
What it means: * matches any text. ? matches one character.
4. mkdir and touch: create folders and files
Why do we need this?
Projects need a folder structure. Scripts need empty files to start with or to mark that a step is done.
What is it?
mkdir makes folders. -p makes parent folders too and does not fail if they exist. -v prints what it made. -m sets permissions. touch makes an empty file, or updates the time of an existing file.
How to do it (step by step)
- Make a nested project tree in one go.
mkdir -pv shop/{src,tests,docs}
Output:
mkdir: created directory 'shop'
mkdir: created directory 'shop/src'
mkdir: created directory 'shop/tests'
mkdir: created directory 'shop/docs'
What it means: {a,b,c} is brace expansion. -p made shop first.
- Make many files at once.
touch shop/src/{app,db,utils}.py && ls shop/src
Output:
app.py db.py utils.py
What it means: Brace expansion works with touch too.
- Make month folders with a range.
mkdir -p reports/2026-{01..12} && ls reports | head -3
Output:
2026-01
2026-02
2026-03
What it means: {01..12} counts with leading zeros.
- Update a file time.
touch -d '2026-01-01 09:00' old.txt && ls -l old.txt
Output:
-rw-rw-r-- 1 arjun arjun 0 Jan 1 09:00 old.txt
What it means: Useful to test cleanup scripts that delete old files.
mkdir in depth: -p, -v and -m
Why do we need this?
Apps, backups and logs all need folders. A script must not fail just because a folder already exists.
What is it?
mkdir makes a new folder. Without -p it fails if the parent is missing or the folder already exists.
Common options
| Option | What it does |
|---|---|
mkdir NAME | Make one folder. |
-p | Make missing parents; no error if it exists. |
-v | Print each folder made. |
-m 750 | Set permissions at creation. |
How to do it (step by step)
- Try a nested path without -p.
mkdir data/2026/oct
Output:
mkdir: cannot create directory 'data/2026/oct': No such file or directory
What it means: data and 2026 do not exist yet.
- Now with -p and -v.
mkdir -pv data/2026/oct
Output:
mkdir: created directory 'data'
mkdir: created directory 'data/2026'
mkdir: created directory 'data/2026/oct'
What it means: -p made every missing level.
- Run it again.
mkdir -p data/2026/oct; echo $?
Output:
0
What it means: Exit code 0: no error even though it exists. Safe in scripts.
- Make a private folder.
mkdir -m 750 payroll && ls -ld payroll
Output:
drwxr-x--- 2 arjun arjun 4096 Oct 25 11:00 payroll
What it means: Others get no access from the start.
Practise this in the tasks below: Practice tasks
touch in depth: empty files and timestamps
Why do we need this?
Cleanup jobs, make and backups decide by file time. touch lets you create a file or set its time on purpose.
What is it?
Every file has an access time (atime) and a modify time (mtime). touch FILE creates it if missing, or sets both times to now.
Common options
| Option | What it does |
|---|---|
touch f | Create f, or update its times to now. |
-a | Change only the access time. |
-m | Change only the modify time. |
-c | Do not create the file if it is missing. |
-d "2026-01-01 09:00" | Set a date you choose. |
-t 202601010900 | Same, in the [[CC]YY]MMDDhhmm form. |
-r ref | Copy the times of another file. |
How to do it (step by step)
- Create a new empty file.
touch todo.txt && ls -l todo.txt
Output:
-rw-rw-r-- 1 arjun arjun 0 Oct 25 11:05 todo.txt
What it means: Size 0, time is now.
- See both times with stat.
stat -c '%x | %y' todo.txt
Output:
2026-10-25 11:05:10 | 2026-10-25 11:05:10
What it means: %x is access, %y is modify.
- Change only the modify time.
touch -m -d '2026-09-01 08:00' todo.txt && stat -c '%x | %y' todo.txt
Output:
2026-10-25 11:05:10 | 2026-09-01 08:00:00
What it means: The access time did not change.
- Do not create a missing file.
touch -c nofile.txt; ls nofile.txt
Output:
ls: cannot access 'nofile.txt': No such file or directory
What it means: -c only updates files that already exist.
- Copy times from another file.
touch -r /etc/hostname todo.txt && ls -l --time-style=+%F todo.txt
Output:
-rw-rw-r-- 1 arjun arjun 0 2026-03-14 todo.txt
What it means: todo.txt now has the same time as /etc/hostname.
Practise this in the tasks below: Practice tasks
5. cp: copy files and folders
Why do we need this?
Before you edit a config or change data, you take a copy. Copies are also how you deploy files.
What is it?
cp SOURCE DEST. -r copies folders. -a (archive) copies folders and keeps permissions, owners and times. -i asks before overwrite. -u copies only newer files. -v shows each file. -n never overwrites. Trailing / on folders matters little for cp, but the destination existing or not matters a lot.
How to do it (step by step)
- Back up a config with a date.
cp -a nginx.conf nginx.conf.$(date +%F)
What it means: -a keeps the same permissions and time.
- Copy a folder.
cp -r shop shop-backup && ls
Output:
shop shop-backup
What it means: Without -r you get: cp: -r not specified; omitting directory.
- Copy many files into a folder.
cp -v *.csv /data/incoming/
Output:
'jan.csv' -> '/data/incoming/jan.csv'
'feb.csv' -> '/data/incoming/feb.csv'
What it means: When there are many sources, the last argument must be a folder.
- Ask before overwriting.
cp -i report.pdf /share/
Output:
cp: overwrite '/share/report.pdf'? n
What it means: Typing n saved the old file.
- The destination-exists trap.
cp -r src dst; cp -r src dst; ls dst
Output:
app.py src
What it means: The second time, src was copied inside dst. Use cp -r src/. dst/ to copy the contents.
cp in depth: -r, -i, -v and -p
Why do we need this?
A copy is your safety net before every change. The right option keeps folders whole, asks before damage and keeps file details.
What is it?
cp SOURCE DEST copies. Plain cp skips folders and gives the copy a new owner and the current time.
Common options
| Option | What it does |
|---|---|
-r | Copy a folder and everything inside. |
-i | Ask before overwriting. |
-v | Print each file copied. |
-p | Keep mode, owner and times. |
-a | Archive: -r plus -p plus links. |
How to do it (step by step)
- Plain cp on a folder fails.
cp assets assets-copy
Output:
cp: -r not specified; omitting directory 'assets'
What it means: cp needs -r for folders.
- Copy the folder with -r and -v.
cp -rv assets assets-copy
Output:
'assets' -> 'assets-copy'
'assets/logo.png' -> 'assets-copy/logo.png'
What it means: -v lists every file it copied.
- See the time problem without -p.
cp invoice-sep.pdf /tmp/ && ls -l --time-style=+%F invoice-sep.pdf /tmp/invoice-sep.pdf
Output:
-rw-r--r-- 1 arjun arjun 52K 2026-09-30 invoice-sep.pdf
-rw-r--r-- 1 arjun arjun 52K 2026-10-25 /tmp/invoice-sep.pdf
What it means: The copy looks new. A cleanup by date would treat it wrong.
- Keep the details with -p.
cp -p invoice-sep.pdf /backup/ && ls -l --time-style=+%F /backup/invoice-sep.pdf
Output:
-rw-r--r-- 1 arjun arjun 52K 2026-09-30 /backup/invoice-sep.pdf
What it means: Same date and mode as the original.
- Ask before overwrite with -i.
cp -i notes.md /backup/
Output:
cp: overwrite '/backup/notes.md'? n
What it means: n keeps the old backup safe.
Practise this in the tasks below: Practice tasks
6. mv: move and rename
Why do we need this?
Files need new names and new places. On the same disk mv is instant because it only changes the name.
What is it?
mv SOURCE DEST renames or moves. There is no rename command needed. -i asks before overwrite. -n never overwrites. -v prints. -b keeps a backup of an overwritten file. Moving to another disk = copy + delete.
How to do it (step by step)
- Rename a file.
mv draft.txt final.txt && ls
Output:
final.txt
What it means: Same folder, new name.
- Move files into a folder.
mv -v *.jpg photos/
Output:
renamed 'a.jpg' -> 'photos/a.jpg'
renamed 'b.jpg' -> 'photos/b.jpg'
What it means: The files are now inside photos.
- Do not overwrite an existing file.
mv -n new.conf app.conf; ls
Output:
app.conf new.conf
What it means: app.conf existed, so nothing moved.
- Rename a folder.
mv shop-backup shop-old
What it means: Folders do not need -r with mv.
mv in depth: rename vs move
Why do we need this?
New users look for a rename command. In Linux, mv does both jobs, and it matters which one you get.
What is it?
If DEST is an existing folder, mv moves SOURCE into it. If DEST is a new name, mv renames. Same disk = instant. Another disk = copy then delete.
Common options
| Option | What it does |
|---|---|
mv a b | b does not exist: rename a to b. |
mv a dir/ | dir is a folder: move a into it. |
mv a dir/b | Move and rename in one step. |
-i | Ask before overwriting. |
-n | Never overwrite. |
-v | Print what happened. |
How to do it (step by step)
- Rename in the same folder.
mv -v quote.txt quote-final.txt
Output:
renamed 'quote.txt' -> 'quote-final.txt'
What it means: Same place, new name.
- Move into a folder.
mv -v quote-final.txt clients/
Output:
renamed 'quote-final.txt' -> 'clients/quote-final.txt'
What it means: clients existed, so the file went inside.
- Move and rename together.
mv clients/quote-final.txt archive/quote-2026.txt
What it means: New folder and new name in one step.
- The trap: a typo in the folder name.
mv report.pdf clinets; ls
Output:
clinets
What it means: No folder clinets existed, so the file was renamed to clinets. Add a / to be safe: mv report.pdf clinets/ fails instead.
Practise this in the tasks below: Practice tasks
7. rm and rmdir: delete safely
Why do we need this?
Disks fill up and old builds must go. But Linux has no recycle bin. A wrong rm -rf can wipe a company's data.
What is it?
rm deletes files. -r deletes folders and everything in them. -f forces, no questions, no error if missing. -i asks for each file. -I asks once for more than 3 files. rmdir deletes only empty folders. Modern rm refuses / unless --no-preserve-root.
How to do it (step by step)
- Delete one file and confirm.
rm -i old.txt
Output:
rm: remove regular empty file 'old.txt'? y
What it means: y deletes it. It is gone for good.
- Delete an empty folder.
rmdir shop/docs
What it means: Fails if not empty: Directory not empty.
- Delete a folder tree with a preview first.
ls -R build | head; rm -rI build
Output:
rm: remove 1 argument recursively? y
What it means: -I asks once. Safer than -f.
- Delete files older than 7 days (preview, then delete).
find /tmp/reports -name '*.csv' -mtime +7 -print; find /tmp/reports -name '*.csv' -mtime +7 -delete
Output:
/tmp/reports/sep.csv
What it means: Always run with -print first.
- Delete a file whose name starts with -.
rm -- -bad.txt
What it means: -- tells rm that options are over.
rm in depth: -r, -f and -i
Why do we need this?
There is no recycle bin. You must know exactly what each rm option does before you press Enter.
What is it?
rm removes files. With -r it removes folders and all inside. -f removes without asking and hides "not found" errors. -i asks for each file.
Common options
| Option | What it does |
|---|---|
rm f | Delete a file. |
-r | Delete a folder tree. |
-f | Force: no questions, no error if missing. |
-i | Ask before each delete. |
-I | Ask once for big deletes. |
-v | Print each removed file. |
How to do it (step by step)
- rm on a folder without -r.
rm old-build
Output:
rm: cannot remove 'old-build': Is a directory
What it means: rm needs -r for folders.
- Delete each file with a question.
rm -i draft1.txt draft2.txt
Output:
rm: remove regular file 'draft1.txt'? y
rm: remove regular file 'draft2.txt'? n
What it means: You chose to keep draft2.txt.
- Force a missing file.
rm -f lock.pid; echo $?
Output:
0
What it means: No error even if lock.pid was not there. Good in scripts.
- Delete a folder tree, showing each file.
rm -rv old-build
Output:
removed 'old-build/app.js'
removed directory 'old-build'
What it means: Check the path twice before -r. Never combine -rf with a variable that may be empty.
Practise this in the tasks below: Practice tasks
rmdir vs rm -r
Why do we need this?
Sometimes you want to delete a folder only if it is really empty. rmdir is the safe tool for that.
What is it?
rmdir deletes only empty folders and refuses otherwise. rm -r deletes the folder and everything inside, no matter what.
Common options
| Option | What it does |
|---|---|
rmdir d | Delete d only if empty. |
rmdir -p a/b/c | Delete c, then b, then a, if each becomes empty. |
-v | Print each removed folder. |
rm -r d | Delete d and all content. No safety check. |
How to do it (step by step)
- rmdir on a folder with files.
rmdir uploads
Output:
rmdir: failed to remove 'uploads': Directory not empty
What it means: rmdir protected your files.
- rmdir on an empty folder.
rmdir -v empty-dir
Output:
rmdir: removing directory, 'empty-dir'
What it means: Gone, because it had nothing inside.
- Remove an empty chain.
rmdir -pv tmp/x/y
Output:
rmdir: removing directory, 'tmp/x/y'
rmdir: removing directory, 'tmp/x'
rmdir: removing directory, 'tmp'
What it means: -p walked up and removed each empty parent.
- When you really mean everything.
rm -rI uploads
Output:
rm: remove 1 argument recursively? y
What it means: Use rm -r only when you are sure. -I asks once.
Practise this in the tasks below: Practice tasks
8. The cheat sheet
Why do we need this?
A one-page reference saves time during work and interviews.
What is it?
Here are the commands of this chapter with their best options.
| Task | Command |
|---|---|
| Where am I | pwd |
| Go home / back / up | cd ~ / cd - / cd .. |
| Detailed list | ls -lah |
| Newest first | ls -lt |
| Biggest first | ls -lhS |
| Make folder tree | mkdir -p a/b/c |
| Empty file | touch f.txt |
| Copy folder keeping info | cp -a src dst |
| Copy, ask first | cp -i a b |
| Rename / move | mv old new |
| Move, no overwrite | mv -n a dir/ |
| Delete, ask | rm -i f |
| Delete folder | rm -r dir |
| Delete empty folder | rmdir dir |
| Show folder tree | tree -L 2 |
How to do it (step by step)
- See the tree you built.
tree -L 2 shop
Output:
shop
├── src
│ ├── app.py
│ ├── db.py
│ └── utils.py
└── tests
2 directories, 3 files
What it means: tree shows the structure at a glance.
9. Real examples from many industries
Why do we need this?
The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.
What is it?
Each row is a made-up team. It shows who uses this and how.
How to do it (step by step)
| Team and city | How they use it | Command |
|---|---|---|
| Arjun, Java backend, Bengaluru | Copies the new JAR and keeps the old one | cp -a app.jar app.jar.bak |
| Emma, data science, Toronto | Makes dated folders for daily data | mkdir -p data/$(date +%F) |
| Wei, frontend, Singapore | Cleans old builds before a fresh build | rm -rf dist/ |
| Lakshmi, QA, Chennai | Moves failed test reports aside | mv reports/failed-* archive/ |
| Pedro, media, Lisbon | Finds the biggest video files | ls -lhS /media/videos |
| Nikhil, sysadmin, Nagpur | Backs up /etc before changes | sudo cp -a /etc /root/etc-$(date +%F) |
| Amara, NGO IT, Nairobi | Creates donor report folders by month | mkdir -p donors/2026-{01..12} |
10. Common errors and how to fix them
Why do we need this?
Everyone hits these errors. Knowing the cause saves hours.
What is it?
Read the exact message first. Then match it to a row below.
How to do it (step by step)
| Error you see | Why it happens | How to fix |
|---|---|---|
cp: -r not specified; omitting directory 'shop' | You tried to copy a folder without -r | Add -r or -a.cp -a shop shop2 |
rmdir: failed to remove 'docs': Directory not empty | rmdir only removes empty folders | Look inside, then use rm -r if sure.ls docs; rm -r docs |
mkdir: cannot create directory 'a/b/c': No such file or directory | Parent folders do not exist | Use -p.mkdir -p a/b/c |
mkdir: cannot create directory 'logs': File exists | Name already used | Use mkdir -p (no error if it exists) or pick another name.mkdir -p logs |
cp: target 'backup' is not a directory | Many sources but the last argument is not a folder | Create the folder first.mkdir backup && cp *.csv backup/ |
rm: cannot remove 'file': Permission denied | You do not have write permission on the folder | Check ls -ld of the folder; use sudo only if it is a system file you mean to delete.ls -ld . |
mv: cannot move 'a' to 'b/a': Directory not empty | Target folder of the same name already exists with files | Merge with cp -a then delete, or rename.cp -a a/. b/a/ && rm -r a |
ls: cannot access '*.log': No such file or directory | No file matches the wildcard | Check the folder and the pattern.ls -la |
rm: invalid option -- 'b' | File name starts with - | Use -- or ./ before the name.rm ./-bad.txt |
bash: cd: too many arguments | Folder name has spaces and no quotes | Use quotes.cd "My Files" |
11. Troubleshooting step by step
Why do we need this?
When a file command does something you did not expect, slow down and check these.
What is it?
Go top to bottom. Stop at the first step that shows the problem.
How to do it (step by step)
- Where am I?
pwd
- What will the wildcard expand to?
echo rm *.log
- Does the destination exist and is it a folder?
ls -ld dest
- Do I own the folder?
ls -ld . ; id
- Is it a link or a real folder?
ls -l name
- Is there a backup?
ls -l *.bak
12. Lab 1: Organise a messy downloads folder
Why do we need this?
Zoya runs a design studio in Lucknow. Her downloads folder has 300 mixed files. Sort them into folders by type.
What is it?
A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).
How to do it (step by step)
- Create test files.
mkdir -p ~/dl && cd ~/dl && touch logo{1..3}.png brief{1..2}.pdf song.mp3 invoice-{jan,feb}.pdf
- Look first.
ls
Output:
brief1.pdf brief2.pdf invoice-feb.pdf invoice-jan.pdf logo1.png logo2.png logo3.png song.mp3
- Make type folders.
mkdir -p images docs music
- Move by type.
mv -v *.png images/ && mv -v *.pdf docs/ && mv -v *.mp3 music/
Output:
renamed 'logo1.png' -> 'images/logo1.png'
...
- Put invoices in a sub-folder.
mkdir -p docs/invoices && mv docs/invoice-* docs/invoices/
- Check the result.
tree
Output:
.
├── docs
│ ├── brief1.pdf
│ ├── brief2.pdf
│ └── invoices
│ ├── invoice-feb.pdf
│ └── invoice-jan.pdf
├── images
│ ├── logo1.png ...
└── music
└── song.mp3
- Back up and clean.
cp -a ~/dl ~/dl-backup && rm -rI ~/dl-backup/music
Output:
rm: remove 1 argument recursively? y
13. Lab 2: Safe deploy with copies
Why do we need this?
Deploy a new version of a site and be able to roll back in one command.
What is it?
A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).
How to do it (step by step)
- Create releases.
mkdir -p /srv/site/releases/{v1,v2} && echo v1 > /srv/site/releases/v1/index.html && echo v2 > /srv/site/releases/v2/index.html
- Copy v1 live.
cp -a /srv/site/releases/v1/. /srv/site/live/ 2>/dev/null || { mkdir -p /srv/site/live && cp -a /srv/site/releases/v1/. /srv/site/live/; }; cat /srv/site/live/index.html
Output:
v1
- Back up live, deploy v2.
cp -a /srv/site/live /srv/site/live.bak && cp -a /srv/site/releases/v2/. /srv/site/live/ && cat /srv/site/live/index.html
Output:
v2
- Roll back.
rm -r /srv/site/live && mv /srv/site/live.bak /srv/site/live && cat /srv/site/live/index.html
Output:
v1
Ravindra Bagale's Tip
Before any rm with a wildcard, run the same line with echo in front. echo rm *.log shows exactly what will be deleted, and deletes nothing.
Ravindra Bagale's Tip – मराठी
Wildcard असलेल्या कोणत्याही rm आधी त्याच line च्या पुढे echo लावा. echo rm *.log नेमकं काय delete होईल ते दाखवतो, आणि काहीच delete करत नाही.
Ravindra Bagale's Tip – हिंदी
Wildcard वाले किसी भी rm से पहले उसी line के आगे echo लगाइए। echo rm *.log ठीक-ठीक दिखाता है कि क्या delete होगा, और कुछ delete नहीं करता।
14. Frequently asked questions
Q. Can I undo rm?
No. There is no recycle bin. Restore from backup or snapshot.
Q. cp -r vs cp -a?
-a = -r plus keep permissions, owner, times and links. Use -a for backups.
Q. How do I see hidden files?
ls -a. Hidden files start with a dot, like .bashrc.
Q. How do I rename many files?
Use a loop: for f in *.jpeg; do mv "$f" "${f%.jpeg}.jpg"; done.
Q. Why is mv instant for a 10 GB file?
On the same file system it only changes the directory entry. The data does not move.
15. Interview questions and answers
Beginner level
Q. How do you create nested folders?
mkdir -p a/b/c.
Q. How do you copy a folder?
cp -r src dst, or cp -a to keep attributes.
Q. How do you rename a file?
mv old new.
Q. What does cd - do?
Goes back to the previous folder.
Mid level level
Q. rm -i vs rm -I?
-i asks for every file. -I asks once when deleting more than 3 files or recursively.
Q. Why does cp -r src dst behave differently the second time?
If dst exists, src is copied inside it as dst/src. Use src/. to copy contents.
Q. How do you delete a file named -f?
rm -- -f or rm ./-f.
Q. How to list files sorted by size?
ls -lhS.
Senior level
Q. How do you protect production from a careless rm -rf?
Least privilege, no root shells, backups and snapshots, immutable infra, set -u in scripts so empty variables do not become rm -rf /, and code review.
Q. A script runs rm -rf $DIR/. What can go wrong?
If DIR is empty it becomes rm -rf /. Use ${DIR:?} and set -u.
Q. cp vs rsync for big folders?
rsync copies only changes, can resume, shows progress and works over SSH. cp copies everything each time.
Q. Why is mv across disks slower and riskier?
It copies then deletes. A crash in the middle can leave half data. Use rsync then delete after a check.
Try at home
- Create a folder tree for a pretend shop with mkdir -p and brace expansion.
- Use ls -lt and ls -lhS in your downloads folder.
- Practise echo rm *.tmp before a real rm.
Practice Tasks
Amravati Agro Market is a agriculture business in Amravati. Anjali Thakre looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.
0 / 30
Easy Where am I?
Why: Never delete in the wrong folder.
What to do: Print the current folder on the Amravati Agro Market server with every symlink resolved (the physical path).
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Another user home
Why: Switch between two folders fast.
What to do: Go to the home folder of user kavya using the tilde shortcut.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Long list with hidden files
Why: .env files are hidden.
What to do: List everything in /etc/nginx including hidden files, in long format with human sizes.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Newest first
Why: See what changed just now.
What to do: List /var/log/nginx in long format with the newest files first.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Nested folders
Why: One command, full tree.
What to do: Create amravatiagro/src/api including the parent folders.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Create an empty file
Why: Placeholders and markers.
What to do: Create an empty file named README.md in the current folder.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Copy a folder
Why: Folders need a recursive copy.
What to do: Copy the folder site to site-old.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Rename a file
Why: There is no separate rename needed.
What to do: Rename notes-v1.txt to notes-final.txt.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Delete with a question
Why: No recycle bin in Linux.
What to do: Delete old.log but ask for confirmation first.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Delete an empty folder
Why: Safe: fails if not empty.
What to do: Remove the empty folder tmpdir.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Go to another user home
Why: Admins check user folders.
What to do: Go to the home folder of the user deploy using the tilde form.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Go three folders up
Why: Leave a deep path fast.
What to do: You are deep in a project. Move up three levels in one cd command.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Nested folders, verbose
Why: Scripts should show what they made.
What to do: Create logs/2027/jan including missing parents and print each folder created.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Update only if it exists
Why: Do not create stray files.
What to do: Update the time of report.csv but do not create it if it is missing.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Copy a folder verbosely
Why: See every file copied.
What to do: Copy the folder themes to themes-bak and print each file copied.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Move into a folder
Why: Keep HR files together.
What to do: Move salary.xlsx into the existing folder hr.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Delete, no error if missing
Why: Cleanup scripts must not stop.
What to do: Delete app.pid without any question and without an error if it does not exist.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Biggest first
Why: Find space hogs.
What to do: List /srv/amravatiagro/uploads in long format, human sizes, biggest first.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Backup keeping attributes
Why: Owner and time must stay the same.
What to do: Copy the folder /etc/nginx to /root/nginx-backup keeping permissions, owners and times.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Copy but ask first
Why: Do not overwrite by mistake.
What to do: Copy invoice-q2.pdf into /mnt/team/ but ask before overwriting.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Move without overwrite
Why: Protect existing files.
What to do: Move site.new to site.conf but never overwrite if site.conf exists.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Delete a folder tree
Why: Old old-dists take space.
What to do: Delete the folder old-dist and everything in it, asking once.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Private folder at creation
Why: No window where others can read.
What to do: Create a folder named vault that only the owner can use (rwx for owner, nothing for others), in one mkdir command.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Copy a timestamp
Why: Keep build tools in sync.
What to do: Give site.cfg the same timestamps as master.cfg.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Access time only
Why: Test atime-based cleanup.
What to do: Change only the access time of stats.log to now.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Copy and keep details
Why: Audits need original dates.
What to do: Copy contract.pdf into /archive/ keeping its mode, owner and timestamps.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Copy folder, ask first
Why: Do not overwrite live files by mistake.
What to do: Copy the folder assets into public/ but ask before overwriting any file.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Delete folder, ask each
Why: Careful cleanup.
What to do: Delete the folder tmp-build and ask before each file.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Remove an empty chain
Why: Tidy empty parents too.
What to do: cache/a/b is empty. Remove b, then a, then cache, with one rmdir command.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Hard Delete a file named -x
Why: Names starting with - confuse commands.
What to do: Delete the file named -x in the current folder.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
16. Related chapters
- Linux Ops Refresh for DevOps
- Bash Scripting for Automation
- Linux grep and find Commands with Examples
- How to Set Up Passwordless SSH Between Linux Servers
In short
- pwd, cd, ls to look around.
- mkdir -p and touch to create.
- cp -a to copy safely, mv to move or rename.
- rm is final: preview with echo, use -i or -I.
Revision, comparison and interview cards
Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.








More cards: Ravi Sir's Cards.