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

AWS Cloud

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.

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

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.

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.

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

Before typing a command that changes files, read the prompt as a clue and use pwd for the working directory. A path beginning with / starts at the file-system root. A relative path starts in your current directory. If you are in /home/asha/lab, notes.txt means /home/asha/lab/notes.txt, while ../notes.txt goes up one level to /home/asha/notes.txt.

Practise inside a dedicated lab directory. Use ls to inspect both the source and destination before a copy or move. A name such as my notes.txt needs quotes in the shell so it is passed as one argument. When a wildcard is involved, list what it matches before trying a destructive command. Checking both paths also helps you notice whether a destination already exists.

Every command works on the current folder unless you give a path. Running rm in the wrong folder is a classic disaster.

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
  1. Print where you are.
pwd

Output:

/home/arjun

You are in Arjun's home.

  1. Jump to logs and back.
cd /var/log && pwd && cd - 

Output:

/var/log
/home/arjun

cd - returns and prints the old folder.

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

Output:

/etc

Each .. removes one part of the path.

  1. Names with spaces need quotes.
cd "Project Files"

Without quotes, cd sees two words and fails.

cd in depth: cd ~, cd - and cd ..

You move between folders all day. Knowing the short forms saves time and stops you from working in the wrong place.

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.
  1. Start in a deep folder.
cd /var/www/shop/images && pwd

Output:

/var/www/shop/images

You are inside images.

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

Output:

/var/www/shop

.. is the parent of the current folder.

  1. Jump home with ~
cd ~ && pwd

Output:

/home/arjun

~ is short for your home path.

  1. Go back with -
cd -

Output:

/var/www/shop

cd - swaps between the last two folders.

Practise this in the tasks below: Practice tasks

3. ls: see what is in a folder

You need to see names, sizes, owners, dates and hidden files before you act.

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.

  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

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

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

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

Without -d, ls would list what is inside.

  1. Match with a wildcard.
ls *.log

Output:

app.log  error.log

* matches any text. ? matches one character.

4. mkdir and touch: create folders and files

Projects need a folder structure. Scripts need empty files to start with or to mark that a step is done.

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.

  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'

{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

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

{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

Useful to test cleanup scripts that delete old files.

mkdir in depth: -p, -v and -m

Apps, backups and logs all need folders. A script must not fail just because a folder already exists.

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.
  1. Try a nested path without -p.
mkdir data/2026/oct

Output:

mkdir: cannot create directory 'data/2026/oct': No such file or directory

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'

-p made every missing level.

  1. Run it again.
mkdir -p data/2026/oct; echo $?

Output:

0

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

Others get no access from the start.

Practise this in the tasks below: Practice tasks

touch in depth: empty files and timestamps

Cleanup jobs, make and backups decide by file time. touch lets you create a file or set its time on purpose.

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

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

%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

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

-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

todo.txt now has the same time as /etc/hostname.

Practise this in the tasks below: Practice tasks

5. cp: copy files and folders

Before you edit a config or change data, you take a copy. Copies are also how you deploy files.

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.

  1. Back up a config with a date.
cp -a nginx.conf nginx.conf.$(date +%F)

-a keeps the same permissions and time.

  1. Copy a folder.
cp -r shop shop-backup && ls

Output:

shop  shop-backup

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'

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

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

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

A copy is your safety net before every change. The right option keeps folders whole, asks before damage and keeps file details.

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.
  1. Plain cp on a folder fails.
cp assets assets-copy

Output:

cp: -r not specified; omitting directory 'assets'

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'

-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

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

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

n keeps the old backup safe.

Practise this in the tasks below: Practice tasks

6. mv: move and rename

Files need new names and new places. On the same disk mv is instant because it only changes the name.

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.

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

Output:

final.txt

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'

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

app.conf existed, so nothing moved.

  1. Rename a folder.
mv shop-backup shop-old

Folders do not need -r with mv.

mv in depth: rename vs move

New users look for a rename command. In Linux, mv does both jobs, and it matters which one you get.

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.
  1. Rename in the same folder.
mv -v quote.txt quote-final.txt

Output:

renamed 'quote.txt' -> 'quote-final.txt'

Same place, new name.

  1. Move into a folder.
mv -v quote-final.txt clients/

Output:

renamed 'quote-final.txt' -> 'clients/quote-final.txt'

clients existed, so the file went inside.

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

New folder and new name in one step.

  1. The trap: a typo in the folder name.
mv report.pdf clinets; ls

Output:

clinets

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

Disks fill up and old builds must go. But Linux has no recycle bin. A wrong rm -rf can wipe a company's data.

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.

  1. Delete one file and confirm.
rm -i old.txt

Output:

rm: remove regular empty file 'old.txt'? y

y deletes it. It is gone for good.

  1. Delete an empty folder.
rmdir shop/docs

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

-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

Always run with -print first.

  1. Delete a file whose name starts with -.
rm -- -bad.txt

-- tells rm that options are over.

rm in depth: -r, -f and -i

There is no recycle bin. You must know exactly what each rm option does before you press Enter.

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.
  1. rm on a folder without -r.
rm old-build

Output:

rm: cannot remove 'old-build': Is a directory

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

You chose to keep draft2.txt.

  1. Force a missing file.
rm -f lock.pid; echo $?

Output:

0

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'

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

Sometimes you want to delete a folder only if it is really empty. rmdir is the safe tool for that.

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.
  1. rmdir on a folder with files.
rmdir uploads

Output:

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

rmdir protected your files.

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

Output:

rmdir: removing directory, 'empty-dir'

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'

-p walked up and removed each empty parent.

  1. When you really mean everything.
rm -rI uploads

Output:

rm: remove 1 argument recursively? y

Use rm -r only when you are sure. -I asks once.

Practise this in the tasks below: Practice tasks

8. The cheat sheet

A one-page reference saves time during work and interviews.

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
  1. See the tree you built.
tree -L 2 shop

Output:

shop
├── src
│   ├── app.py
│   ├── db.py
│   └── utils.py
└── tests

2 directories, 3 files

tree shows the structure at a glance.

9. Real examples from many industries

The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.

Each row is a made-up team. It shows who uses this and how.

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

Everyone hits these errors. Knowing the cause saves hours.

Read the exact message first. Then match it to a row below.

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

When a file command does something you did not expect, slow down and check these.

Go top to bottom. Stop at the first step that shows the problem.

  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

Zoya runs a design studio in Lucknow. Her downloads folder has 300 mixed files. Sort them into folders by type.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  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

Deploy a new version of a site and be able to roll back in one command.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  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.

Practise file changes in a disposable directory

Make a temporary directory and two harmless files. Verify pwd and list contents before cp, mv or rm. Use -- before path operands that could start with a hyphen, and preview wildcard expansion before deletion.

Interview check Why does rm ./* differ from rm -r directory?

The shell expands ./* before calling rm, usually excluding dotfiles. A recursive directory operand lets rm traverse that directory, including hidden contents, subject to the flags used.

Reference documentation

Practise this chapter

Interview questions and answers

How do you make a file move predictable when both the source and destination are relative?

I would run pwd and inspect each path before moving anything. I would confirm whether the destination already exists and is a directory or file. I would quote names with spaces and verify the source is gone and the expected destination now contains the file.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. In /home/asha/lab, where does ../notes.txt resolve?

Answer and explanation

B. /home/asha/notes.txt

The double dot goes to the parent directory. Then the relative filename is resolved there.

2. How do you pass my notes.txt as one shell argument?

Answer and explanation

B. Quote the full filename

Quoting keeps the space as part of the filename rather than dividing shell arguments.

3. What is a safe first check before a wildcard delete in a lab?

Answer and explanation

B. List the files matched by the same pattern

Previewing the match exposes unintended targets before the delete runs.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

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?

Never delete in the wrong folder.

Print the current folder on the Amravati Agro Market server with every symlink resolved (the physical path).

  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

Switch between two folders fast.

Go to the home folder of user kavya using the tilde shortcut.

  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

.env files are hidden.

List everything in /etc/nginx including hidden files, in long format with human sizes.

  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

See what changed just now.

List /var/log/nginx in long format with the newest files first.

  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

One command, full tree.

Create amravatiagro/src/api including the parent folders.

  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

Placeholders and markers.

Create an empty file named README.md in the current folder.

  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

Folders need a recursive copy.

Copy the folder site to site-old.

  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

There is no separate rename needed.

Rename notes-v1.txt to notes-final.txt.

  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

No recycle bin in Linux.

Delete old.log but ask for confirmation first.

  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

Safe: fails if not empty.

Remove the empty folder tmpdir.

  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

Admins check user folders.

Go to the home folder of the user deploy using the tilde form.

  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

Leave a deep path fast.

You are deep in a project. Move up three levels in one cd command.

  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

Scripts should show what they made.

Create logs/2027/jan including missing parents and print each folder created.

  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

Do not create stray files.

Update the time of report.csv but do not create it if it is missing.

  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

See every file copied.

Copy the folder themes to themes-bak and print each file copied.

  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

Keep HR files together.

Move salary.xlsx into the existing folder hr.

  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

Cleanup scripts must not stop.

Delete app.pid without any question and without an error if it does not exist.

  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

Find space hogs.

List /srv/amravatiagro/uploads in long format, human sizes, biggest first.

  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

Owner and time must stay the same.

Copy the folder /etc/nginx to /root/nginx-backup keeping permissions, owners and times.

  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

Do not overwrite by mistake.

Copy invoice-q2.pdf into /mnt/team/ but ask before overwriting.

  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

Protect existing files.

Move site.new to site.conf but never overwrite if site.conf exists.

  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

Old old-dists take space.

Delete the folder old-dist and everything in it, asking once.

  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

No window where others can read.

Create a folder named vault that only the owner can use (rwx for owner, nothing for others), in one mkdir command.

  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

Keep build tools in sync.

Give site.cfg the same timestamps as master.cfg.

  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

Test atime-based cleanup.

Change only the access time of stats.log to now.

  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

Audits need original dates.

Copy contract.pdf into /archive/ keeping its mode, owner and timestamps.

  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

Do not overwrite live files by mistake.

Copy the folder assets into public/ but ask before overwriting any file.

  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

Careful cleanup.

Delete the folder tmp-build and ask before each file.

  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

Tidy empty parents too.

cache/a/b is empty. Remove b, then a, then cache, with one rmdir command.

  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

Names starting with - confuse commands.

Delete the file named -x in the current folder.

  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.