Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux Shell Commands Explained: A Beginner's Guide

· 5 min read · Home Tech

In this article
  1. What Is the Linux Shell?
  2. How Shell Commands Work
  3. Essential Commands You Should Know
  4. Combining Commands with Pipes and Redirection
  5. Common Mistakes and How to Avoid Them
  6. Shell Scripting Basics
  7. Frequently Asked Questions

Shell commands are the text-based instructions you type into a Linux terminal to control the operating system, from listing files to installing software. The shell is a command-line interpreter that reads these commands and tells the kernel what to do, making it a powerful alternative to graphical interfaces. This guide explains how shell commands work, how to use them effectively, and answers common questions for newcomers.

What Is the Linux Shell?

The shell is a program that provides a user interface to the operating system. When you open a terminal emulator (like GNOME Terminal or Konsole), you are running a shell inside it. The shell waits for your input, parses the command line, and executes the requested program or built-in function. Common Linux shells include Bash (the default on most distributions), Zsh, and Fish, but Bash is the most widely used and the focus of this article.

Each command you type is either a built-in shell command (like cd or echo) or an external program located in directories listed in the PATH environment variable (like ls or grep). The shell also supports scripting, allowing you to combine multiple commands into a file for automation.

How Shell Commands Work

When you type a command and press Enter, the shell performs several steps:

  1. Parsing: The shell splits the line into words based on spaces and interprets special characters like quotes and pipes.
  2. Expansion: It expands variables (e.g., $HOME), wildcards (e.g., *.txt), and command substitutions (e.g., $(date)).
  3. Execution: The first word is treated as the command name. If it’s a built-in, the shell runs it directly. Otherwise, it searches the PATH for an executable file.
  4. Input/Output: The command receives any arguments and can read from standard input (keyboard) and write to standard output (screen). Redirection operators like > and | change where output goes.

For example, the command ls -l /home runs the ls program with the option -l (long format) and the argument /home. The shell finds /bin/ls and executes it, passing those arguments.

Essential Commands You Should Know

While there are hundreds of commands, a small set covers most daily tasks:

  • File operations: ls (list), cp (copy), mv (move/rename), rm (delete), mkdir (make directory), touch (create empty file).
  • Viewing files: cat (print whole file), less (paginated view), head / tail (first/last lines).
  • Text processing: grep (search), sed (stream editor), awk (pattern scanning).
  • System info: ps (processes), df (disk usage), free (memory), uname -a (kernel details).
  • Package management: apt (Debian/Ubuntu), dnf (Fedora), pacman (Arch).

Each command typically has a manual page accessible with man command (e.g., man ls). Use --help for a quick summary, like ls --help.

Combining Commands with Pipes and Redirection

One of the shell’s strengths is connecting commands. The pipe operator | sends the output of one command as input to another. For example, ls -l | grep "txt" lists files and then filters for lines containing “txt”. Redirection operators control input/output:

  • > redirects standard output to a file, overwriting it (e.g., echo "Hello" > file.txt).
  • >> appends to a file instead of overwriting.
  • < reads input from a file (e.g., sort < list.txt).
  • 2> redirects error messages (stderr) to a file.

These tools allow complex workflows, like cat access.log | awk '{print $1}' | sort | uniq -c to count unique IP addresses.

Common Mistakes and How to Avoid Them

Beginners often run into the same pitfalls:

  • Using rm carelessly: rm -rf deletes directories recursively and forcefully with no confirmation. Always double-check the path. Consider using rm -i for interactive confirmation.
  • Forgetting spaces: cd /home/user is correct; cd/home/user will fail because the shell sees a single word.
  • Ignoring quotes: Filenames with spaces need quotes, e.g., cat "My File.txt". Without quotes, the shell treats it as two arguments.
  • Running commands as root unnecessarily: Using sudo for every command risks damaging the system. Use it only when needed.
  • Not reading error messages: “Permission denied” or “command not found” usually points to the issue. Check permissions with ls -l and verify the command name.

If a command seems stuck, press Ctrl+C to interrupt it, or Ctrl+Z to suspend it and bring it back with fg.

Shell Scripting Basics

Shell commands can be saved in a file to create a script, which is a program that runs multiple commands sequentially. A basic script starts with a shebang line #!/bin/bash, then contains commands. For example, a script to back up a directory might look like:

#!/bin/bash
cp -r /home/user/documents /home/user/backup
echo "Backup completed"

To run it, make it executable with chmod +x script.sh and execute with ./script.sh. Scripts support variables, loops, conditionals, and functions, making them a full programming language. Learning basic scripting greatly expands what you can automate.

Frequently Asked Questions

What is the difference between a shell and a terminal?

A terminal (or terminal emulator) is the graphical window that displays text and accepts input. The shell is the program running inside that window that interprets commands. You can run different shells (Bash, Zsh) in the same terminal.

How do I find out which shell I am using?

Run echo $SHELL to see the default shell for your user, or ps -p $$ to see the shell currently running. The output will show something like /bin/bash.

What does “command not found” mean?

It means the shell could not locate an executable file with that name in your PATH. Check for typos, verify the package is installed, or use the full path (e.g., /usr/bin/command).

How can I see all available commands?

Press Tab twice after typing a partial command to see completions. Use compgen -c to list all commands, or type -a command to see where a specific command is located.

Is it safe to experiment with shell commands?

Yes, but start in a virtual machine or a disposable container. Avoid running destructive commands like rm -rf / or formatting drives. Use man pages to understand what a command does before running it, and test on dummy files.

What is the best way to learn shell commands?

Practice daily with real tasks, like managing files or checking system status. Read the manual pages, follow online tutorials, and try writing small scripts. The more you use the shell, the more natural it becomes.