Terminal Guide

Austin Garcia

Introduction

Welcome to the CLI

If you have never used the command line interface (CLI) before, then now is the time to embark on a journey to a new realm where simplicity meets power. It’s the key to making computers do exactly what you want, how you want it, when you want it. It is a direct line of communication to your OS. As you learn each new command, you’ll find yourself with the ability to conjure tasks and vanquish errors.

Do not be daunted by the stark text and blinking cursor. They are your allies on this journey. This is the beginning of a journey to technological enlightenment!

Terminal Access Across Different Operating Systems

Accessing the Terminal can vary depending on the operating system in use. Here’s how to access it on the most commonly used systems:

macOS
  • Navigate to /Applications/Utilities/ and double-click on Terminal.
  • Alternatively, use Spotlight by pressing Command + Space, typing “Terminal,” and pressing Enter.
Linux
  • Use the keyboard shortcut Ctrl + Alt + T in most distributions.
  • For those using a graphical interface like GNOME, KDE, or XFCE, look for the Terminal in your applications menu.
Windows
  • For Windows 10 and later, access the Command Prompt or PowerShell via the Start menu or by typing “cmd” or “PowerShell” in the search bar. PowerShell follows the standard outlined in this guide more closely.
  • For advanced users, Windows Subsystem for Linux (WSL) can be installed to provide a rich Linux-like Terminal environment.

Once the Terminal window is open, you’re presented with a prompt, followed by a blinking cursor. The initial prompt often contains information about the current user, the host system, and the present working directory.

Warning

The Terminal is a powerful tool. Proceed with caution. Don’t copy and paste random commands without understanding them unless you want to mess up your entire system.

Terminal Essentials

Fundamental Commands

Knowing a few fundamental commands is essential for navigating and operating within the CLI environment effectively. The commands listed here are foundational for file management, navigation, and system interaction. Master these, and you’ll have taken your first step into a broader world of command-line proficiency.

pwd

Prints the current working directory path.

pwd
cd

Changes the current directory to the one specified. Without arguments, it changes to the home directory.

cd <directory> # Example: `cd /home/user/documents`
ls

Lists the contents of the current directory. Flags can be used to alter the output, such as -l for long listing or -a to show all files (including hidden ones).

ls [options] [directory] # Example: `ls -la`
mkdir

Creates a new directory with the specified name.

mkdir <directory> # Example: `mkdir new_folder`
rm

Removes files or directories. Use -r for recursive deletion (necessary for directories) and -f to force deletion without prompt.

rm [options] <file/directory> # Example: `rm -rf old_folder`
rmdir

Removes empty directories.

rmdir <directory> # Example: `rmdir empty_folder`
mv

Moves a file or directory, or renames it if the destination is within the same directory.

mv <source> <destination> # Example: `mv old_name.txt new_name.txt`
cp

Copies files or directories. Use -r to copy directories.

cp [options] <source> <destination> # Example: `cp -r source_folder destination_folder`
touch

Creates a new empty file or updates the timestamp on existing files.

touch <file> # Example: `touch new_file.txt

Viewing and Editing Files

When working with files, you often need to view their contents or edit them. This section covers the basic commands and programs for viewing and editing files directly from the terminal.

cat

Concatenates and displays file contents. Commonly used to quickly view small files.

cat <file> # Example: `cat example.txt`
more

Views the contents of a file one page at a time. Move forward in the file with the spacebar and quit with q.

more <file> # Example: `more example.txt`
less

An improved version of more. It allows backward movement in the file as well as forward movement.

less <file> # Example: `less example.txt`
nano

A simple, easy-to-use text editor that runs in the terminal. Use Ctrl + O to save and Ctrl + X to exit.

nano <file> # Example: `nano example.txt`
vi

A classic text editor with a modal interface. Requires learning different modes but is very powerful.

vi <file> # Example: `vi example.txt`

vim

An improved version of vi. Includes syntax highlighting, a comprehensive help system, and a wide range of plugins.

vim <file> # Example: `vim example.txt`

Essential CLI Keyboard Shortcuts

Navigating the command line efficiently requires familiarity with keyboard shortcuts. These shortcuts enhance productivity and streamline your workflow. Depending on the system you are using, Mac/Windows/Linux, the equivalent keys may vary between Crl and Super, or something completely different. Regardless, these options exist but may be bound to another set of keys. Below are fundamental keyboard shortcuts for command-line use:

  • Ctrl + A or Home: Move the cursor to the beginning of the line.
  • Ctrl + E or End: Move the cursor to the end of the line.
  • Ctrl + B or Left Arrow: Move the cursor back one character.
  • Ctrl + F or Right Arrow: Move the cursor forward one character.
  • Alt + B: Move the cursor back one word.
  • Alt + F: Move the cursor forward one word.

Editing

  • Ctrl + U: Cut the text from the cursor to the beginning of the line.
  • Ctrl + K: Cut the text from the cursor to the end of the line.
  • Ctrl + W: Cut the word before the cursor.
  • Alt + D: Cut the word before the cursor.
  • Ctrl + Y: Paste the last cut text.
  • Ctrl + _: Undo the last text modification.

History

  • Ctrl + P or Up Arrow: Go to the previous command in the history.
  • Ctrl + N or Down Arrow: Go to the next command in the history.
  • Ctrl + R: Reverse search command history.
  • Ctrl + G: Exit history searching mode without running a command.
  • Ctrl + S: Forward search command history (note that this may be disabled in some terminal configurations. Sometimes you can change this terminal setting with stty -ixon).

Control

  • Ctrl + C: Terminate the current process.
  • Ctrl + Z: Suspend the current process by sending it to the background.
  • Ctrl + D: Exit the current shell. When used at the prompt, it logs out of the session, similar to running the exit command.

Miscellaneous

  • Ctrl + L: Clear the screen (similar to running the clear command).
  • Ctrl + T: Swap the last two characters before the cursor.
  • Alt + T: Swap the last two words before the cursor.
  • Ctrl + X, followed by Ctrl + E: Open the current line in the default command-line editor (as set in the EDITOR bash variable) for a full-screen editing experience.
  • Alt + <number>, followed by key press: Press some key a given number times. For example: Alt+12+delete delete 12 characters

These shortcuts are based on the GNU Readline library, which is utilized in various command-line interfaces, including many shells. Specific functionality may vary depending on your shell configuration and terminal emulator.

Output

Messaging Commands

These commands allow for displaying and sending messages in the terminal.

echo

Displays a line of text/string that is passed as an argument.

echo <string> # Example: `echo "Hello, World!"`
printf

Similar to printf in C. Usually installed by default on most systems and allows to tweak the output a little finer.

printf <string> [$var] [$var] ... # Example: printf "%s has logged in %d times\n" $USER $TIMES
wall

Broadcasts a message to all users logged into the system.

wall <string> # Example: `wall "The server will restart in 10 minutes!"`

Advanced Command Execution

Output Redirection

Directing the flow of data to and from files or programs.

  • >: Redirects output to a file, overwriting previous content.
  • >>: Redirects output to a file, appending to any existing content.
  • |: Sends the output from one command to another command as input.
  • <: Redirects input from a file to a command.

Streamlining Text Processing

Tools

Powerful command-line utilities for text processing.

grep

Searches through text using patterns.

grep [options] <pattern> [file...] # Example: `grep "search_term" example.txt`
sed

Stream editor for filtering and transforming text.

sed [options] [script] [input_file...] # Example: `sed 's/old/new/' example.txt`
awk

A programming language for text processing and data extraction.

awk [options] [program] [file...] # Example: `awk '/search_pattern/ { action_to_take_on_matches; another_action; }' example.txt`

File Ownership and Permissions

Changing Ownership

Commands to change the owner and group of files and directories.

chown

Change the owner and/or group of each given file or directory.

chown <owner>[:<group>] <file> # Example: `chown alice:alice file.txt`

Modifying Permissions

Adjust the read, write, and execute permissions on files and directories.

chmod

Change the file mode bits for file access permissions.

chmod <mode> <file> # Example: `chmod 755 script.sh`
chgrp

Change the group ownership of files or directories.

chgrp <group> <file> # Example: `chgrp developers script.sh`

System Process Management

Monitoring

Keeping track of system processes and their states.

ps

Reports a snapshot of the current processes.

ps aux # Show all running processes
top

Displays Linux tasks and provides a dynamic real-time view of a running system.

top # Start the top program
htop

Interactive process viewer, considered an improved version of top.

htop # Interactive process viewer (may require separate installation)

Termination

Commands to stop running processes.

kill

Sends a signal to a process, typically to stop the process.

kill <PID> # Example: `kill 12345`
pkill

Allows sending signals to processes by pattern.

pkill <pattern> # Example: `pkill firefox`

System and Performance Insights

System Info

Commands to display user and system information.

who

Shows who is logged on to the system.

who # List logged-in users
w

Displays who is logged in and what they are doing.

w # Detailed listing of logged-in users
neofetch

(not installed by default)

A command-line system information tool that displays system information alongside an image, generally

your OS logo.

neofetch # Show system information with ASCII art of the OS logo (requires installation)

Information

If you don’t know what something does, you can use one of several documentation tools to check out what it does.

man

man (manual) gives you the user man-page for a command. A goto for nearly all commands.

man <command> # Example: `man find`
# Or for multiple page entries
man [page number] <command> # Example: `man 1 man`
info

info gives you the newer user manual for a command. Info is more comprehensive. It has basic hyperlinking and markup and vim navigation bindings. Some describe info-pages it as a guide instead of a manual. Not everything has an info page.

info <command> # Example: `info info`

You can run info on its own to get a list of all available commands with an info page.

info

help

Summary of the usage of a command.

<command> --help # Example: man --help
whatis

Display one-line manual page descriptions.

whatis <command> # Example: whatis whatis
type

Display the type of command the shell will execute.

type <command> # Example: type type
which

Locates the executable file associated with the given command.

which <command> # Example: `which ls`
tldr

(not installed by default)

This stands for “Too long: didn’t read”. This provides a super short version of the info, man, and help pages. This has to be installed separately on most systems.

tldr <command> # Example: tldr tldr
tldr tldr # Example tun of tldr on tldr.
tldr

  Display simple help pages for command-line tools from the tldr-pages project.
  More information: https://tldr.sh.

  - Print the tldr page for a specific command (hint: this is how you got here!):
    tldr command

  - Print the tldr page for a specific subcommand:
    tldr command-subcommand

  - Print the tldr page for a command for a specific [p]latform:
    tldr -p android|linux|osx|sunos|windows command

  - [u]pdate the local cache of tldr pages:
    tldr -u

cheat

(not installed by default)

Provides a cheat sheet for using the command

cheat <command> # Example: cheat cheat
cheat cheat # Example run of cheat on cheat.
# To see example usage of a program:
cheat <command>

# To edit a cheatsheet
cheat -e <command>

# To list available cheatsheets
cheat -l

# To search available cheatsheets
cheat -s <command>

# To get the current `cheat' version
cheat -v