Shell Course 1
Chapter 1: Shell Basics
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.
- 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 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>
)
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 | $ pwd |
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 | $ ls |
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 Videosls -a
: Shows all files, including hidden ones (starting with.
)1
2$ ls -a
. .. .bash_history .bashrc Documents Downloads Music Pictures Videosls -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
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 | $ cd Documents |
cd ~
or simplycd
: Returns to your home directorycd ..
: Moves up one level in the directory treecd -
: Returns to the previous directory
1 | $ pwd |
4. mkdir
- Make Directory
The mkdir
command creates a new directory.
1 | $ mkdir Projects |
To create a nested directory structure in a single command, use the -p
option:
1 | $ mkdir -p 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 | $ touch Projects/notes.txt |
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 | $ echo "Hello, Shell!" > hello.txt |
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.
apropos
followed by a keyword to search for related commands.
1 | $ apropos directory |
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 |
Even experts sometimes need help. Here’s how to get it:
Built-in documentation: Most commands accept the
--help
or-h
option1
$ ls --help
Manual pages: As seen earlier with
man
1
$ man mkdir
The
whatis
command: Gives a very brief description of a command1
2$ whatis ls
ls (1) - list directory contentsThe
info
command: Similar toman
, but often more detailed1
$ info ls
To help solidify these concepts, here are some simple exercises:
- Open a terminal and determine which directory you’re in.
- List the files in your home directory, including hidden files.
- Create a directory named
ShellPractice
. - Navigate to this directory.
- Create an empty text file named
notes.txt
. - Add the phrase “I’m learning the shell!” to this file (hint: use
echo
and>
). - Display the contents of your file.
- Return to your home directory.
- Check the manual for the
cp
command (we’ll use it in the next chapter).
1 | $ pwd |
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.