A Guide to Unix/Linux Shell

A Practical Guide to the Unix/Linux Shell

Here is a ranked guide to the most important commands and tricks for the Linux/Unix shell. The commands are grouped by function, starting with the most common and fundamental. This guide shows you the command syntax and then demonstrates the effect.

On a unix-like OS there are arround 100 commands pre-installed (depending on the OS, derivative, installation setup, etc). During a system lifetime, there may be thousands of commands added to the system with various package managersquora link how many commands.


1. File and Directory Management (The Essentials)

These are the commands you’ll use every day to navigate and organize your filesystem.

ls - List Directory Contents

Lists files and directories. Used on its own, it’s simple. With options, it’s powerful.

Syntax:

ls [options] [path]

Effect/Examples:

  • Simple listing:
    # Command
    ls
      
    # Example Output
    # Documents  Downloads  Music  Pictures  Public
    
  • Detailed list (-l), including hidden files (-a): The -la combination is one of the most common.
    # Command
    ls -la
      
    # Example Output
    # drwxr-xr-x  15 rvamos staff   480 Oct  6 21:20 .
    # drwxr-xr-x   5 root   admin   160 Sep 15 10:00 ..
    # -rw-r--r--   1 rvamos staff  3102 Oct  1 09:30 .bash_profile
    # drwx------  35 rvamos staff  1120 Oct  6 18:00 Documents
    

pwd - Print Working Directory

Prints the full, absolute path of the directory you are currently in.

Syntax:

pwd

Effect:

# Command
pwd

# Example Output
# /home/rvamos/documents/projects

cd - Change Directory

Navigates into another directory.

Syntax:

cd [directory_path]

Effect/Examples:

# Go to a specific directory
cd /home/rvamos/Documents

# Go up one level
cd ..

# Go to your home directory
cd

# Go to the previous directory you were in
cd -

cat - Concatenate and Display Files

The cat command reads file contents and prints them to standard output. It’s great for quickly viewing small files.

Syntax:

cat [filename]

Effect:

# Command
cat /etc/hosts

# Example Output
# 127.0.0.1   localhost
# ::1         localhost

2. System Monitoring and Process Management

Commands to check on your system’s health and manage running programs.

top - Display and Manage Processes Interactively

Provides a real-time, dynamic view of the running processes on your system. It’s an essential tool for identifying resource-heavy tasks.

Syntax:

top

Effect: Launches an interactive screen showing a live-updating list of processes sorted by CPU usage. You can press q to quit.

# Command
top

# Example Output (truncated)
# top - 21:22:02 up 10 days,  4:20,  1 user,  load average: 0.65, 0.75, 0.85
# Tasks: 410 total,   1 running, 320 sleeping,   0 stopped,   0 zombie
# %Cpu(s):  2.5 us,  1.0 sy,  0.0 ni, 96.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
#   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
#  9211 rvamos    20   0   3.5g   250m   100m S   8.5   3.1   1:30.15 firefox
#  1234 root      20   0   1.2g   150m    50m S   2.0   1.8   0:45.10 Xorg

ps, pidof, and kill - List and Terminate Processes

These commands work together to manage processes non-interactively.

  1. Find the process:
    # Command: List your processes and filter for "firefox"
    ps aux | grep "firefox"
        
    # Command: Get the PID directly
    pidof firefox
    # Example Output: 9211
    
  2. Terminate the process:
    # Command (Polite shutdown):
    kill 9211
        
    # Command (Force quit):
    kill -9 9211
    

3. Searching for Text and Files

grep - Search for Strings in Text

Searches for patterns in text files or streams.

Syntax:

grep [options] "search_string" /path/to/file

Effect: This command recursively (-r) and case-insensitively (-i) searches for “database_url” in the current directory, but only in files ending with .yml.

# Command
grep -ri "database_url" . --include="*.yml"

# Example Output
# ./config/production.yml:  database_url: postgres://...

find and locate - Search for Files

  • find: A powerful, versatile tool for searching based on name, type, size, time, etc.
  • locate: A much faster but less flexible tool that uses a pre-built database.

Effect/Examples:

# Find all files modified in the last 24 hours in the current directory
find . -mtime -1

# Quickly find the path to your 'bash_history' file
locate bash_history

4. Remote Access and File Transfer

Commands for securely connecting to and exchanging files with other machines.

ssh - Secure Shell

Connects to a remote machine and lets you execute commands there securely.

Syntax:

ssh [user]@[hostname_or_ip]

Effect: This command opens a secure shell session for the user admin on the server at 192.168.1.100. You will be prompted for a password.

# Command
ssh admin@192.168.1.100

scp - Secure Copy

Copies files between machines over a secure ssh connection.

Syntax:

# Copy from local to remote
scp /path/to/local/file.txt user@remote:/path/to/destination/

# Copy from remote to local
scp user@remote:/path/to/remote/file.txt /path/to/local/destination/

Effect: This command copies the local file backup.zip to the /tmp directory on the remote server.

# Command
scp ./backup.zip admin@192.168.1.100:/tmp/

5. Advanced Techniques: I/O Redirection and Pipes

Redirecting Output (>, >>, 2>)

Control where a command’s output and errors go.

Effect/Example: Run a script, send its normal output to output.log, and send any errors to errors.log.

# Command
./my_script.sh > output.log 2> errors.log

Piping Commands (|) and tee

Chain commands together, using the output of one as the input for the next.

Effect/Example: Count the number of files in the current directory. ls -1 lists one file per line, and wc -l counts the lines.

# Command
ls -1 | wc -l

The tee command lets you save the output while also passing it to the next command or viewing it.

# Command: See the process list and also save it to a file
ps aux | tee process_list.txt

Written by Ralf //