Parts of the command line
The linux command line is not one singular thing, it consists of many parts. Each having a specific important purpose. Each of these parts is modular and can be swapped out to fit your needs.
The Terminal:
The terminal (often called terminal emulator), is a front end interface that allows you to interact with the shell of the system.
At its core its just a window in which you input text which gets passed down to the shell and then displays the output of the shell.
The terminal emulator does not understand commands, it just passes input to the shell and renders output.
Common terminal emulators include: Konsole, Kitty, Gnome Terminal, and Ghostty.
The Shell:
The shell is a program that interprets commands and then runs the right programs.
It is like the bridge between the user and the kernel. its the most basic form of interacting with your system.
The most commonly used shell programs are: Bash, Zsh, Powershell.
The Binaries:
The shell itself doesnt execute anything, it just interprets a command and then executes the correct binary programs with the correct arguments.
Each command is just calling one or more of those binary programs.
So the commands like: cd, ls, cat.
Are basically just names of executable binaries.
All major Linux distro’s come with a standard set of these commands (binaries).
Most of these binaries are part one of the following projects: gnu-coreutils, util-linux, procps-ng, iproute2, iputils. and then there are a number of binaries that are their own projects.
But you can be sure that all major Linux distros come with the binaries of those 5 projects, so if you learn those you are set.
Commonly used commands
π Files & Directories
| Command | Project | Description | Example |
|---|---|---|---|
| ls | gnu | List files and directories | ls /home |
| cd | bash | Change the current directory | cd /home |
| pwd | gnu | Print current working directory | pwd |
| cp | gnu | Copy files or directories | cp file.txt copy.txt |
| mv | gnu | Move or rename files/directories | mv file.txt moved.txt |
| rm | gnu | Remove files or directories | rm file.txt |
| mkdir | gnu | Create directories | mkdir folder |
| rmdir | gnu | Remove empty directories | rmdir folder |
| touch | gnu | Create empty file or update timestamp | touch file.txt |
| cat | gnu | Display or concatenate file contents | cat file.txt |
| echo | bash | Print text to stdout | echo "hello world" |
| basename | gnu | Strip directory path from filename | basename /path/file.txt |
| dirname | gnu | Strip filename to show directory path | dirname /path/file.txt |
π Privileges & Ownership
| Command | Project | Description | Example |
|---|---|---|---|
| chmod | gnu | Change file permissions | chmod +x script.sh |
| chown | gnu | Change file owner and group | chown user:group file.txt |
| sudo | other | Execute command as root user | sudo rm file.txt |
| su | util-linux | Switch to root user | su |
π Search & Filters
| Command | Project | Description | Example |
|---|---|---|---|
| find | gnu | Search for files in directories | find /home -name "*.txt" |
| grep | gnu | Search text in files using patterns | grep "error" log.txt |
| sed | gnu | Filters text using given rules | sed 's/old/new/' file.txt |
| xargs | gnu | Use output as arguments | cat dirs.txt | xargs rmdir |
βοΈ Processes
| Command | Project | Description | Example |
|---|---|---|---|
| ps | procps-ng | Show currently running processes | ps aux |
| kill | gnu | Terminate a process by PID | kill 1234 |
| pkill | gnu | Terminate a process by name | pkill firefox |
π¦ Archiving & Compression
| Command | Project | Description | Example |
|---|---|---|---|
| tar | gnu | Archive or extract files | tar -cvf archive.tar file.txt |
| gzip | gnu | Compress files | gzip archive.tar |
| gunzip | gnu | Decompress gzip files | gunzip archive.tar.gz |
π Networking
| Command | Project | Description | Example |
|---|---|---|---|
| ping | iputils | Test network connectivity | ping google.com |
| ip | iproute2 | Show or configure network interfaces | ip addr |
| curl | other | Downloads files from the web | curl -O http://example.com/file |
| wget | other | Downloads files from the web | wget http://example.com/file |
πΎ Storage & Disks
| Command | Project | Description | Example |
|---|---|---|---|
| mount | util-linux | Mount disks or drive | mount /dev/sda1 /mnt/usb |
| umount | util-linux | Unmount disks or drive | umount /mnt/usb |
| fdisk | util-linux | Manages disks or drives | fdisk -l |
π₯οΈ System & Other
| Command | Project | Description | Example |
|---|---|---|---|
| shutdown | util-linux | Shut down the system | shutdown |
| reboot | util-linux | Reboot the system | reboot |
| clear | gnu | Clears the terminal output | clear |
| env | gnu | Configure environment variables | env |
| export | gnu | Configure temp environment variables | export VAR=value |
| which | other | Locate a command in PATH | which cat |
| whereis | util-linux | Locate binaries | whereis cat |
| type | bash | Show command type information | type cat |
| uname | gnu | Show system and kernel information | uname -a |
| uptime | procps-ng | Show system uptime and load | uptime |
| who | util-linux | Show who is logged in | who |
| whoami | gnu | Show current user | whoami |
| id | gnu | Show user and group IDs | id |
| groups | gnu | List groups for a user | groups |
Shebangs
You will often encounter shell scripts starting with #!/bin/bash, the #! is called a shebang. It is used to specify which interpreter will be used to run the script.
So if your script uses bash specific scripting syntax, then you put a bash shebang on the first line of your script.
If you want your script to be posix compatible so it can run on other unix like systems that don’t have bash, then you use the sh shebang.
The sh shell is compatible with alot more unix like operating systems than just linux, making your scripts more portable at the cost of bash specific scripting features.
Bash Compatible
| Shebang | Comment | Debian | Arch | Fedora | MacOS | FreeBSD |
|---|---|---|---|---|---|---|
| #!/bin/bash | the classic bash location on linux | β | β | β | β | β |
| #!/usr/bin/bash | the modern bash location on linux | β | β | β | β | β |
| #!/bin/env bash | the env binary is almost never in bin/ | β | β | β | β | β |
| #!/usr/bin/env bash | finds bash via the path | β | β | β | β | β |
Posix Compatible
| Shebang | Comment | Debian | Arch | Fedora | MacOS | FreeBSD |
|---|---|---|---|---|---|---|
| #!/bin/sh | the default sh location on most systems | β | β | β | β | β |
| #!/usr/bin/sh | the sh binary is almost never in usr/bin/ | β | β | β | β | β |
| #!/bin/env sh | the env binary is almost never in bin/ | β | β | β | β | β |
| #!/usr/bin/env sh | finds sh via the path | β | β | β | β | β |
Conclusion
Just use #!/bin/bash or #!/usr/bin/bash for linux bash scripts.
Just use #!/usr/bin/env bash for cross platform bash scripts.
Just use #!/bin/sh or #!/usr/bin/env sh for cross platform posix scripts.
File globbing
In many tools including many shells you can filter your file or directory selection with something called Globbing, and its a way of quickly filtering your file or directory selection selection. If you want to select a specific set of files or directories from the command line you can use this following specific syntax that is specifically designed for that purpose.
Basics
. | Current directory |
.. | Parent directory |
~ | Home directory |
* | Matches any file or directory |
** | Matches any file or directory recursively |
/ | Directory separator |
Examples
ls * # list files and dirs in the current directory
ls ** # list files and dirs recursively starting from this dir
ls *.txt # list text files in this dir
ls **/*.txt # list text files recursively starting from this dir
ls . # list files and dirs inside the current directory
ls .. # list files and dirs inside the parent directory
ls ~ # list files and dirs inside the home directory
ls / # list files and dirs inside the root directory
ls text* # list files and dirs starting with "text" in the name
ls *text # list files and dirs ending with "text" in the name
ls *text* # list files and dirs containing "text" anywhere in the name
ls file.c* # list files with extensions starting with "c" (c, cpp, cs)
ls file?.txt # list txt files named "file" + any character (file1.txt, file2.txt)
ls file??.txt # list txt files named "file" + any 2 characters (file12.txt, file13.txt)
ls file{1,2,3}.txt # list files: file1.txt, file2.txt, and file3.txt
ls {foo,bar,hello}.txt # list files: foo.txt, bar.txt, and hello.txt
ls file[123].txt # list files: file1.txt, file2.txt, and file3.txt
ls file[a-f].txt # list files with a single char in the range a-c (fileb.txt, filec.txt)
lsΒ file[!0-9].txt # list files where the character after "file" is NOT a digit (0-9)
Piping
Piping is a powerful feature in the Linux command line that allows you to connect the output of one command directly to the input of another command. This is done using the pipe | symbol. The pipe takes the standard output stdout of the command on the left and feeds it as standard input stdin to the command on the right.
Basic Examples
cat file.txt | sort # display file contents and sort the lines
ps aux | grep firefox # show running processes and filter for firefox
Advanced Examples
# find errors, count occurrences, sort by frequency
cat log.txt | grep "error" | sort | uniq -c | sort -nr
Shell Differences
In powershell the parentheses () evaluate the expression inside them first and pass the result as input to the outer command. Since powershell commands return objects (like arrays of lines), this works naturally and feels similar to function chaining in other languages. In bash parentheses do not pass values between commands, instead they create a subshell (a separate execution environment), so this syntax doesnβt work for chaining.
# in powershell these both work
cat file.txt | sort
sort (cat file.txt)
# in bash these do not both work
cat file.txt | sort
sort (cat file.txt)
Redirection
Redirection changes the default source or destination of a command’s input/output streams. While it often uses files, it can also redirect to devices (like /dev/null to discard output) or other file descriptors. Unlike piping, which connects commands directly, redirection controls where input comes from and where output goes.Redirection is crucial for controlling command output, logging errors, and automating scripts without user interaction. Redirection is crucial for controlling command output, logging errors, and automating scripts without user interaction.
Redirection Operators
> | redirect stdout to a file |
>> | append stdout to a file |
< | redirect stdin from a file |
2> | redirect stderr to a file |
2>> | append stderr to a file |
&> | redirect both stdout and stderr to a file |
>&2 | redirect stdout to stderr |
Number Codes for Streams
- 0 (stdin): standard input, usually from the keyboard
- 1 (stdout): standard output, usually to the terminal
- 2 (stderr): standard error, usually to the terminal for errors
Examples
echo "hello" > output.txt # overwrite file
echo "hello" >> output.txt # append to file
cat < input.txt # read from file via stdin
command 2> error.log # redirect stderr
command > /dev/null # discard stdout
command &> all_output.log # redirect stdout + stderr
Chaining
Command chaining allows you to run multiple commands in sequence, with control over whether subsequent commands execute based on the success or failure of previous commands. This is done using special operators. Chaining is useful for scripting and automating tasks where you need conditional execution based on command results.
Chaining Operators
; | run the next command regardless of the exit status of the previous command |
&& | run the next command only if the previous command succeeded (exit status 0) |
|| | run the next command only if the previous command failed (exit status non-zero) |
Examples
mkdir folder; cd folder # create directory and change to it (always)
mkdir folder && cd folder # create directory and change to it (only if mkdir succeeded)
rm file.txt || echo "file not found" # try to remove file, echo message if it failed
first && second || third # run second if first succeeds, otherwise run third