Skip to content

Linux Command Line

131 companion flashcards · AI-assisted study content · Open the deck →

deck introduces the fundamentals of the Linux command line, a powerful text-based way to interact with your computer. You'll learn what a shell and terminal are, how to open one, and how to navigate your file system using core commands like pwd, ls, and cd. The cards also cover everyday file management tasks such as creating, copying, moving, and removing files and directories, along with simple ways to peek inside files using tools like cat, less, and head.

It's a great starting point if you're completely new to Linux, transitioning from a graphical interface, or preparing for coursework, certifications, or entry-level roles in IT, development, or data work. Even a little command-line confidence can make you noticeably faster and more comfortable on any Unix-like system, from a Raspberry Pi to a cloud server.

To get the most out of these cards, try to follow along on a real Linux machine or a virtual terminal as you study, since hands-on practice cements commands far better than reading alone. Review a small batch of cards each day rather than cramming, and challenge yourself to complete one tiny task at the command line between sessions, like making a folder or inspecting a file. Repetition over time, combined with real use, is what turns memorized commands into genuine fluency.

Introduction to the Linux Command Line

The Linux command line interface (CLI) is a text-based environment for interacting with the operating system by typing commands into a terminal emulator. Unlike a graphical user interface, which relies on windows, icons, and pointer-based actions, the CLI lets users accomplish tasks by issuing precise textual instructions, often faster and more reproducibly than their GUI equivalents.

Behind the CLI sits a shell — a program that reads each command, interprets it, and asks the Linux kernel to perform the corresponding action. The most common shell on Linux distributions is Bash (the Bourne Again SHell), which provides features like command history, tab completion, and scripting. Other shells such as Zsh or Fish exist, but Bash remains the default for most systems and is the focus of this textbook.

To begin using the CLI, you launch a terminal, which opens a shell session. On Ubuntu and many other distributions, pressing Ctrl+Alt+T opens a default terminal immediately. Alternatively, you can search for "Terminal" in the applications menu. Once the terminal is open, you are presented with a prompt where you can begin typing commands.

Navigating and Managing the Filesystem

Linux organizes files in a single hierarchical tree rooted at /. To understand where you are at any moment, the pwd (Print Working Directory) command shows the absolute path of your current location. To inspect the contents of that directory, ls lists files and folders. Adding flags refines the output: ls -l shows a detailed view including permissions and sizes, ls -a reveals hidden files (those whose names begin with a dot), and ls -h produces human-readable sizes.

Movement between directories is handled by cd (Change Directory). You can supply an absolute path such as cd /var/log to jump directly to a known location, or use relative paths like cd .. to move up one level and cd ~ to return to your home directory. The home directory, located at /home/username, is each user's personal workspace and the default landing spot when a new shell opens.

Creating and organizing directories and files is fundamental daily work. mkdir directory_name makes a new directory, and mkdir -p /path/to/nested/dir creates any missing parent directories along the way. The touch filename command creates an empty file or updates the timestamp of an existing one, which is handy for placeholder files. For moving data around, cp source destination copies files (add -r for directories and -i for an interactive confirmation), while mv source destination either moves or renames items depending on whether the destination is a new path or a new name in the same directory. The rm command deletes files; rm -r removes directories recursively, and -f forces the action without prompting, so it should be used with care because deletions are permanent. You can also create pointers to existing files with ln: ln -s target linkname produces a symbolic link (a small reference that points to the original), while a plain ln target linkname creates a hard link that shares the same underlying data.

Inspecting and Editing Files

Inspecting the contents of files is a frequent task. cat filename concatenates and prints the whole file to the screen, which is fine for short files but unwieldy for large ones. Piping through less, as in cat file | less, allows page-by-page navigation using Space to advance, b to go back, and q to quit. For quick checks, head filename shows the first ten lines and tail filename shows the last ten; the -n flag adjusts that count, and tail -f follows a file in real time, making it ideal for watching logs grow.

When you need to create or modify file contents directly from the terminal, two editors dominate. nano is the friendlier option: open a file with nano file, save with Ctrl+O, and exit with Ctrl+X. Vim is more powerful but has a steeper learning curve. It operates in modes — Normal mode for navigation and commands, Insert mode (entered with i) for typing, and Visual mode (v) for selecting text. From Normal mode, :w saves, :q quits, and :wq saves and quits together.

Whenever you are unsure how a command behaves, Linux offers built-in documentation. command --help prints a short usage summary, while man command opens the full manual page, navigated with the same keys as less and exited with q. These references are invaluable for discovering flags and behaviors without leaving the terminal.

Permissions, Searching, and Data Flow

Every file and directory in Linux carries a permission set that controls who can read, write, or execute it. These permissions appear in the output of ls -l as a string like rwxr-xr-x, broken into three triplets for the owner, the group, and everyone else. The chmod command changes them, either using octal numbers such as chmod 755 file (giving the owner full access and others read/execute), or symbolic forms like chmod u+x file to add execute permission for the user. Ownership itself is changed with chown user:group file, which requires root privileges when applied to files you do not own.

Searching through files and the filesystem is another everyday need. grep pattern file prints lines that match a pattern; -i makes the search case-insensitive and -r walks through directories recursively. To locate files by name, find /path -name "*pattern*" searches in real time, with -type f narrowing results to files and -type d to directories. For a faster alternative, locate filename queries a pre-built index updated by the updatedb command, returning matches almost instantly at the cost of slight staleness.

One of the shell's most powerful features is the ability to chain commands together and redirect their data. Output redirection with > writes a command's output to a file, overwriting any existing content, while >> appends to the file instead — for example, ls > list.txt saves a directory listing. Input redirection with < feeds a file into a command, as in sort < names.txt. Pipes (|) go further by sending the output of one command directly into another, enabling compositions like ls | grep txt to show only files whose names contain "txt". These primitives let you build complex data-processing pipelines from simple tools.

Processes, Resources, and Shell Customization

Linux exposes the running programs on your system through processes. ps aux provides a snapshot of every process with details such as the owning user, CPU usage, and memory consumption. For a continuously updating, interactive view, top (or its more colorful cousin htop) is the tool of choice. To stop a misbehaving process, note its PID from ps or top and run kill PID for a polite termination request (SIGTERM), or kill -9 PID to force the issue with SIGKILL when the process refuses to stop.

Long-running tasks do not need to monopolize your terminal. Appending an ampersand to a command — for example, sleep 10 & — runs it in the background and frees the prompt for other work. The jobs command lists your current background jobs, while fg brings one back to the foreground and bg resumes a suspended job in the background. You can also revisit your earlier work with history, which lists previously entered commands; rerun any of them with !n (where n is the number shown) or !! to repeat the most recent one.

Aliases let you define custom shortcuts, such as alias ll='ls -l', and adding them to ~/.bashrc makes them persistent; after editing that file, run source ~/.bashrc to apply the changes immediately. To monitor system health, several utilities are available: free -h displays memory usage, uptime reports load averages, and vmstat provides a broader view of memory, CPU, and I/O activity. For disk space, df -h summarizes free space across mounted filesystems in human-readable form, while du -sh directory reports the size of a specific directory and its contents. The env command lists environment variables, and export VAR=value sets one for your shell and any processes it spawns — useful for configuring tools like editors or API keys.

Networking and Package Management

Linux distributions ship with package managers that handle installing, updating, and removing software. On Debian-based systems such as Ubuntu, apt update refreshes the local index of available packages, and apt upgrade applies updates to what is already installed. New software is installed with sudo apt install package, while apt remove uninstalls it and apt autoremove cleans up orphaned dependencies. On RPM-based systems like Fedora, the equivalent tools are dnf (and the older yum); dnf install package adds software and dnf update brings the system up to date.

For network diagnostics and remote access, the CLI provides a rich toolkit. ping host sends ICMP echo requests to verify connectivity, with -c 4 limiting the test to four packets. To log into another machine, ssh user@host opens a secure encrypted shell session; the -p flag selects a non-standard port. For ad-hoc network configuration inspection, ifconfig (legacy) and ip addr (modern) display interface names, IP addresses, and link status.

Downloading files and interacting with web services from the terminal is straightforward. wget URL retrieves a file directly, with -r enabling recursive downloads and -O specifying a custom output filename. curl URL is similar but more flexible, often used to call APIs with flags like -X POST to send data and -O to save the response to a file. When troubleshooting, the which command utility is handy for finding the full path of any executable in your PATH, helping you understand which version of a tool the shell will actually invoke.

Archiving, Compression, and Automation

Combining many files into a single archive and then compressing them is a common task. tar is the traditional Unix archiver: tar -cvf archive.tar dir creates an archive (the c is "create", v is "verbose", f names the file), and tar -xvf archive.tar extracts it. Adding -z runs the archive through gzip on the fly, producing a .tar.gz file. For standalone compression, gzip file produces a .gz file and gunzip file.gz reverses the operation. ZIP archives, common when interoperating with Windows, are handled by unzip file.zip to extract and zip archive.zip files to create.

Automation is where the command line truly shines. The crontab system runs scheduled jobs in the background. crontab -e opens your personal cron table for editing, where each line follows the format * * * * * command to specify minute, hour, day of month, month, and day of week. Cron then runs the command at the matching times, making it perfect for routine backups, log rotations, and report generation.

For more elaborate automation, you can write Bash scripts. A script begins with a shebang line such as #!/bin/bash so the kernel knows which interpreter to use. After writing your commands in a file, make it executable with chmod +x script.sh and run it with ./script.sh. Inside scripts you can use variables, loops, and conditional statements, allowing you to package complex sequences of commands into reusable, parameterized tools. Together, archives, compression, scheduling, and scripting turn the Linux CLI from an interactive prompt into a powerful platform for reliable, repeatable system administration.

Frequently asked questions

What is the Linux command line interface (CLI)?

The Linux CLI is a text-based interface for interacting with the operating system using commands entered via a terminal emulator, allowing users to perform tasks efficiently without a graphical user interface.

What is <code>less</code> used for?

less filename views files page-by-page with navigation keys (Space forward, b backward, q quit), more efficient than cat for large files.

What is <code>locate</code>?

locate filename quickly finds files by name using a pre-built database (update with updatedb).

What is <code>yum</code> or <code>dnf</code> on RPM systems?

yum install package or dnf install package (Fedora successor); yum update for system updates.

What does mkdir -p do when creating directories?

The -p flag creates parent directories as needed and does not error if the directory already exists. For example: mkdir -p /opt/app/logs builds /opt, /opt/app, and /opt/app/logs in one command even if some exist.

How do you add a user to an existing group without removing other memberships?

Use 'usermod -aG groupname username'. The -a (append) flag is critical; without it, usermod replaces the user's group list. Verify with 'groups username' or 'id username' afterward.

How do you perform a DNS lookup for a domain name using dig?

Use 'dig example.com'. By default it shows the A record. Add a record type like 'dig example.com MX' for mail servers, or 'dig example.com ANY' for all records. Use +short for concise output.

How do you copy a file while preserving its permissions, timestamps, and ownership?

Use `cp -p source destination`. The `-p` (preserve) flag retains mode, ownership, and timestamps. Without it, the copy gets default permissions and the current time as its modification time.

What does the `top` command display and how do you exit it?

`top` shows a real-time, dynamic view of running processes sorted by CPU usage by default. It refreshes every few seconds. Press `q` to quit, `k` to kill a process, `M` to sort by memory.

What is the shebang line and why is it needed?

The shebang `#!` is the first line of a script, e.g., `#!/bin/bash`. It tells the kernel which interpreter to use when the script is executed directly. Without it, the default shell runs the script.

Drill this topic

131 flashcards on Linux Command Line — free, no signup needed to start.

Study Linux Command Line flashcards

LearnWiki pages are generated with AI assistance from LearnCoachAssist's reviewed study catalog and may contain errors — verify anything critical against your course materials.