Essential Linux Commands Lecture Notes

Jun 27, 2024

Linux Commands Lecture by Colt Steele

Overview

Colt Steele, an experienced developer and instructor, teaches about 50+ essential Linux commands applicable to Linux, Mac OS, and other Unix-like environments. The lecture includes practical tips, explanations of command structures, and real-world applications.

Introduction

  • Colt’s background: Developer and teacher, known for in-person and online web development bootcamps.
  • Acknowledgement to Flavio Copez for the Linux Commands Handbook used in the lecture.
  • Lecture goal: Cover 50+ useful Linux commands, starting with basics (navigation, file management) and moving to advanced concepts.

Why Learn Linux Commands?

  1. Control: More direct control over your system compared to GUI.
  2. Speed: Faster execution of tasks once you learn the commands.
  3. Automation: Automate repetitive tasks using scripts.
  4. Availability: Standardized across Unix-like systems, essential for developers, sysadmins, engineers, etc.
  5. Requirement: Many tech roles require command line proficiency (e.g., web developers, data scientists).
  6. Cloud Computing: Many services necessitate command line interaction.
  7. Historical Context: Commands are shared across Unix-descended systems like Linux, macOS (due to their Unix heritage).

Key Concepts and Setup

  • Unix and Linux Origins: Unix inspired the development of many OS including Linux; Linux distributions (distros) vary but share core commands (Ubuntu, Fedora, etc.).
  • Shells: Interface between user and OS; common shells include bash, zsh (default on macOS), and fish.
  • Commands Structure: Composed of the command, options (flags), and arguments.
  • Installation: Setup guidance for Windows (WSL installation required) and Linux/Mac (terminals pre-installed).

Basic Commands

Who Am I?

  • Command: whoami
  • Purpose: Prints the currently logged-in user’s name.

Man Pages

  • Command: man [command]
  • Purpose: Displays the manual page for a command.
  • Navigation: Use space to scroll down, q to exit.

Clear Screen

  • Command: clear
  • Purpose: Clears terminal screen.
  • Shortcut: Ctrl + L

Print Working Directory (PWD)

  • Command: pwd
  • Purpose: Displays current directory path.

List Directory Contents (LS)

  • Command: ls
  • Purpose: Lists files and directories.
  • Options: -l (detailed list), -a (include hidden files), -la (combine).

Change Directory (CD)

  • Command: cd [path]
  • Purpose: Changes the current directory.
  • Special paths: .. (parent directory), ~ (home directory).
  • Example: cd ~/Documents

Make Directory (MKDIR)

  • Command: mkdir [directory]
  • Purpose: Creates a new directory.
  • Option: -p (makes parent directories as needed).

Create Files (TOUCH)

  • Command: touch [file]
  • Purpose: Creates an empty file or updates timestamp on an existing file.

Remove Directory (RMDIR)

  • Command: rmdir [directory]
  • Purpose: Deletes an empty directory.

Remove Files (RM)

  • Command: rm [file]
  • Purpose: Removes files or directories.
  • Options: -r (recursive for directories), -v (verbose), -i (interactive).

Open Files (MAC ONLY)

  • Command: open [file/app]
  • Purpose: Opens file or app (works on macOS).
  • Alternative on Linux: xdg-open

Intermediate Commands

Move/Rename Files (MV)

  • Command: mv [source] [destination]
  • Purpose: Moves or renames files and directories.

Copy Files (CP)

  • Command: cp [source] [destination]
  • Purpose: Copies files and directories.
  • Option: -r (recursive for directories).

View File Contents (HEAD and TAIL)

  • Commands:
    • head [file] (first 10 lines)
    • tail [file] (last 10 lines)
  • Purpose: Views the beginning or end of files.
  • Options: -n [number] (specify number of lines), -f (follow for tail).

Print Text (ECHO)

  • Command: echo [text]
  • Purpose: Prints text to terminal or writes to a file using redirect (>).

Word Count (WC)

  • Command: wc [file]
  • Purpose: Displays number of lines, words, and bytes in a file.
  • Options: -l (lines), -w (words).

Piping

  • Operator: |
  • Purpose: Pipes output of one command as input to another.

Sort

  • Command: sort [file]
  • Purpose: Sorts lines in a file.
  • Options: -n (numerical), -r (reverse), -u (unique).

Unique (UNIQ)

  • Command: uniq [file]
  • Purpose: Filters out adjacent duplicate lines.
  • Options: -d (only duplicates), -u (non-duplicates), -c (counts occurrences).
  • Requires: Files often need sorting first.

Find

  • Command: find [path] [criteria]
  • Purpose: Searches for files and directories meeting specified criteria.
  • Common Criteria: -name, -type, -size, -mtime.
  • Example: find . -name '*.txt'

Grep

  • Command: grep [pattern] [file]
  • Purpose: Searches for text patterns within files.
  • Options: -i (case insensitive), -r (recursive), -n (line numbers).
  • Supports: Regular expressions.

Disk Usage (DU)

  • Command: du [file/folder]
  • Purpose: Shows disk usage of files and directories.
  • Options: -h (human-readable), -s (summary).

Disk Free (DF)

  • Command: df
  • Purpose: Shows amount of free disk space on filesystems.
  • Option: -h (human-readable).

Advanced Commands

History

  • Command: history
  • Purpose: Lists command history.
  • Recall command: !number
  • Search History: history | grep [pattern]

Process Management (PS, TOP, KILL)

  • Commands: ps, top, kill
    • ps: Displays processes started by the current user.
    • top: Monitors system resources and running processes interactively.
    • kill [PID]: Kills a process by ID; common signals include -15 (graceful termination) and -9 (forceful).

Advanced Process Management (JOBS, BG, FG)

  • Commands: jobs, bg, fg
  • Purpose: Manages background and foreground processes.
  • Usage:
    • jobs: Lists background jobs.
    • bg [job ID]: Resumes job in the background.
    • fg [job ID]: Brings job to the foreground.

File Compression and Archiving (gzip, tar)

  • Commands:
    • gzip [file]: Compresses files with gzip.
    • gunzip [file]: Decompresses gzip files.
    • tar: Archives directories and files.
    • Options: -c (create), -x (extract), -z (gzip), -f (filename)
  • Example: tar -czf archive.tar.gz [directories/files]

Text Editor (nano)

  • Command: nano [file]
  • Purpose: Simple text editor.
  • Commands: Ctrl + X (exit), Ctrl + O (save), Ctrl + W (search).

Create Aliases

  • Command: alias name='command'
  • Purpose: Creates shortcuts for longer commands.
  • Permanent Aliases: Add to ~/.bashrc or ~/.zshrc

Environment Variables

  • Concept: Variables that affect the behavior of the shell.
  • Example: echo $PATH prints the executable search path.
  • Exporting: export VAR=VALUE sets a variable.

Input/Output Redirection

  • Symbols:
    • > : Redirects output to a file (overwrites).
    • >>: Redirects output to a file (appends).
    • < : Takes input from a file.
    • 2> : Redirects standard error.

Links (ln)

  • Commands:
    • Hard link: ln [target] [link_name]
    • Soft link: ln -s [target] [link_name]
  • Purpose: Creates links to files or directories (hard and symbolic).

User and Permissions Commands

Who

  • Command: who
  • Purpose: Displays logged-in users.

Switch User (SU)

  • Command: su [username]
  • Purpose: Switches to another user.
  • Option: - (simulates a full login).

Superuser Do (SUDO)

  • Command: sudo [command]
  • Purpose: Runs command with superuser (root) privileges.

Password

  • Command: passwd [username]
  • Purpose: Changes the user’s password.

Change Ownership (CHOWN)

  • Command: chown [owner][:group] [file]
  • Purpose: Changes file or directory ownership.
  • Option: -R (recursive).

Change Permissions (CHMOD)

  • Command: chmod [permissions] [file]
  • Purpose: Changes file or directory permissions.
  • Syntax: [ugoa][+-=][rwx]
  • Advanced: Octal notation (e.g., chmod 755).

Conclusion

  • Important Concepts: Understanding and combining simple commands is more critical than memorizing everything.
  • Practice: Continually practice and refer to man pages as needed.
  • Final Note: Thanks to Flavio Copez for resources, and keep exploring Linux commands to improve efficiency.