Shell Course 1

Chapter 1: Shell Basics

## What is the shell?

The shell is a text-based interface that allows you to interact with your operating system. Think of it as a command interpreter: you type instructions, and the shell executes them for you. It’s like having a conversation with your computer where you tell it exactly what you want it to do.

Unlike the graphical user interfaces (GUIs) you’re probably used to, the shell offers a more direct and often more powerful approach to working with your system. It’s the tool of choice for developers, system administrators, and power users.

Why learn the shell?

  • Efficiency: Performing certain tasks is much faster via command line
  • Automation: Easily repeat complex tasks
  • Remote access: Control servers or remote machines
  • Power: Access features not available through graphical interfaces

The shell is like a digital Swiss Army knife: it might seem intimidating at first, but once you master the basics, you’ll wonder how you ever managed without it.


## First steps with the shell

The first time you open a terminal, you’re greeted with what’s called the prompt. It typically looks something like:

1
username@hostname:~$

This prompt tells you:

  • Your username (username)
  • Your machine’s name (hostname)
  • Your current directory (~, which is a shortcut for your home folder)
  • The $ symbol marking the end of the prompt (on some systems, this might be % or >)

This is where the magic happens. You can now type commands and press Enter to execute them.


## Essential commands to get started

Let’s begin with the basic commands that every shell user should know. These are the foundations upon which you’ll build your expertise.

1. pwd - Print Working Directory

The pwd command tells you which directory you’re currently in.

1
2
$ pwd
/home/username

This is equivalent to asking “Where am I?” to the system. Particularly useful when navigating between different folders.

2. ls - List

The ls command displays the contents of the current directory.

1
2
$ ls
Documents Downloads Music Pictures Videos

Useful variations:

  • ls -l: Displays details (permissions, size, modification date)

    1
    2
    3
    4
    5
    6
    7
    $ ls -l
    total 28
    drwxr-xr-x 2 username group 4096 Mar 4 10:15 Documents
    drwxr-xr-x 2 username group 4096 Mar 4 10:15 Downloads
    drwxr-xr-x 2 username group 4096 Mar 4 10:15 Music
    drwxr-xr-x 2 username group 4096 Mar 4 10:15 Pictures
    drwxr-xr-x 2 username group 4096 Mar 4 10:15 Videos
  • ls -a: Shows all files, including hidden ones (starting with .)

    1
    2
    $ ls -a
    . .. .bash_history .bashrc Documents Downloads Music Pictures Videos
  • ls -lh: Like -l, but with human-readable sizes (MB, GB, etc.)

    1
    2
    3
    4
    5
    6
    7
    $ ls -lh
    total 28K
    drwxr-xr-x 2 username group 4.0K Mar 4 10:15 Documents
    drwxr-xr-x 2 username group 4.0K Mar 4 10:15 Downloads
    drwxr-xr-x 2 username group 4.0K Mar 4 10:15 Music
    drwxr-xr-x 2 username group 4.0K Mar 4 10:15 Pictures
    drwxr-xr-x 2 username group 4.0K Mar 4 10:15 Videos

Tip: You can combine options! For example, ls -lha shows all files with their details and human-readable sizes.

3. cd - Change Directory

The cd command allows you to navigate between directories.

1
2
3
$ cd Documents
$ pwd
/home/username/Documents

Essential navigation shortcuts:

  • cd ~ or simply cd: Returns to your home directory
  • cd ..: Moves up one level in the directory tree
  • cd -: Returns to the previous directory

Navigation example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ pwd
/home/username
$ cd Documents
$ pwd
/home/username/Documents
$ cd ..
$ pwd
/home/username
$ cd /etc
$ pwd
/etc
$ cd -
$ pwd
/home/username

4. mkdir - Make Directory

The mkdir command creates a new directory.

1
2
3
$ mkdir Projects
$ ls
Documents Downloads Music Pictures Projects Videos

To create a nested directory structure in a single command, use the -p option:

1
2
3
4
5
6
7
8
9
$ mkdir -p Projects/Web/HTML
$ ls -R Projects
Projects:
Web

Projects/Web:
HTML

Projects/Web/HTML:

5. touch - Create an empty file

The touch command creates an empty file or updates the modification date of an existing file.

1
2
3
4
5
$ touch Projects/notes.txt
$ ls -l Projects
total 4
-rw-r--r-- 1 username group 0 Mar 4 10:30 notes.txt
drwxr-xr-x 2 username group 4096 Mar 4 10:25 Web

6. cat - Concatenate and print files

The cat command displays the contents of a file. For small files, it’s a quick way to see what they contain.

1
2
3
$ echo "Hello, Shell!" > hello.txt
$ cat hello.txt
Hello, Shell!

Note: echo "text" > file is a quick way to write text to a file.

7. man - Manual

The man command displays the manual for a command. It’s your best friend for learning how to use a command or discovering its options.

1
$ man ls

To exit the manual, press q.

Tip: If you’re not sure of the exact name of a command, you can use apropos followed by a keyword to search for related commands.

1
2
3
4
5
$ apropos directory
basename (1) - strip directory and suffix from filenames
...
mkdir (1) - make directories
...

8. clear - Clean the screen

The clear command erases the content of your terminal, giving you a clean slate. This is particularly useful when your terminal becomes too cluttered.

1
$ clear

Keyboard shortcut: Ctrl + L does the same thing in most shells.


## Getting help

Even experts sometimes need help. Here’s how to get it:

  1. Built-in documentation: Most commands accept the --help or -h option

    1
    $ ls --help
  2. Manual pages: As seen earlier with man

    1
    $ man mkdir
  3. The whatis command: Gives a very brief description of a command

    1
    2
    $ whatis ls
    ls (1) - list directory contents
  4. The info command: Similar to man, but often more detailed

    1
    $ info ls

Don’t be afraid to explore! If you’re not sure what a command does, check its documentation before running it.


## Practical exercises

To help solidify these concepts, here are some simple exercises:

  1. Open a terminal and determine which directory you’re in.
  2. List the files in your home directory, including hidden files.
  3. Create a directory named ShellPractice.
  4. Navigate to this directory.
  5. Create an empty text file named notes.txt.
  6. Add the phrase “I’m learning the shell!” to this file (hint: use echo and >).
  7. Display the contents of your file.
  8. Return to your home directory.
  9. Check the manual for the cp command (we’ll use it in the next chapter).

Solution:

1
2
3
4
5
6
7
8
9
$ pwd
$ ls -a
$ mkdir ShellPractice
$ cd ShellPractice
$ touch notes.txt
$ echo "I'm learning the shell!" > notes.txt
$ cat notes.txt
$ cd ~
$ man cp

## Conclusion

In this first chapter, we’ve explored the fundamentals of the shell. These basic commands form the foundation upon which we’ll build in the coming chapters. Though simple, they constitute a large part of your daily interaction with the terminal.

In the next chapter, we’ll delve deeper into file navigation and manipulation, learning how to copy, move, and delete files, as well as understanding permissions and access rights.

Remember: practice is key! Try to use these commands regularly until they become second nature.