OS: Linux Command Line Basics

When working with Linux, the terms terminal and shell are often used, sometimes interchangeably. However, they refer to different but related components of the command-line environment.

Terminal vs Shell

Understanding the distinction will help you navigate Linux more effectively.

Terminal

The terminal is the program that provides a window or interface where you can interact with the shell. In the past, this referred to physical terminals (keyboards and screens connected to mainframes). Today, it usually means a terminal emulator, a software application that mimics those terminals inside a desktop environment.

Examples of terminal emulators:

  • GNOME Terminal (GNOME desktop)
  • Konsole (KDE Plasma)
  • xterm (lightweight classic emulator)
  • Alacritty, Terminator, Tilix (modern alternatives)

In short: The terminal displays what you type and what the shell returns.

Shell

The shell is the actual command interpreter that takes the text you type and executes it. It is a program running inside the terminal.

Popular Linux shells include:

  • Bash (Bourne Again SHell, the most common)
  • Zsh (extended features, customization, used in macOS by default now)
  • Fish (user-friendly, with auto-suggestions)
  • Dash, Ksh, Tcsh (used in specific systems or for scripting)

The shell is what understands commands like ls, cd, pwd, and runs them.

How They Work Together

  • The terminal is the “container” (the screen/program).
  • The shell is the “engine” (the interpreter that runs your commands).

Example: When you open GNOME Terminal and type ls:

  1. The terminal captures your keystrokes.
  2. The shell (e.g., Bash) interprets ls.
  3. The shell executes the command and sends output back.
  4. The terminal displays the result on screen.

In summary:

  • Terminal = interface (window/emulator)
  • Shell = interpreter (command processor)

One of the most important skills in Linux is learning how to move around the filesystem using the command line. Unlike a graphical file manager (where you click folders), the Linux shell uses commands to display where you are, list files, and change directories.

`pwd` – Print Working Directory

The pwd command shows your current location in the filesystem.

  • Example:
  • bash
    $ pwd /home/amr/Documents

  • Meaning: You are currently inside the Documents directory of user _amr_.

`ls` – List Files and Directories

The ls command lists the contents of the current directory.

  • Basic usage:
  • bash
    $ ls file1.txt file2.png project/

  • Useful options:
  • ls -l → detailed list (permissions, owner, size, date).
  • ls -a → shows hidden files (those starting with .).
  • ls -lh → human-readable sizes.

`cd` – Change Directory

The cd command lets you move into another directory.

  • Examples:
  • bash
    $ cd Documents     # go into Documents
    $ cd ..            # go up one level (parent directory) 
    $ cd /etc          # go to /etc folder (absolute path) 
    $ cd ~             # go to your home directory 
    $ cd -             # go back to previous directory

Putting It All Together

A typical workflow when navigating might look like this:

bash
$ pwd 
/home/amr 
$ ls 
Documents Downloads Pictures 
$ cd Documents 
$ pwd 
/home/amr/Documents 
$ ls -l 
-rw-r--r-- 1 amr amr 1200 Sep 25 notes.txt 
drwxr-xr-x 2 amr amr 4096 Sep 25 projects

Here, you:

  1. Checked where you are with pwd.
  2. Listed the files in your home directory with ls.
  3. Entered the Documents folder with cd Documents.
  4. Confirmed your new location with pwd.
  5. Listed detailed contents with ls -l.

Mastering these three commands — pwd, ls, cd — gives you the foundation to explore any Linux system confidently.


File Operations (cp, mv, rm, cat, nano, vim)

After learning to navigate the Linux filesystem, the next step is working with files and directories. Linux provides powerful commands to create, copy, move, delete, and edit files.

`cp` – Copy Files and Directories

The cp command creates a duplicate of a file or directory.

  • Copy a file:
  • bash
    cp file.txt backup.txt

  • Copy a directory (with contents):
  • bash
    cp -r project/ project_backup/

`mv` – Move or Rename Files

The mv command moves files to another location or renames them.

  • Rename a file:
  • bash
    mv oldname.txt newname.txt

  • Move a file:
  • bash
    mv file.txt /home/amr/Documents/

`rm` – Remove Files and Directories

The rm command deletes files permanently (no recycle bin). Use with caution!

  • Remove a file:
  • bash
    rm file.txt

  • Remove a directory and its contents:
  • bash
    rm -r folder_name

    Warning: Once removed, files cannot be easily recovered.


`cat` – View File Contents

The cat command displays the contents of a text file directly in the terminal.

  • Example:
  • bash
    cat notes.txt

For long files, combine with less or more:

bash
cat longfile.txt | less

Editing Files in Linux

`nano` – Simple Text Editor

Nano is a beginner-friendly text editor that works directly in the terminal.

  • Open a file in nano:
  • bash
    nano notes.txt

  • Save changes: CTRL + O
  • Exit: CTRL + X

`vim` – Advanced Text Editor

Vim is a powerful but complex editor. It has different modes (insert, command).

  • Open a file in vim:
  • bash
    vim notes.txt

  • Switch to insert mode: press i
  • Write text, then press ESC to return to command mode
  • Save and exit: :wq
  • Quit without saving: :q!

Summary

  • cp → copy files
  • mv → move/rename files
  • rm → delete files
  • cat → display file contents
  • nano → simple editor
  • vim → advanced editor

Getting Help (man, --help)

Linux comes with extensive built-in documentation and help options. Instead of memorizing every command, you can use the system itself to learn what a command does and how to use it.

`man` – The Manual Pages

The man command shows the manual page (documentation) for a command.

  • Example:
  • bash
    man ls

    This opens the manual for the ls command, explaining options like -l or -a.

  • Navigation inside man:
  • Use arrow keys or Page Up/Page Down to scroll.
  • Press q to quit.
  • Search for a word: type /keyword and press Enter.
  • Manual pages are grouped into sections (commands, system calls, configuration files, etc.). For example:
  • bash
    man 5 passwd

Shows documentation about the passwd file format (section 5).

`--help` Option

Most commands also support the --help option, which gives a short description and lists available flags.

  • Example:
  • bhas
    ls --help

    Displays all options for ls directly in the terminal.

  • Useful when you need a quick reference without browsing the full manual.

Combining Both

  • Use --help when you need quick syntax reminders.
  • Use man when you want detailed explanations and examples.

🛠️ Extra Tools for Help

  • whatis → one-line description of a command.
  • bash
    whatis grep

  • apropos → search for commands related to a keyword.
  • bash
    apropos network

In summary: Linux has help built right in. man = detailed manuals, --help = quick options, and tools like whatis and apropos make discovery easier.