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

CommandProjectDescriptionExample
lsgnuList files and directoriesls /home
cdbashChange the current directorycd /home
pwdgnuPrint current working directorypwd
cpgnuCopy files or directoriescp file.txt copy.txt
mvgnuMove or rename files/directoriesmv file.txt moved.txt
rmgnuRemove files or directoriesrm file.txt
mkdirgnuCreate directoriesmkdir folder
rmdirgnuRemove empty directoriesrmdir folder
touchgnuCreate empty file or update timestamptouch file.txt
catgnuDisplay or concatenate file contentscat file.txt
echobashPrint text to stdoutecho "hello world"
basenamegnuStrip directory path from filenamebasename /path/file.txt
dirnamegnuStrip filename to show directory pathdirname /path/file.txt

πŸ”’ Privileges & Ownership

CommandProjectDescriptionExample
chmodgnuChange file permissionschmod +x script.sh
chowngnuChange file owner and groupchown user:group file.txt
sudootherExecute command as root usersudo rm file.txt
suutil-linuxSwitch to root usersu

πŸ” Search & Filters

CommandProjectDescriptionExample
findgnuSearch for files in directoriesfind /home -name "*.txt"
grepgnuSearch text in files using patternsgrep "error" log.txt
sedgnuFilters text using given rulessed 's/old/new/' file.txt
xargsgnuUse output as argumentscat dirs.txt | xargs rmdir

βš™οΈ Processes

CommandProjectDescriptionExample
psprocps-ngShow currently running processesps aux
killgnuTerminate a process by PIDkill 1234
pkillgnuTerminate a process by namepkill firefox

πŸ“¦ Archiving & Compression

CommandProjectDescriptionExample
targnuArchive or extract filestar -cvf archive.tar file.txt
gzipgnuCompress filesgzip archive.tar
gunzipgnuDecompress gzip filesgunzip archive.tar.gz

🌐 Networking

CommandProjectDescriptionExample
pingiputilsTest network connectivityping google.com
ipiproute2Show or configure network interfacesip addr
curlotherDownloads files from the webcurl -O http://example.com/file
wgetotherDownloads files from the webwget http://example.com/file

πŸ’Ύ Storage & Disks

CommandProjectDescriptionExample
mountutil-linuxMount disks or drivemount /dev/sda1 /mnt/usb
umountutil-linuxUnmount disks or driveumount /mnt/usb
fdiskutil-linuxManages disks or drivesfdisk -l

πŸ–₯️ System & Other

CommandProjectDescriptionExample
shutdownutil-linuxShut down the systemshutdown
rebootutil-linuxReboot the systemreboot
cleargnuClears the terminal outputclear
envgnuConfigure environment variablesenv
exportgnuConfigure temp environment variablesexport VAR=value
whichotherLocate a command in PATHwhich cat
whereisutil-linuxLocate binarieswhereis cat
typebashShow command type informationtype cat
unamegnuShow system and kernel informationuname -a
uptimeprocps-ngShow system uptime and loaduptime
whoutil-linuxShow who is logged inwho
whoamignuShow current userwhoami
idgnuShow user and group IDsid
groupsgnuList groups for a usergroups

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

ShebangCommentDebianArchFedoraMacOSFreeBSD
#!/bin/bashthe classic bash location on linuxβœ…βœ…βœ…βŒβŒ
#!/usr/bin/bashthe modern bash location on linuxβœ…βœ…βœ…βŒβŒ
#!/bin/env bashthe env binary is almost never in bin/❌❌❌❌❌
#!/usr/bin/env bashfinds bash via the pathβœ…βœ…βœ…βœ…βœ…

Posix Compatible

ShebangCommentDebianArchFedoraMacOSFreeBSD
#!/bin/shthe default sh location on most systemsβœ…βœ…βœ…βœ…βœ…
#!/usr/bin/shthe sh binary is almost never in usr/bin/❌❌❌❌❌
#!/bin/env shthe env binary is almost never in bin/❌❌❌❌❌
#!/usr/bin/env shfinds 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

comming soon…

Chaining

comming soon…

Loops

comming soon…