Ravindra BagaleCourses & study guides मराठी Track your progress

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)

Safe file workflowpwd: where am I?ls -l: what is here?cp -a: make a backupmv / editls -l: checkrm -i: clean up
Always look before you change, and back up before you delete.

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.

CommandGoes to
cd /var/logAbsolute path
cd logsSub folder from here
cd ..Parent
cd ../..Two up
cd ~ or cdHome
cd -Previous folder

How to do it (step by step)

  1. Print where you are.
pwd

Output:

/home/arjun

What it means: You are in Arjun's home.

  1. 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.

  1. Go two levels up.
cd /etc/nginx/sites-enabled && cd ../.. && pwd

Output:

/etc

What it means: Each .. removes one part of the path.

  1. 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

OptionWhat it does
cd ~Go to your home folder (same as plain cd).
cd ~priyaGo 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)

  1. Start in a deep folder.
cd /var/www/shop/images && pwd

Output:

/var/www/shop/images

What it means: You are inside images.

  1. Go one up with ..
cd .. && pwd

Output:

/var/www/shop

What it means: .. is the parent of the current folder.

  1. Jump home with ~
cd ~ && pwd

Output:

/home/arjun

What it means: ~ is short for your home path.

  1. 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)

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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)

  1. 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.

  1. 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.

  1. 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.

  1. 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

OptionWhat it does
mkdir NAMEMake one folder.
-pMake missing parents; no error if it exists.
-vPrint each folder made.
-m 750Set permissions at creation.

How to do it (step by step)

  1. 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.

  1. 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.

  1. 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.

  1. 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

OptionWhat it does
touch fCreate f, or update its times to now.
-aChange only the access time.
-mChange only the modify time.
-cDo not create the file if it is missing.
-d "2026-01-01 09:00"Set a date you choose.
-t 202601010900Same, in the [[CC]YY]MMDDhhmm form.
-r refCopy the times of another file.

How to do it (step by step)

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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)

  1. 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.

  1. 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.

  1. 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.

  1. Ask before overwriting.
cp -i report.pdf /share/

Output:

cp: overwrite '/share/report.pdf'? n

What it means: Typing n saved the old file.

  1. 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

OptionWhat it does
-rCopy a folder and everything inside.
-iAsk before overwriting.
-vPrint each file copied.
-pKeep mode, owner and times.
-aArchive: -r plus -p plus links.

How to do it (step by step)

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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)

  1. Rename a file.
mv draft.txt final.txt && ls

Output:

final.txt

What it means: Same folder, new name.

  1. 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.

  1. 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.

  1. 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

OptionWhat it does
mv a bb does not exist: rename a to b.
mv a dir/dir is a folder: move a into it.
mv a dir/bMove and rename in one step.
-iAsk before overwriting.
-nNever overwrite.
-vPrint what happened.

How to do it (step by step)

  1. 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.

  1. 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.

  1. Move and rename together.
mv clients/quote-final.txt archive/quote-2026.txt

What it means: New folder and new name in one step.

  1. 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)

  1. 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.

  1. Delete an empty folder.
rmdir shop/docs

What it means: Fails if not empty: Directory not empty.

  1. 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.

  1. 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.

  1. 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

OptionWhat it does
rm fDelete a file.
-rDelete a folder tree.
-fForce: no questions, no error if missing.
-iAsk before each delete.
-IAsk once for big deletes.
-vPrint each removed file.

How to do it (step by step)

  1. 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.

  1. 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.

  1. 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.

  1. 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

OptionWhat it does
rmdir dDelete d only if empty.
rmdir -p a/b/cDelete c, then b, then a, if each becomes empty.
-vPrint each removed folder.
rm -r dDelete d and all content. No safety check.

How to do it (step by step)

  1. rmdir on a folder with files.
rmdir uploads

Output:

rmdir: failed to remove 'uploads': Directory not empty

What it means: rmdir protected your files.

  1. rmdir on an empty folder.
rmdir -v empty-dir

Output:

rmdir: removing directory, 'empty-dir'

What it means: Gone, because it had nothing inside.

  1. 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.

  1. 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.

TaskCommand
Where am Ipwd
Go home / back / upcd ~ / cd - / cd ..
Detailed listls -lah
Newest firstls -lt
Biggest firstls -lhS
Make folder treemkdir -p a/b/c
Empty filetouch f.txt
Copy folder keeping infocp -a src dst
Copy, ask firstcp -i a b
Rename / movemv old new
Move, no overwritemv -n a dir/
Delete, askrm -i f
Delete folderrm -r dir
Delete empty folderrmdir dir
Show folder treetree -L 2

How to do it (step by step)

  1. 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 cityHow they use itCommand
Arjun, Java backend, BengaluruCopies the new JAR and keeps the old onecp -a app.jar app.jar.bak
Emma, data science, TorontoMakes dated folders for daily datamkdir -p data/$(date +%F)
Wei, frontend, SingaporeCleans old builds before a fresh buildrm -rf dist/
Lakshmi, QA, ChennaiMoves failed test reports asidemv reports/failed-* archive/
Pedro, media, LisbonFinds the biggest video filesls -lhS /media/videos
Nikhil, sysadmin, NagpurBacks up /etc before changessudo cp -a /etc /root/etc-$(date +%F)
Amara, NGO IT, NairobiCreates donor report folders by monthmkdir -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 seeWhy it happensHow to fix
cp: -r not specified; omitting directory 'shop'You tried to copy a folder without -rAdd -r or -a.
cp -a shop shop2
rmdir: failed to remove 'docs': Directory not emptyrmdir only removes empty foldersLook inside, then use rm -r if sure.
ls docs; rm -r docs
mkdir: cannot create directory 'a/b/c': No such file or directoryParent folders do not existUse -p.
mkdir -p a/b/c
mkdir: cannot create directory 'logs': File existsName already usedUse mkdir -p (no error if it exists) or pick another name.
mkdir -p logs
cp: target 'backup' is not a directoryMany sources but the last argument is not a folderCreate the folder first.
mkdir backup && cp *.csv backup/
rm: cannot remove 'file': Permission deniedYou do not have write permission on the folderCheck 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 emptyTarget folder of the same name already exists with filesMerge with cp -a then delete, or rename.
cp -a a/. b/a/ && rm -r a
ls: cannot access '*.log': No such file or directoryNo file matches the wildcardCheck 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 argumentsFolder name has spaces and no quotesUse 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)

  1. Where am I?
pwd
  1. What will the wildcard expand to?
echo rm *.log
  1. Does the destination exist and is it a folder?
ls -ld dest
  1. Do I own the folder?
ls -ld . ; id
  1. Is it a link or a real folder?
ls -l name
  1. 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)

  1. Create test files.
mkdir -p ~/dl && cd ~/dl && touch logo{1..3}.png brief{1..2}.pdf song.mp3 invoice-{jan,feb}.pdf
  1. Look first.
ls

Output:

brief1.pdf  brief2.pdf  invoice-feb.pdf  invoice-jan.pdf  logo1.png  logo2.png  logo3.png  song.mp3
  1. Make type folders.
mkdir -p images docs music
  1. Move by type.
mv -v *.png images/ && mv -v *.pdf docs/ && mv -v *.mp3 music/

Output:

renamed 'logo1.png' -> 'images/logo1.png'
...
  1. Put invoices in a sub-folder.
mkdir -p docs/invoices && mv docs/invoice-* docs/invoices/
  1. Check the result.
tree

Output:

.
├── docs
│   ├── brief1.pdf
│   ├── brief2.pdf
│   └── invoices
│       ├── invoice-feb.pdf
│       └── invoice-jan.pdf
├── images
│   ├── logo1.png ...
└── music
    └── song.mp3
  1. 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)

  1. 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
  1. 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
  1. 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
  1. 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.

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

  1. Create a folder tree for a pretend shop with mkdir -p and brace expansion.
  2. Use ls -lt and ls -lhS in your downloads folder.
  3. Practise echo rm *.tmp before a real rm.

Practice Tasks

About this scenario

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).

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

In short

  1. pwd, cd, ls to look around.
  2. mkdir -p and touch to create.
  3. cp -a to copy safely, mv to move or rename.
  4. 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.

File commands. Daily work on every server. ls, cd, cp, mv, rm, mkdir. Use -r for folders. Make nested folders.
Revision card: File commands · Download PNG · Open card page
Find files. Locate lost or big files. find searches by name, size. Add -type f and -mtime. Files over 100 MB.
Revision card: Find files · Download PNG · Open card page
cp vs mv. Copies a file. / Moves or renames.
Comparison: cp vs mv · Download PNG · Open card page
find vs locate. Walks the disk live. / Reads a saved index.
Comparison: find vs locate · Download PNG · Open card page
How to stay safe with rm?. rm has no recycle bin. Use ls first, then rm. Ask before each delete.
Interview card: How to stay safe with rm? · Download PNG · Open card page
cp vs mv?. cp makes a second copy. mv moves or renames. Keep times with -p.
Interview card: cp vs mv? · Download PNG · Open card page
How to see hidden files?. Names start with a dot. ls hides them by default. Use -a to show all.
Interview card: How to see hidden files? · Download PNG · Open card page
Absolute vs relative path?. Absolute starts with /. Relative starts from here. See where you are.
Interview card: Absolute vs relative path? · Download PNG · Open card page

More cards: Ravi Sir's Cards.