Introduction to Linux: Core Commands
Philipp Fruck & Pius Walter Core Command ██ Goal of this lecture • Learn the basic Linux command-line tools • Understand what they do and when to use them • Practice with useful flags ██ Why? • Mastering the CLI -> faster, more precise, automatable workflows • Essential for sysadmin, security, development work (and the upcoming semesters) Introduction to Linux: Core Commands 2 / 13
The Linux PATH PATH is a special environment variable (we will learn more about them later)
• It contains a list of directories separated by colons (:)
• When you execute a command, the SHELL searches these directories in order until it finds a matching executable
◦ The first binary that matches is executed ◦ Directories coming first have higher precedence Example PATH:
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
Introduction to Linux: Core Commands 3 / 13
Executing a command Running a command can be as simple as executing mycmd
• If mycmd is found in the PATH (e.g. /bin/mycmd) it is executed
• Each command can be invoked with positional arguments, e.g. mycmd arg1 arg2
• There are also additional flags that are not positional (mostly) ◦ There can be short and long variants of the same flag ▪ mycmd -h, mycmd --help
◦ Typically, flags -a, -b can be combined to -ab
• Of course, there are exceptions... Introduction to Linux: Core Commands 4 / 13
Example in C Let's build our own executable and run it as /tmp/mycmd arg1 arg2
#include <stdio.h>
int main(int argc, char *argv[]) {
for (int i = 0; i < argc; i++) {
printf("%d: %s\n", i, argv[i]);
}
}
——————————————————— [finished] ———————————————————
0: /tmp/mycmd 1: arg1 2: arg2 For flags, use libraries like getopt (or something more modern)
Introduction to Linux: Core Commands 5 / 13
Working with paths Don't confuse absolute and relative paths! • /bin: Is the global bin folder
• bin: The bin folder relative your current directory (e.g. /home/user)
Paths are case-sensitive • foobar and FooBar are different files that can co-exist
~ is a shortcut for your home directory
• ~/Downloads instead of /home/user/Downloads
. refers to your current directory
• ./foobar == foobar == ././foobar
.. refers to your parent directory
• From /home/user: .. == /home, ../.. == /
Files starting with . are hidden by default
• e.g. config files: ~/.config
• Don't confuse ./config with .config or ~/.config
Introduction to Linux: Core Commands 6 / 13
Getting started Command │ Description │ Useful Flags / Examples ────────┼────────────────────────────────────┼────────────────────────────────────────────── pwd │ Print current working directory │
ls │ List directory contents │ -l long format, -a show all files,
│ │ -sh size (human-readable) of files
cd │ Change directory │ cd - go back to previous directory
│ │ cd without parameters goes back to home
tree │ Show directory structure │ -L 2 limit depth, -a show all files
echo │ Print given arguments or variables │ echo $PATH, echo -n text without newline
clear │ Clear the current terminal screen │ Similar to Ctrl+l which scrolls to new screen
which │ Show full path of command │ -a shows all matches
▍ ranger, lf, mc, yazi are terminal file managers
Introduction to Linux: Core Commands 7 / 13
File Manipulation Command │ Description │ Useful Flags / Examples ────────┼──────────────────────────────────────┼─────────────────────────────────────── cp │ Copy files/directories │ -r recursive,
│ │ -i interactive (confirm overwrite)
mv │ Move/rename files │ -i interactive (confirm overwrite)
rm │ Remove files │ -r recursive, -i interactive, -f force
rmdir │ Remove empty directories │ -p remove empty parent dirs
mkdir │ Create (make) directory │ -p create parent dirs if missing
touch │ Create empty file / update timestamp │ -d specify a custom date
shred │ Override a file to hide its contents │ -u removes the file after shredding
▍ rm does not use a trash can. If you remove something, it's gone
▍ ⚠️ Danger Zone: rm -rf / will erase everything - don't run this!
Introduction to Linux: Core Commands 8 / 13
Viewing Files Command │ Description │ Useful Flags / Examples ────────┼────────────────────────────────┼──────────────────────────────────────────── cat │ Show file contents │ cat file.txt
tac │ Reverse cat │ tac file.txt
less │ View file page by page │ /text search forward, ?text search backward
more │ More primitive pager than less │
head │ Show first lines of file │ -n 20 show first 20 lines
tail │ Show last lines of file │ -f follow file (useful for logs)
wc │ Count lines, words, or bytes │ -l lines, -w words, -c characters
Instead of cd foo, cd bar, cat linux.txt simply cat foo/bar/linux.txt
Introduction to Linux: Core Commands 9 / 13
Editing Files ██ Basics • When working on the CLI, you also need a text-based editor • Today, nano and vim are the most common editors
• nano is a bit simpler and shows the required keyboard shortcuts
◦ ^ is the Ctrl key
• vim is more advanced and allows faster editing, but is more complex!
◦ Command driven editor with different modes ◦ i for insert mode, v for visual mode
◦ : for command mode, Esc to go back to regular mode
◦ From insert mode: Esc, :wq to write and quit
◦ From regular mode: Use / to search (like in less)
Introduction to Linux: Core Commands 10 / 13
Editing Files ██ Advanced • Nowadays, there are more modern editors that support the Language Server Protocol (LSP) ◦ If you want a really advanced text editor, have a look at helix or neovim
Introduction to Linux: Core Commands 11 / 13
Improve Your Workflow Command │ Description │ Useful Flags / Examples ────────┼─────────────────────────────────────┼──────────────────────── history │ Show command history │ !42 rerun command #42
!! │ Run the same command again │
key: ↑ │ Select last commands │
key: ⇥ │ Autocomplete command/directory/file │ note: case-sensitive
key: ^r │ Reverse Search │
▍ ^r is the terminal notation for Ctrl+r
In bash your history is not shared across sessions.
Your history is persisted in ~/.bash_history.
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— HISTSIZE: maximum number of commands kept in the current shell session's history.
HISTFILESIZE: maximum number of lines stored in ~/.bash_history.
Introduction to Linux: Core Commands 12 / 13
Thank you! Thank you for your attention! Don't forget the feedback in Moodle please! Introduction to Linux: Core Commands 13 / 13