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

AWS Cloud

Linux Directory Commands: pwd, ls, cd, mkdir for Beginners

Let's start. In the previous chapter we logged in to our EC2 server with SSH and saw that an operating system is mostly files and folders. Now we actually walk around inside the server. Where am I? What is in here? How do I make a folder, go into it, come back out, and jump to any folder in one line? By the end of this chapter you will read a path like /home/ec2-user/calendar/weeks and know exactly where it points. Keep your server running and type every command with me.

In this class

  • A short SSH recap and the four errors you will meet most
  • Linux users: root and ec2-user, and why you log in as ec2-user
  • When an idle SSH session drops, and how to get back fast
  • The Linux tree: everything starts at / (root)
  • Your home directory, and pwd
  • Reading a path
  • Files vs folders (directories)
  • mkdir: one folder or many
  • ls and ls -r
  • cd into a folder, and what the prompt tells you
  • Going up: cd .., cd ../.., cd ../../..
  • cd alone, cd -, and moving sideways
  • mkdir -p: folders inside folders in one line
  • Absolute paths, relative paths and ~
  • Worked scenario 1: the calendar (four ways to make week1)
  • Worked scenario 2: one to five (three ways to make 22)
  • Names with spaces
  • Tab completion, and what to do when names start the same
  • clear and history
  • Practice task

1. Quick recap: logging in with SSH

All Linux work starts with logging in to the server. If SSH fails, nothing else works. The full step-by-step, with all the fixes, is in the previous chapter: What Is Amazon EC2 and How Does It Work.

The command has four parts:

ssh -i Downloads\firstserver.pem ec2-user@3.3.3.3

  1. ssh: the program.
  2. -i: the identity file (your private key) comes next.
  3. Downloads\firstserver.pem: the path to the key. Type Dow and press Tab, then fir and press Tab, so there are no typos.
  4. ec2-user@3.3.3.3: the user name, @, and your server's public IPv4. 3.3.3.3 is a placeholder; use your own.

Paste the IP with Ctrl+V or right-click. Both work in the PowerShell of Windows 10 and 11, and in Windows Terminal.

The four errors you will meet most:

Error Usual cause Fix
Permission denied (publickey) wrong key for this server, or wrong user name use the key chosen at launch; ec2-user on Amazon Linux
No such file or directory typo in the key path use Tab completion
Permission denied again user name typed wrong: ec2user, or a space inside ec2-user@... it is ec2-user, with a hyphen, and no spaces
Connection timed out the public IP changed after a stop and start copy the new public IP from the console

If SSH still won't work:

  1. Check the OpenSSH client with ssh -V. If PowerShell says ssh is not recognized, add the OpenSSH Client optional feature, or install the latest PowerShell from Microsoft. Then open a new window.
  2. Use EC2 Instance Connect: select the instance → Connect → EC2 Instance Connect → Connect. A terminal opens in the browser.
  3. PuTTY is an older third-party tool. You rarely need it now, because Windows has ssh built in.

Classroom line

Miss one sentence and you won't follow the next.

Red text only means the command failed. One common cause is a missing OpenSSH client; check with ssh -V. Ctrl+V pastes in current PowerShell and Windows Terminal; right-click works too.

2. Linux users: root and ec2-user

The prompt says ec2-user. Who is that, and why not log in as the administrator?

Tell the top directory, the home folder and the administrator apart

Linux uses the word root in two different places. A path that begins with / starts at the top directory, called the filesystem root. The user called root is the administrator. The administrator’s home directory is often /root. These names describe three different things.

From a normal lab login, run whoami, pwd, then cd / and pwd again. Your folder can change to / while whoami still prints ec2-user. Return with cd. A changing path tells you where the shell is standing. The username controls what that shell is allowed to read or change.

  1. root is the administrator of a Linux system. It can do anything, including delete the whole system.
  2. ec2-user is the normal default login user on Amazon Linux. On Ubuntu the default user is ubuntu.
  3. Linux has many more accounts for system programs, but these two matter for logging in.
  4. We log in as ec2-user. When a task needs admin rights, we put sudo in front of that one command, for example sudo yum install tree -y.

How AWS sets this up.

  1. You choose a key pair at launch. The key pair is not tied to any user.
  2. At first boot, AWS puts the public half of that key into the default user's file ~/.ssh/authorized_keys: ec2-user on Amazon Linux, ubuntu on Ubuntu.
  3. That is why your key works for ec2-user.
  4. Direct SSH login as root is blocked, for safety. Use sudo instead.

The key pair isn't tied to a user: AWS installs it for the default user (ec2-user) at first boot, and root SSH login is disabled on purpose. Linux has many system accounts; root and ec2-user are the two that matter for login.

3. When an idle SSH session drops

You leave the terminal for a while, come back, and you are suddenly at your Windows prompt (PS C:\Users\ravi>). Your server is fine. Only the connection dropped.

AWS does not close idle SSH sessions by default. The drop usually comes from your home router or Wi-Fi, which forgets quiet connections after some idle minutes, or from your laptop going to sleep.

How to get back fast.

  1. Press the Up Arrow key in PowerShell. Your last command, the ssh -i ... line, comes back.
  2. Press Enter. You are logged in again.
  3. To stop the drops, add a keep-alive: ssh -o ServerAliveInterval=60 -i Downloads\firstserver.pem ec2-user@3.3.3.3. SSH then sends a small message every 60 seconds, so the router keeps the connection.

How long an idle session lasts before it drops is not a fixed AWS rule. It depends on your router, Wi-Fi or laptop sleep settings.

4. The Linux tree: everything starts at /

On Windows you think in drives: C:\, D:\. Linux has no drive letters. Everything hangs from one top point.

  1. The top of everything is /, called root (the root directory).
  2. Under / are the system directories: bin, sbin, etc, home, root, dev, opt, usr, var and more.
  3. /home holds one folder per user: /home/ec2-user, /home/ubuntu, /home/ravi.
  4. Your own work goes inside your home folder, /home/ec2-user.
  5. Careful with the word "root". / is the root directory. /root (with a name after the slash) is the home folder of the root user. They are different places.
Linux starts at one top directory: / (root) / ├─ bin sbin usr dev ├─ etc var opt ├─ root └─ home ├─ ec2-user ├─ ubuntu └─ ravi your folders and files go here /home/ec2-user = home of user ec2-user /root = home of the root user, not the same as / /etc = settings, /var = logs and data /bin, /usr = programs, /dev = devices Like C:\ on Windows (a simplification): everything on the system issomewhere under /.

Figure 1. The Linux tree starts at / (root). Under it are bin, sbin, etc, home, root, dev, opt, usr and var. /home holds a folder for each user; your files go under /home/ec2-user.

What the main directories hold (for now, just recognise the names):

Directory Holds
/bin, /usr/bin commands like ls and mkdir
/sbin system admin commands
/etc settings (configuration files)
/home one home folder per user
/root home of the root user
/var logs and changing data, e.g. /var/log
/dev devices, like disks
/opt extra software you install

Think of / like C:\ on Windows to start with, but / is not a folder like the others: it is the single top of the whole tree, and even other disks appear somewhere under it.

5. Your home directory, and pwd

"Where am I?" is the first question every time. Before you create or delete anything, know where you are standing.

pwd means print working directory. It prints the full path of the directory you are in now.

  1. Log in.
  2. Type pwd and press Enter.
  3. You see /home/ec2-user.
  4. After login you always land in your home directory.

6. Reading a path

Paths are how Linux says "go here". Read one slowly once, and you can read all of them.

Take /home/ec2-user/ravi/one:

  1. The first / is root, the top.
  2. Every other / only separates names. Read it as "go inside".
  3. So: start at root → into home → into ec2-user → into ravi → into one.
Reading a path: /home/ec2-user/ravi/one / first / = root, the top home / ec2-user / ravi / one every other / just separates names: go inside Read it as: start at root → into home → into ec2-user → into ravi →into one. Below root there are four directories here: home, ec2-user, ravi and one.pwd prints this kind of path.

Figure 2. The path /home/ec2-user/ravi/one: the first / is root; every other / separates names. Read it as root, then into home, ec2-user, ravi and one.

Worked example: count the levels.

  1. /home/ec2-user has two directories below root: home and ec2-user.
  2. Counting root itself, that is three levels: /, home, ec2-user.
  3. /home/ec2-user/ravi/one has four directories below root.

/home/ec2-user has two directories, home and ec2-user, or three levels counting the root /, which is the top and not a folder inside the path.

7. Files vs folders

Everything you create will be one of these two. They behave differently.

  1. A file holds content. You open it to read it or write in it: notes, a web page, a script.
  2. A folder is a container. It holds files and other folders. Think of a tiffin box: the box itself is not food; it holds the food, and even smaller boxes.
  3. Windows says folder. Linux says directory. Same thing. This course uses both words.

Classroom line

A folder is like a tiffin box.

File vs folder (directory) File: holds contentyou read it and write in it folder = tiffin box noteslist one a folder inside a folder Folder: a container for files and other foldersWindows says folder; Linux says directory. Same thing.

Figure 3. A file holds content you read and write. A folder (a directory, in Linux) is a container like a tiffin box: it holds files and other folders.

8. mkdir: make one folder, or many

Before you can organise anything, you need folders.

mkdir means make directory.

  1. mkdir ravi makes one folder named ravi.
  2. Linux prints nothing. No output means success. Linux stays quiet when all is well.
  3. ls shows ravi.
  4. Several at once: mkdir raja rani aws. Each name is a separate folder.
  5. Try the same name again: mkdir ravi.
  6. Error: mkdir: cannot create directory 'ravi': File exists. Two items in the same folder can't share a name.

Ravindra Bagale's Tip

mkdir -p ravi gives no error if ravi already exists. Useful in scripts. Section 13 shows the real job of -p.

9. ls and ls -r

After every change, look. ls is how you look.

ls means list. It lists the files and directories in the directory you are in.

  1. ls → aws raja rani ravi. Alphabetical order.
  2. ls -r → ravi rani raja aws. The -r option reverses the order (Z to A).
  3. A brand-new server shows nothing for ls in your home folder. That is normal.

The -r is an option: a letter after a dash that changes how a command behaves. You will meet many: ls -l, ls -a, ls -R.

10. cd into a folder, and what the prompt tells you

Making folders is half the job. You also need to go into them.

cd means change directory.

  1. Start at home. The prompt is [ec2-user@ip-172-31-5-10 ~]$.
  2. cd ravi
  3. The prompt now ends in ravi ]$ instead of ~ ]$.
  4. pwd → /home/ec2-user/ravi.
  5. mkdir one makes one inside ravi.
  6. cd one
  7. pwd → /home/ec2-user/ravi/one.

Reading the prompt [ec2-user@ip-172-31-5-10 ~]$:

  1. ec2-user: who you are.
  2. ip-172-31-5-10: the server's name, built from its private IP (the IP here is an example).
  3. ~: where you are. The prompt shows only the last part of the path, and shows ~ when you are at home.
  4. $: a normal user. A root shell shows #.
Handwritten board. Left column: mkdir ravi, cd ravi, then mkdir one at the ravi prompt, pwd, /home/ec2-user/ravi, cd one, pwd arrow /home/ec2-user/ravi/one. Right column: mkdir 'my folder', and mkdir 'my folder' 'your folder'.

Whiteboard: Left: mkdir ravi, cd ravi, mkdir one; pwd shows /home/ec2-user/ravi; cd one; pwd shows /home/ec2-user/ravi/one. Right: a folder name with a space goes in quotes, one or several at once.

Classroom line

Pay attention, and do your own work yourself.

~ in the prompt is the short form of your home path. The shell shows ~ instead of /home/ec2-user to keep the prompt short.

11. Going up: cd .., cd ../.., cd ../../..

You went into a folder. How do you come out?

.. (two dots) means the parent directory: one level up. Each ../ climbs one more level.

  1. Suppose you are in /home/ec2-user/ravi/one.
  2. cd .. → /home/ec2-user/ravi (one level up).
  3. Go back: cd one.
  4. cd ../.. → /home/ec2-user (two levels up).
  5. Go back: cd ravi/one.
  6. cd ../../.. → /home (three levels up).
  7. Check with pwd after every step.

Worked example: a common mistake.

  1. You are in /home (after cd ../../..).
  2. You type cd myfolder.
  3. Error: -bash: cd: myfolder: No such file or directory.
  4. Why? myfolder is inside ec2-user, not inside /home.
  5. Fix: cd ec2-user/myfolder.

Three levels up is cd ../../..: count one .. per level, and check with pwd before and after.

12. cd alone, cd -, and moving sideways

Typing many ../ is slow. Two shortcuts save you every day.

  1. cd alone takes you home (/home/ec2-user) from anywhere.
  2. cd - takes you back to the previous directory, and prints where it went. Press it again to switch back. It works like a back and forward switch.
  3. Sideways: to reach a folder next to yours, go up, then down in the same line.

Classroom line

There's a shortcut here too: to go back where you came from.

Worked example: a full walk. Suppose your home has one/two and myfolder side by side.

  1. cd ~/one/two then pwd → /home/ec2-user/one/two.
  2. cd .. → /home/ec2-user/one.
  3. cd ../myfolder → /home/ec2-user/myfolder (up to home, then into myfolder).
  4. cd ../.. → /home.
  5. cd → /home/ec2-user.
  6. cd - → prints /home and goes there.

Sideways from deeper. From /home/ec2-user/one/two, cd ../../myfolder reaches myfolder in one step. Back again: cd ../one/two.

Moving around with cd / └─ home └─ ec2-user (~) ├─ one │   └─ two └─ myfolder $ pwd start here /home/ec2-user/one/two $ cd .. up one level /home/ec2-user/one $ cd ../myfolder up, then sideways /home/ec2-user/myfolder $ cd ../.. up two levels /home $ cd alone: home /home/ec2-user $ cd - back to previous /home The orange box shows where you are after each command. cd prints nothing;pwd tells you where you are.

Figure 4. Animation: pwd, cd .., cd ../myfolder, cd ../.., cd and cd -. The orange box shows where you are after each command.

Classroom line

What is our job? To solve the problem in the least time.

13. mkdir -p: folders inside folders, in one line

Real projects need nested folders: project/src/css. Making them one by one with cd in between is slow.

Build the parent chain from a known starting place

Run cd and then pwd so the shell is in your home directory. If you ask mkdir path-lab/year/month in a fresh home, Linux needs to find path-lab/year before it can create month. When those parents do not exist, that command fails. Now run mkdir -p path-lab/year/month. It creates the missing parents and the last directory in one step.

Run the same mkdir -p again. It should leave the existing directories in place. Confirm with ls -ld path-lab/year/month and read the last part of the output as the path you asked to create. If any part of the chain is already a file instead of a directory, check that path before trying again.

Spaces and slashes mean different things to mkdir:

  1. mkdir india maharashtra pune → spaces = three separate folders, side by side.
  2. mkdir -p india/maharashtra/pune → slashes = one inside the other: pune inside maharashtra inside india.
  3. -p means parents (--parents): create any missing parent folders on the way, and give no error if they already exist.
  4. Without -p, mkdir india/maharashtra/pune fails when india doesn't exist yet: No such file or directory.
Side by side, or one inside another? mkdir india maharashtra pune three names = three folders, side by side ~ ├─ india ├─ maharashtra └─ pune ls in ~ shows all three: india maharashtra pune mkdir -p india/maharashtra/pune slashes = one inside the other; -p makes the parents ~ └─ india └─ maharashtra └─ pune cd into all three, then pwd: /home/ec2-user/india/maharashtra/pune Without -p, mkdir india/maharashtra/pune fails if india does not exist yet:No such file or directory.

Figure 5. mkdir india maharashtra pune makes three folders side by side. mkdir -p india/maharashtra/pune makes them one inside another.

Handwritten board: a tree india, arrow to maharashtra, arrow to pune; below it the command mkdir -p india/maharashtra/pune.

Whiteboard: india contains maharashtra, which contains pune. One command makes all three: mkdir -p india/maharashtra/pune.

Worked example: prove it.

  1. cd (go home).
  2. mkdir -p india/maharashtra/pune
  3. cd india then ls → maharashtra.
  4. cd maharashtra then ls → pune.
  5. cd pune then pwd → /home/ec2-user/india/maharashtra/pune.

A common question: months with jan, feb and mar.

  1. mkdir -p months/jan/feb/mar makes them nested: feb inside jan, mar inside feb. Usually not what you want.
  2. Side by side, the simple way: mkdir months, cd months, mkdir jan feb mar.
  3. Or, from home, give each full path separated by spaces: mkdir -p months/jan months/feb months/mar.
  4. Bonus shortcut (more in the next chapter): mkdir -p months/{jan,feb,mar}. The shell expands the braces into the three paths of step 3.

Classroom line

What will just typing it get you? Short-term satisfaction... What is the main aim? To understand a new concept.

14. Absolute paths, relative paths and ~

Every command that takes a path (cd, mkdir, ls, cp) accepts all three kinds. Knowing them lets you work on any folder without walking to it first.

Predict the target before you change directories

Put /home/ec2-user/calendar/weeks on the board as your current location. ../months means climb to calendar and go into months. /home/ec2-user/calendar/months starts at the top and names the same place without using your current location. ~/calendar/months starts with the current user’s home as expanded by the shell.

Try ls -d ../months while standing in weeks. Then move to a different folder and try the same relative path. Its meaning has changed with your position. An absolute path still points to the same place. This is why a command copied from a tutorial may work for the teacher and fail on your server until you check pwd.

  1. Absolute path: starts with /. It starts from root, so it means the same place no matter where you are. Example: /home/ec2-user/calendar/weeks.
  2. Relative path: does not start with /. It starts from where you are now. .. means the parent; a plain name means a folder inside the current one. Example: ../weeks.
  3. Tilde path: starts with ~. ~ means your home directory. Example: ~/calendar/weeks.

~ depends on who is logged in:

  1. Logged in as ec2-user: ~ = /home/ec2-user.
  2. Logged in as ubuntu: ~ = /home/ubuntu.
  3. Logged in as ravi: ~ = /home/ravi.
  4. / is always the top of the whole tree. ~ is your own folder inside it.
~ means the home of whoever is logged in /home ec2-user logged in as ec2-user: ~ = /home/ec2-user ubuntu logged in as ubuntu: ~ = /home/ubuntu ravi logged in as ravi: ~ = /home/ravi The shell (bash) turns ~ into that path before the commandruns. / is the top of the whole tree; ~ is your own folder init.

Figure 6. ~ means the home of whoever is logged in: /home/ec2-user, /home/ubuntu or /home/ravi. The shell turns ~ into that path.

It is the shell (bash) that turns ~ into your home path, before the command runs. One result: inside quotes, '~/x' is not expanded, so leave ~ outside quotes.

15. Worked scenario 1: the calendar

This scenario makes the three kinds of path click.

Set it up (from home):

  1. mkdir calendar
  2. cd calendar
  3. mkdir months weeks days
  4. cd months
  5. mkdir jan feb mar
  6. pwd → /home/ec2-user/calendar/months. You are in months.

The task: make week1 inside weeks, while you are in months. Four ways:

  1. Walk there, then make it: cd .., then cd weeks, then mkdir week1.
  2. Relative, stay where you are: mkdir ../weeks/week1 (up to calendar, into weeks, make week1).
  3. Absolute, works from anywhere: mkdir /home/ec2-user/calendar/weeks/week1.
  4. Tilde: mkdir ~/calendar/weeks/week1.
You are in months. Make week1 inside weeks: fourways ~ (/home/ec2-user) └─ calendar ├─ months ← you are here │   └─ jan feb mar ├─ weeks │   └─ week1 (new) └─ days 1. Walk there, then make it cd ..; cd weeks; mkdir week1 2. Relative (from where you are) mkdir ../weeks/week1 3. Absolute (starts with /, works from anywhere) mkdir /home/ec2-user/calendar/weeks/week1 4. Tilde (~ = your home) mkdir ~/calendar/weeks/week1 All four make the same folder: /home/ec2-user/calendar/weeks/week1

Figure 7. You are in months. Make week1 inside weeks by walking there, with a relative path, an absolute path or a tilde path. All four make /home/ec2-user/calendar/weeks/week1.

Check: ls ~/calendar/weeks → week1. If you try all four with the same name, the second one onwards says File exists, because week1 is already there. To practise all four, use a new name each time: week1, week2, week3, week4.

Classroom line

Real study happens when you build situations in your own mind.

16. Worked scenario 2: one to five

This is the situation you meet on real servers: you are deep inside a project and need a folder two levels up. Doing it three ways shows that absolute, relative and ~ paths all reach the same place, so you can pick the shortest one.

Set it up:

  1. cd
  2. mkdir -p one/two/three/four/five
  3. cd one/two/three/four
  4. pwd → /home/ec2-user/one/two/three/four. You are in four.

The task: make 22 inside two. Three ways:

  1. Absolute: mkdir /home/ec2-user/one/two/22. (mkdir -p works too; it gives no error when the parents exist.)
  2. Relative: mkdir ../../22. The first .. goes from four to three; the second goes from three to two.
  3. Tilde: mkdir ~/one/two/22.
You are in four. Make 22 inside two ~ (/home/ec2-user) one two 22new three fouryou are here five 1st .. : up to three2nd .. : up to two Absolutemkdir /home/ec2-user/one/two/22 Relative: four → three → twomkdir ../../22 Tildemkdir ~/one/two/22 Same result each time: /home/ec2-user/one/two/22.mkdir -p makes missing parents and gives no errorif they already exist.

Figure 8. Animation: you are in four. Two .. steps climb to two, and 22 appears inside two. Absolute, relative and tilde paths give the same result.

Handwritten board: a tree from / to home to ec2-user, then a chain of circles 1, 2, 3, 4, 5 with 22 branching off 2; on the right three commands: mkdir /home/ec2-user/1/2/22, mkdir ../../22, mkdir ~/1/2/22.

Whiteboard: Folders named 1 to 5 under ec2-user. From inside 4, three ways to make 22 inside 2: mkdir /home/ec2-user/1/2/22, mkdir ../../22 and mkdir ~/1/2/22.

Check: ls ~/one/two → 22 three.

Classroom line

Go back and forth like this yourself; make up your own situations.

17. Names with spaces

On Windows, "My Folder" is normal. In the shell, a space separates arguments, so it causes surprises.

  1. mkdir my folder makes two folders: my and folder.
  2. mkdir 'my folder' makes one folder named my folder. Double quotes work too: mkdir "my folder".
  3. Several names with spaces: mkdir 'my folder' 'your folder'.
  4. To go in: cd 'my folder', or type cd my and press Tab. Tab writes cd my\ folder/.
  5. The backslash \ is an escape character: it tells the shell "the next space is part of the name".
Folder names with a space mkdir my folderthe space splits it: TWO folders, my and folder my folder mkdir 'my folder'quotes keep it together: ONE folder ("my folder" works too) my folder cd my\ folderbackslash escapes the space; Tab writes this for you my folder Easiest: avoid spaces. Use my-folder or my_folder.

Figure 9. mkdir my folder makes two folders. mkdir 'my folder' makes one. cd my\ folder escapes the space; Tab writes it for you.

Worked example.

  1. mkdir 'my folder'
  2. cd my + Tab → cd my\ folder/ → Enter.
  3. mkdir 'inside folder'
  4. pwd → /home/ec2-user/my folder.
  5. ls → inside folder.

Classroom line

Go on. Create folders with spaces... Press Tab and see what happens.

Ravindra Bagale's Tip

At work, avoid spaces in file and folder names. Use my-folder or my_folder. Scripts, URLs and other people's commands will thank you.

18. Tab completion, and what to do when names start the same

On the job, nobody types long paths letter by letter. Tab is faster and makes no spelling mistakes.

  1. Type the first few letters of a name and press Tab. If only one name matches, the shell completes it.
  2. If several names match, one Tab completes only the letters they share, or does nothing.
  3. Press Tab twice to see the matching names.
  4. Type one or two more letters until only one name matches, then press Tab.

Worked example 1: tata, twelve, taja (and two).

  1. In a fresh folder: mkdir tata twelve taja two.
  2. cd t + Tab → nothing happens: four names start with t.
  3. Tab Tab → taja/ tata/ twelve/ two/.
  4. cd taj + Tab → cd taja/.
  5. cd .., then cd tat + Tab → cd tata/.

Worked example 2: amazon, amma, aman.

  1. mkdir amazon amma aman
  2. cd ama + Tab → stops at ama, because amazon and aman both start with ama.
  3. cd amaz + Tab → cd amazon/.
  4. cd aman + Tab → cd aman/.
  5. cd amm + Tab → cd amma/.
Tab completion when names start the same $ ls taja tata twelve two $ cd t [Tab] → nothing: four names start with t $ cd t [Tab][Tab] → shows the choices taja/ tata/ twelve/ two/ $ cd taj type more letters, then [Tab] $ cd taja/ completed

Figure 10. Animation: cd t + Tab does nothing (four matches); Tab Tab lists them; cd taj + Tab completes taja. In the second example ama is not unique, so type amaz or aman.

Tab completes only when the start is unique. With both amazon and aman present, ama + Tab stops at ama. Type amaz, then Tab.

19. clear and history

A screen full of old output hides the one error you need to read, so clear it. And when something breaks, history shows what you actually typed, not what you think you typed.

clear.

  1. clear (or Ctrl+L) wipes the screen.
  2. Only the screen. Your files and folders are untouched.

history.

  1. history prints a numbered list of commands you ran.
  2. Handy to see what you did, or to copy a long command again.
  3. !45 runs command number 45 again.
  4. The Up Arrow walks back through the same list.

Classroom line

While I'm explaining, don't type along... when I'm showing, just watch.

history shows only the commands of this user's shell (saved in ~/.bash_history), and a user can edit or clear it. It is not an audit log. On AWS, account actions are recorded by CloudTrail; that comes later in the course.

20. Two more SSH fixes: the port 22 rule and chmod 400

When SSH fails, the four errors in section 1 cover most cases. Two more come up often: a connection that just hangs and then says Connection timed out even though the IP is right, and a key that is refused on a Mac or Linux laptop.

Background. 1. Port 22. SSH talks to the server on TCP port 22. The instance's security group is a firewall. If its inbound rules have no rule of type SSH (TCP, port 22) from your network, AWS drops your packets silently. Your laptop keeps waiting and finally prints Connection timed out. 2. Fix: EC2 → your instance → Security tab → click the security group → Edit inbound rules → Add rule → Type SSH, Source My IP → Save rules. Try ssh again. 3. chmod 400. On Mac and Linux, ssh refuses a private key that other users could read. You see WARNING: UNPROTECTED PRIVATE KEY FILE!, then Permissions 0644 for 'firstserver.pem' are too open., then Permission denied (publickey). 4. Fix: chmod 400 firstserver.pem. Now only you can read it. Run the same ssh -i firstserver.pem ec2-user@<public IP> again. 5. On Windows PowerShell this check rarely triggers for a key in your own Downloads folder, which is why Windows users usually never meet it.

Connection to this chapter. You cannot practise a single command from this chapter until you are logged in. These two fixes, plus the four errors in section 1, cover almost every failed login you will meet.

21. Hands-on lab: pwd, ls, cd and mkdir from start to finish

Each command above was shown on its own. Here you use them together, in order, the way you will on a real server. Run every step and compare your screen with the expected output before you move on.

  1. Log in and find where you are
    • Command: pwd
    • You should see: /home/ec2-user
    • Check: The prompt ends with ~]$.
    • Why: Every login starts in your home folder. Knowing where you stand before you create anything stops files from landing in the wrong place.
  2. Make three folders in one command
    • Command: mkdir ravi raja rani
    • You should see: Nothing. The next prompt appears.
    • Check: ls → raja rani ravi
    • Why: A space separates names, so one mkdir makes three folders. No output means success; Linux only talks when something is wrong.
  3. Try to make one again
    • Command: mkdir ravi
    • You should see: mkdir: cannot create directory 'ravi': File exists
    • Check: ls still shows one ravi.
    • Why: Two items in one folder can't share a name. Reading this error now means you won't panic when a script hits it later.
  4. Build a deep tree in one line
    • Command: mkdir -p one/two/three/four/five
    • You should see: Nothing.
    • Check: ls one/two/three/four → five
    • Why: -p creates every missing parent on the way. Without it, mkdir stops with No such file or directory because one doesn't exist yet.
  5. Walk down into it
    • Command: cd one/two/three/four
    • You should see: The prompt ends with four]$.
    • Check: pwd → /home/ec2-user/one/two/three/four
    • Why: One cd with a relative path is faster than four cd commands. The prompt shows only the last folder name, so use pwd when you need the full path.
  6. Make 22 inside two, three ways
    • Command: mkdir ../../22
    • You should see: Nothing.
    • Check: ls ~/one/two → 22 three. Then rmdir ~/one/two/22 and repeat with mkdir /home/ec2-user/one/two/22 and with mkdir ~/one/two/22; each time ls ~/one/two shows 22 three.
    • Why: ../.. climbs two levels from four to two. Getting the same result with relative, absolute and ~ paths proves they are three spellings of one place.
  7. Jump home, then back
    • Command: cd
    • You should see: The prompt ends with ~]$.
    • Check: Now cd - prints /home/ec2-user/one/two/three/four and takes you back.
    • Why: cd alone always goes home, from anywhere. cd - returns to the previous folder, so you can hop between two places without typing paths.
  8. Climb up with ..
    • Command: cd ../..
    • You should see: The prompt ends with two]$.
    • Check: pwd → /home/ec2-user/one/two
    • Why: Each .. is one level up. Two of them take you from four to two.
  9. Make a folder with a space in its name
    • Command: cd && mkdir 'my folder'
    • You should see: Nothing.
    • Check: ls shows 'my folder' as one item next to one raja rani ravi.
    • Why: Quotes keep the two words together. Without them, mkdir my folder makes two folders, my and folder.
  10. Let Tab type for you
    • Do: Type cd my and press Tab.
    • You should see: The shell completes it to cd my\ folder/.
    • Check: Press Enter. The prompt ends with my folder]$.
    • Why: Tab never misspells and adds the backslash before the space for you. If nothing happens, two names start the same: press Tab twice to see them and type one more letter.
  11. Check what you did, then clean the screen
    • Command: history
    • You should see: A numbered list of every command from this lab, oldest first.
    • Check: clear empties the screen; ls ~ still shows all your folders.
    • Why: history is your record when something went wrong. clear only wipes the screen, never your files.
  12. Stop the server
    • Do: EC2 console → select the instance → Instance state → Stop instance.
    • Check: The state goes from Stopping to Stopped.
    • Why: A running instance uses free-tier hours. Stopping keeps your disk and folders for next time.
  13. Climb three levels with cd ../../.. (not four)
    • Do: From a deep folder such as ~/one/two/three, run pwd, then cd ../../.., then pwd.
    • You should see: You land in /home/ec2-user (three levels up). cd ../../../.. would go one level too far.
    • Check: Count the .. segments — each one is one folder up.
    • Why: Class mix-up: saying “three” but typing four ... Always confirm with pwd.
  14. Make 22 three ways under one/two
    • Command: mkdir -p /home/ec2-user/one/two/22 then also try mkdir ~/one/two/22 and from inside a child of two use mkdir ../../22 after removing the first.
    • You should see: No output on success. ls ~/one/two lists 22.
    • Check: Absolute, tilde, and relative paths all create the same folder.
    • Why: On the job you pick the path style that needs the fewest keystrokes from where you stand.
  15. Sideways move: ../../myfolder then back
    • Do: From ~/one/two run cd ../../myfolder (if myfolder exists under home). Then cd ../one/two to return.
    • You should see: Prompt changes to myfolder, then back to two.
    • Check: From /home, bare cd myfolder fails with No such file or directory because myfolder lives under ec2-user — use cd ec2-user/myfolder.
    • Why: Relative paths are short but start from your current folder, not from home.
  16. Reverse listing with ls -r
    • Command: ls then ls -r in a folder with several names.
    • You should see: ls A→Z; ls -r Z→A.
    • Check: Same names, opposite order.
    • Why: Handy when the file you want is at the end of a long alphabetical list.

22. Practice task

Classroom line

Don't look at someone else's screen... put in the effort yourself.

Try at home

Task 1: build and walk a tree

  1. Log in and run pwd. Write down what it shows.
  2. mkdir -p school/class1/divA school/class2
  3. cd school/class1/divA, then pwd.
  4. Go to class2 in one command, without going home. (cd ../../class2)
  5. Use cd - twice and watch where you land.
  6. Use cd alone, then pwd.

Task 2: three ways

  1. From inside divA, make notes inside school with a relative path. (mkdir ../../notes)
  2. Make exams inside school with an absolute path.
  3. Make results inside school with a ~ path.
  4. ls ~/school should show class1 class2 exams notes results.

Task 3: spaces and Tab

  1. mkdir 'my notes' 'my marks'
  2. Go into my marks using Tab. What does Tab type?
  3. Make three folders that start the same: mkdir data date dance.
  4. Practise reaching each one with the fewest letters + Tab.

Task 4: your own situations

  1. Draw a folder tree on paper, at least four levels deep.
  2. Make it with one mkdir -p line.
  3. Pick any two folders in it. Move between them with an absolute path, a relative path and a ~ path.
  4. Run history and read back what you did.
  5. When you finish, stop the instance from the console.

Classroom line

Don't forget to stop the server.

A reboot keeps the same public IP. The auto-assigned public IP changes when you stop and then start the instance (an Elastic IP stays). Also, a stopped instance still pays for its EBS disk; see the previous chapter.

Ravindra Bagale's Tip

Before every mkdir, cp or rm, ask yourself "where am I?" and run pwd if you are not sure. Most beginner mistakes happen in the wrong folder, not with the wrong command. For a compact reference of these commands, see 3.2 Navigation and 3.3 Working with files and directories.

Common errors and fixes

Error / symptomFix
mkdir: cannot create directory … File existsThat name is already taken in this folder. Pick another name or cd into the existing one.
No such file or directory on cd or mkdir (without -p)Parent folder missing, or you are in the wrong place. pwd + ls, or use mkdir -p.
cd myfolder fails from /homemyfolder is under ec2-user. Use cd ec2-user/myfolder or cd ~/myfolder.
Tab does nothing on cd tSeveral names share the prefix. Press Tab twice to list, then type more letters.
mkdir my folder made two foldersSpace splits names. Use quotes: mkdir 'my folder'.
Ended up too high after many ..Count .. levels; confirm with pwd every time.

FAQ

Q. Is ~ always /home/ec2-user?

For the Amazon Linux default user yes. For Ubuntu it is often /home/ubuntu. ~ always means “home of the user you are now”.

Q. mkdir gave no output — did it fail?

No. Linux stays quiet on success. Check with ls.

Q. What does cd - do?

Jumps to the previous directory and prints that path. Great for flipping between two folders.

Q. Absolute or relative path?

Absolute (starts with / or ~) works from anywhere. Relative is shorter when you are already nearby.

Recap

In short

  1. SSH: ssh -i <key> ec2-user@<public IP>. Use Tab for the key path. Paste with Ctrl+V or right-click.
  2. Errors: publickey (wrong key or user), no such file (key path), timed out (IP changed after stop/start).
  3. Log in as ec2-user; use sudo for admin work. The key pair isn't tied to a user; root SSH login is blocked.
  4. An idle SSH drop comes from your router, Wi-Fi or sleep, not AWS. Up Arrow + Enter reconnects; ServerAliveInterval=60 keeps it alive.
  5. / is the top of the whole tree. /root is the root user's home, a different place.
  6. You land in /home/ec2-user. pwd prints where you are.
  7. In a path, the first / is root; the others separate names.
  8. A file holds content; a folder (directory) holds files and folders.
  9. mkdir a b c makes three folders. No output = success. Same name again = File exists.
  10. ls lists; ls -r lists in reverse.
  11. cd name goes in. The prompt shows ~ at home.
  12. cd .. up one; cd ../.. up two; cd ../../.. up three.
  13. cd alone = home. cd - = back to the previous directory.
  14. mkdir -p a/b/c makes nested folders; spaces make side-by-side folders.
  15. Absolute starts with /; relative starts from where you are; ~ is your home (the shell expands it).
  16. Calendar: four ways to make week1. One to five: three ways to make 22.
  17. Spaces: quote the name or escape with \. Better: avoid spaces.
  18. Tab completes; Tab Tab lists; type more letters when names start the same.
  19. clear wipes the screen only. history lists this user's commands; it is not an audit log.
  20. Stop the server after practice. Reboot keeps the public IP; stop and start changes it.

Samjla ka? Ghari swatahachya situation banva, absolute, relative ani ~ teenhi vaapra. Pudhchya chapter madhe files.


Ravindra Bagale, trainer: linkedin.com/in/ravindra-bagale. The folder names (ravi, calendar, india/maharashtra/pune, one to five, tata, amazon), the user name ravi and the addresses 3.3.3.3 and 172.31.5.10 are examples for learning. Command outputs were checked with GNU bash and coreutils, the same tools Amazon Linux 2023 uses; AWS behaviour is from AWS's own documentation as of October 2026.

Worked example: read the path before you type the command

You are in /home/ec2-user on an Amazon Linux server. A teammate says, “Put the notes in report.” Which report? Draw the path as a route before you move.

mkdir -p "$HOME/path-practice/report draft/notes"
cd "$HOME/path-practice/report draft"
pwd
cd notes
pwd
cd ..
pwd

What to see. The three paths end in report draft, report draft/notes and report draft. Double quotes keep the folder name with a space together. notes is relative to the current directory. $HOME/path-practice/report draft/notes resolves from your home directory regardless of where you start.

Try it. Run cd /, then try cd notes. Confirm with pwd that the failed command did not change location. Return to the practice folder using its full path. Use rmdir to remove the three practice directories once they are empty.

Interview question: why do scripts work in one directory and fail in another?

A relative path uses the process's working directory. A script started by a service can have a different working directory from your SSH shell. Set that directory explicitly or construct the intended full path.

Reference 1 · Reference 2

Practise this chapter

Interview questions and answers

Why can cd / work for a normal user without giving the user administrative access?

The slash is the starting directory of the filesystem. The current user is a separate property that decides which paths the process can access. You can see that distinction with pwd and whoami before and after cd.

A teammate pastes mkdir notes into two different terminal windows and gets two new directories. Why?

A relative name starts from each window’s current working directory. The two shells were standing in different places. Run pwd in both windows and either move to the intended parent or use the complete absolute path.

Check your understanding

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

0 of 3 answered correctly
1. Your prompt is logged in as ec2-user. You run cd /. Who are you now?

Answer and explanation

B. Still ec2-user, with / as your current directory

Changing the current directory changes the starting point for relative paths. It does not change which user runs your commands.

2. You are in /home/ec2-user/calendar/weeks. Which relative path leads to /home/ec2-user/calendar/months?

Answer and explanation

D. ../months

Two dots move up from weeks to calendar. The next component selects the months directory under calendar.

3. In a new lab folder, neither project nor project/src exists. Which command creates project/src/css in one step?

Answer and explanation

A. mkdir -p project/src/css

The -p option creates the parent directories on the way to the requested folder. Plain mkdir needs the parent path to exist.

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

Nairobi Ride is a transport business in Nairobi. Wanjiru Kamau looks after its IT and cloud work. They count money in KES and run their AWS resources in the af-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 / 12

Easy List all files with details

Hidden files (like .env) and file sizes or owners only show with the right options.

Show a long listing of every file, including hidden ones, in /var/www/nairobiride.

  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 nested folders in one go

Projects need a clean folder tree. Making all levels in one command saves time and avoids errors.

Create the folder /srv/nairobiride/reports/2026 for Nairobi Ride. The parent folders do not exist yet, so create them too.

  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 Show where you are

Before you copy or delete anything, you must know which folder you are in.

Wanjiru Kamau logged in to the Nairobi Ride server and is not sure which folder the terminal is in. Print the current folder 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 Read a config file

Reading a file on the screen is the quickest way to check its settings.

Print the whole file /etc/nairobiride/app.conf on the screen.

  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

Some apps need a file to exist before they can write to it.

Create an empty file named orders-kes.csv 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 with everything inside

A plain copy skips folders. You need the recursive option to copy a whole tree.

Copy the folder site to a new folder named site-backup (both 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 Rename a file

In Linux, renaming is the same as moving a file to a new name.

Rename the file report.txt to report-nairobi.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 Go into the logs folder

Moving into a folder makes the next commands shorter.

Move into the folder /var/log/nairobiride.

  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 List Availability Zones

Spreading servers across AZs keeps you up if one data centre fails.

List the Availability Zones in af-south-1.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy List your EC2 instances

Before changing anything, see what is running and in which state.

Show details of all EC2 instances of Nairobi Ride in af-south-1.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy List all Regions

Choose a Region close to your users for low delay and the right laws.

List the AWS Regions available to your account.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Delete an old folder

Old build folders waste disk. rm needs the recursive option for folders, and force skips the questions.

Delete the folder old-build and everything inside it without being asked to confirm 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.

Quick revision cards

Revise this lesson in two minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens its lesson.

Comparison card: File vs Folder (directory). File: Holds data: text, code, images. Folder (directory): Holds files and other folders.
Comparison: File vs Folder (directory) · Download PNG
Revision card: The Linux tree. Every file has one address. Without the tree you get lost on a server. Everything starts at / (root). Folders branch from it, like /home/ec2-user. pwd shows where you are; ls shows what is here. After login, pwd prints:
Revision card: The Linux tree · Download PNG
Revision card: Move around with cd. Commands work on the current folder, so you must reach the right one. cd folder goes in. cd .. goes up one. cd alone goes home. cd - goes back. Chain ups with slashes: cd ../.. goes up two levels. From /home/ec2-user/a/b:
Revision card: Move around with cd · Download PNG
Revision card: Absolute vs relative paths. The same file can be named two ways. Knowing both saves typing and mistakes. Absolute starts at / and works anywhere. Relative starts from where you are. ~ means your home. Use absolute in scripts, relative for quick work. Same folder:
Revision card: Absolute vs relative paths · Download PNG
Revision card: mkdir -p: nested folders. mkdir a/b/c fails if a does not exist. -p creates every missing parent in one go and never errors if it exists. Combine with braces for many folders at once. Calendar tree:
Revision card: mkdir -p: nested folders · Download PNG
Comparison: cd .. vs cd -. cd ..: Goes to the parent folder, one level up. cd -: Goes back to the folder you were in before.
Comparison: cd .. vs cd - · Download PNG
Interview card: What is the difference between an absolute and a relative path?. Absolute starts at /; relative starts from the current folder. Absolute works from anywhere, so scripts use it. /var/www/html vs ../html.
Interview card: What is the difference between an absolute and a relative path? · Download PNG
Interview card: What do cd, cd - and cd .. do?. cd alone: home. cd -: previous folder. cd ..: one level up. They save typing long paths. /etc › cd /var/log › cd - › back in /etc.
Interview card: What do cd, cd - and cd .. do? · Download PNG
Interview card: How do you work with folder names that have spaces?. Put the name in quotes or escape each space with \. Otherwise the shell sees two separate names. Both work:
Interview card: How do you work with folder names that have spaces? · Download PNG

More cards: Ravi Sir's Cards.