The Linux Command Line

The Linux Command Line is a text interface to your computer. Often referred to as the shell, terminal, or console, this text interface allows the user to interact with their system, navigate files and folders, create, and delete them.

History of the Command Line

During the early years of computing, one of the earliest operating systems was called Unix. Unix was designed to run as a multi-user system on mainframe computers. Users connected to Unix remotely via terminals, which are just a keyboard and screen. They would send keystrokes to the server and the data they received would be displayed on the screen. Everything was text-based - both input and output. Text is very light on resources, making it fast and efficient. The commands were kept very terse to reduce the number of keystrokes needed. When logged into a Unix mainframe via a terminal, users might have needed to create files, rename them, move them into subdirectories - everything was done with text. Each of these commands required its own program. Users would connect to the "shell" program, a singular master program that coordinated the execution of these other programs and commands. By wrapping them together, this also allowed data to pass from one program to another. Users could write simple code (called "shell scripts") which could automate long series of shell commands in order to make complex tasks easier. The original Unix shell program was just called "sh", but it has been extended and superceded over the years - a modern Linux system most likely uses a shell called "bash" or "zsh." Linux is a sort-of-descendent of Unix, with the core parts behaving the same. Nowadays, software terminals offer the same old Unix-style text interface for users to interact with their system.

Data Streams

Data streams are for reading and writing bytes. Readers and Writers are for reading strings/chars in a specific encoding like UTF-8.

There are three pre-connected data streams that automatically open wen a computer program or command line instruction begins running. They serve as standardized communication channels between a program and its environment (usually the terminal or shell). They are:

  • stdin - Standard Input - Accepts data or text input sent to the program. Usually sent from a keyboard or file.
  • stdout - Standard Output - Sends the normal, successful text output from the program. Usually outputted on the terminal screen.
  • stderr - Standard Error - Sends error logs, warnings, and diagnostic text from the program. Usually outputted on the terminal screen.

Standard Input 0

It is the stream a program listens to when it needs data from the user or another process.

For example, when a terminal script asks you to type in your name, the characters you type in via your keyboard pass through stdin to the program.

You can force a program to read data from a file instead of the keyboard using the < operator.

cat < textfile.txt

Standard Output 1

When a program runs successfully, it writes its primary data results to this stream.

Running ls lists your files onto your screen because stdout defaults to the display.

You can capture this successful data and save it directly to a file using the > operator.

ls > output.txt

Standard Error 2

This is an independent output stream specifically reserved for diagnostics and error messages.

By keeping stderr separate from stdout, you can safely pass a program's clean, successful data into another file or script without polluting it with raw error messages. Even if you redirect your normal output to a file, the error messages will still flash on your screen so you can see them.

Because its file descriptor is 2, you can isolate and redirect only the errors ot a dedicated log file using 2>.

cat nonexistent_file.txt 2> error_log.txt

Shell Commands

A shell command is a command meant to be used in a Unix-based terminal. These commands are essential for creating new files and folders, navigating the file tree, and executing files.

  • man - Manual for a command
  • man ls - Manual for ls
  • man uniq - Manual for uniq

Navigation Commands

Relative commands change to a different directory relative to your working directory.

Absolute commands change to a different directory no matter what your working directory is. Any path that starts with the root folder - "/" - is an absolute path.

Relative Directory Locations

  • ./directory - Represents the child directory
  • . - Represents the current working directory
  • .. - Represents the parent directory

Changing Directories

  • whoami - Prints out your username
  • pwd - Print working directory. Where am I?
  • cd - Change directory
    • cd / - Go to the root directory
    • cd ~ (or just cd) - Go to the home directory
    • cd /Users/grantdavis303/Desktop - From the root directory, move down the tree multiple directories (absolute)
    • cd /home - From the root directory, move down the tree one directory (absolute)
    • cd .. - Relatively, move up the tree one directory (relative)
    • cd ../.. - Relatively, move up the tree multiple directories (relative)

Creating Files and Folders

In order for certain commands to work, they require parameters and arguments.

Options, also known as a "switch", are used to modify the way a command operates. They are either a single character preceded by a hyphen: -p, or as longer words preceded by two hyphens: --verbose.

Creating Directories

  • mkdir - Make directory
    • mkdir cli_fun - Make a new directory called "cli_fun"
    • mkdir dir1 dir2 dir3 - Make three new directories called "dir1", "dir2", and "dir3"
    • mkdir -p dir4/dir5/dir6 - Make three new nested directories called "dir4", "dir5", and "dir6"

Creating Directories with Spaces

To work with spaces, you need to "escape" them. Escaping is a computer term that refers to using special codes to tell the computer to treat particular characters differently than normal.

mkdir "dir7"
mkdir 'dir8'
mkdir dir\ 9
mkdir "dir10" "dir11"
mkdir -p "dir12"/"dir13"

Creating Files

Hidden files are commonly used to store settings and configuration data (like .config).

  • echo - Prints the command line arguments back out again
    • echo "This is a test" - Prints "This is a test" to the console
    • echo "This is a test" > cli_output.txt - Prints "This is a test" to a text file called "cli_output.txt" (this will overwrite the text file if used multiple times)
    • echo "This is appended text" >> cli_output.txt - Appends "This is appended text" to the bottom of a text file called "cli_output.txt" (this will not overwrite the text file if used multiple times)
    • echo "This is text in a hidden file" >> .hidden.txt - Writes to a text file called ".hidden.txt"

Writing to a File

Standard Input (stdin) refers to the channel which a program receives its input. Standard Output (stdout) refers to the channel which a program sends its output.

ls -al ~/Desktop > stdout.txt - List all files and folders on the desktop and send the output to a file called "stdout.txt"

Reading Files and Folders

Reading a Folder's Contents

  • ls - List a directory's contents
    • ls -a - List the current directory's contents (including hidden files)
    • ls -l - List the current directory's contents (in a long list format)
    • ls -al - List the current directory's contents (including hidden files and in a long list format)

Reading a File's Contents

  • cat - Comes from 'con-cat-enate' and means 'to link together'
    • cat cli_output.txt - Read a single file's contents
    • cat cli_output.txt stdout.txt - Read multiple file's contents
    • cat cli_outpu?.txt - Shortcut where the ? is used to indicate "any single character"
    • cat *.txt - Shortcut where the * is used to indicate "zero or more characters"
  • less - Use to be called 'more', as it put a line at the bottom that said 'more, indicating that you haven't read anything yet. Press q to quit and return to the command line
    • less cli_output.txt - Paginate the output of a file (especially if the output is long)
  • wc - Get the line, word, and byte count for a file
    • wc cli_output.txt - Get the line, word, and byte count for a file
    • wc -l cli_output.txt - Get just the line count for a file
    • wc -w cli_output.txt - Get just the word count for a file
    • wc -c cli_output.txt - Get just the byte count for a file

Moving and Copying Files

  • mv - Move a folder or file into another folder
    • mv cli_output.txt dir1 - Move "cli_output.txt" into the dir1 directory
    • mv cli_output.txt .. - Move combined.txt into the parent directory
    • mv dir1/* . - Move everything in the dir1 directory to the current working directory
    • mv dir1 cli_output.txt stdout.txt dir2 - Move dir1, cli_output.txt, and stdout.txt into the dir2 directory
    • mv dir2/cli_output.txt dir4/dir5/dir6 - Move cli_output.txt into the nested dir6 directory
    • mv hello.txt updated.txt - Rename "hello.txt" to "updated.txt"
    • mv dir4/dir5/dir6/cli_output.txt renamed_cli_output.txt - Move "cli_output.txt" to the current working directory and rename it "renamed_cli_output.txt"
    • mv dir2 directory_two - Rename the dir2 directory to "directory_two"
  • cp - Copy a folder or file into another directory
    • cp greeting.txt greeting_copy.txt - Create a file copy in the current working directory
    • cp dir2/combined.txt dir1/new_combined.txt - Create a file copy into a specific directory
    • cp -r dir3 new_dir3 - Create a directory copy in the current working directory
    • cp -r dir4 new_dir3 - Create a directory copy into a specific directory
    • cp -R source/. destination - Copy everything inside one directory into another directory

Deleting Files and Folders

  • rm - Remove a folder or file
    • rm updated.txt - Remove a file
    • rm greeting-copy.txt greeting.txt - Remove multiple files
    • rmdir dir4 - Remove an empty directory
    • rmdir e1 e2 e3 - Remove multiple empty directories
    • rmdir -p dir4 - Remove an empty directory and the parent directory (must be empty)
    • rmdir -p dir8/dir9/dir10 - Remove an empty directory and all parent directories (must be empty)
    • rm -r dir4 - Remove all the folders and files within the directory chosen (be careful!)

Piping

Piping refers to the act of taking the output from one command and feeding it directly in as the input to another command, eliminating the need of having to create and delete a temporary file.

  • | - The pipe character
    • ls ~/Desktop | wc -l - List everything on the Desktop and tell me how many lines it outputs
    • ls ~/Desktop | less - List everything on the Desktop
    • sort new.txt | less - Sort a text file
    • cat new.txt | uniq | less - Return only the unique lines in a text file
    • sort new.txt | uniq | wc -l - Return the total amount of unique lines in a text file

The Administrator and Superuser

The machine's administrator is known as the "superuser" in Unix parlance. This super user is a user with super powers, as the name suggests.

  • su - The "superuser" or "switch user", and allows the user to change to another user on the machine without having to log in and out again
  • sudo - A command for "superuser" or "switch user" and "do this command." A user is prompted by their own password, which is cached for 15 minutes. Crucial for system administration
    • sudo apt - Advanced Package Tool is fundamental for managing software packages (installing, updating, removing)
    • sudo apt-get - More stable and script friendly command without pretty output
    • sudo apt install tree - Install the tree library on a Ubuntu operating system

Changing File Permissions

Permissions:

  • Read (r) - The Read permission for a file means that you can view the contents of a file. The read permission for a directory means you can view the contents of a directory.
  • Write (w) - The Write permission for a file means that you can modify the contents of a file (either add new content or delete existing content). The write permission for a directory means you can create or delete files and sub-directories.
  • Execute (x) - The Execute permission for a file means that you can run/execute a file. This permission makes sense only to files which can be executed (shell scripts, C language object files etc). The execute permission for a directory means that you can traverse through the directory.
  • chmod - Change permissions
chmod 444 myfile.txt

chmod +x shell_script.sh

Downloading or Uploading Data

Client URL (CURL) is a versatile command-line tool for transferring data to or from a server using various network protocols. It is widely used by developers for tasks such as testing APIs, downloading files, and interacting with web services directly from the terminal without a web browser.

curl can be used to send data (POST, PUT, and DELETE) as well as receive data (GET, HEAD).

curl supports many different types of protocols (FTP, SFTP, HTTP, HTTPS)

  • curl - Client URL
curl -I http://ec2-18-212-98-151.compute-1.amazonaws.com/

curl -O http://example.com/image.jpg

Homebrew

Homebrew is the de-facto package manager for macOS. With it, you can install, update, and manage software right from the command line.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • brew -
    • brew update -
    • brew upgrade -
    • brew install tree - A command to install the tree library on a Ubuntu operating system
    • brew uninstall tree - A command to install the tree library on a Ubuntu operating system

Processes

A process is a running program in a computer.

A process ID (PID) is the unique number assigned to identify the process.

# Commands

ps                  # list running processes
ps 18512            # list a specific running process
ps -p 18512         # verify that the PID is empty/free

kill 18512          # kill a single process
kill -9 18512       # forcefully kill a single process
pkill "server.js"   # kill w/ name, pattern, or attributes

lsof -nP -iTCP:3000 -sTCP:LISTEN    # make sure nothing is listening on a specific port

Other Linux Commands

nano

  • nano - Brings up the nano command line text editor
    • nano file.txt - Opens file.txt in the nano command line text editor

grep

  • grep - Global Regular Expression Print (GREP) searches for specific text patterns within files or text streams
    • grep "vitest" package.json - Searches file.txt for the string "hello"

cron

Command Run On Notice (CRON) is a time-based job scheduler. A scheduled job is known as a “cron job.” It runs as a background service (daemon) and enables users to automate repetitive tasks at specific times, date, or intervals. Although typically used to automate system maintenance and administration it can be used to automate any task.

  • crontab -
    • crontab -e - Open the crontab editor
    • crontab -l - Lists all active jobs for the user
    • crontab -r - Removes all jobs for the user
* * * * * command_to_run
│ │ │ │ │
│ │ │ │ └─ Day of week (0–7, Sunday = 0 or 7)
│ │ │ └─── Month (1–12)
│ │ └───── Day of month (1–31)
│ └─────── Hour (0–23)
└───────── Minute (0–59)

Octal Dump

The numbers on the left are the byte offset from the start of the file (16 bytes from 0000000 to 0000020).

  • 16 bytes = 020 (Octal -Ao)
  • 16 bytes = 16 (Decimal -Ad)
  • 16 bytes = 0x10 (Hexadecimal -Ax)
  • od - Display file contents in various human-readable formats, such as octal, hexadecimal, decimal, and ASCII.
    • od -o hello.txt - Displays the contents in octal
    • od -d hello.txt - Displays the contents in decimal
    • od -x hello.txt - Displays the contents in hexadecimal
    • od -c hello.txt - Displays the contents as individual ASCII characters, spaces, and newlines
    • od -Ao hello.txt - Displays the byte offset in octal
    • od -Ad hello.txt - Displays the byte offset in decimal
    • od -Ax hello.txt - Displays the byte offset in hexadecimal
    • od -An hello.txt - Displays no byte offset information
    • od -c -Ad hello.txt - Displays the characters with decimal byte offset
    • od -t x1 intro.txt - Displays the individual bytes (1 byte) as characters
    • od -c x1 intro.txt - Displays the individual bytes (1 byte) as hexadecimal
    • od -t u1 intro.txt - Displays the individual bytes (1 byte) as unsigned decimal
    • od -t d1 intro.txt - Displays the individual bytes (1 byte) as signed decimal
    • od -t o1 intro.txt - Displays the individual bytes (1 byte) as octal

tree

  • tree - Displays the full structure of the directory and sub-directories.
    • tree -L 1 - Displays the first layer the directory.
    • tree -L 2 - Displays the first and second layer the directory.
logout touch read open date sudo sudo nano systemctl cron wget pip npm make reset .bash_profile .bashrc