Linux Basics#
Resources#
Guides#
- The Art of the Command Line: one-sheet condensed linux study guide
- Ubuntu Community Documentation: useful beginner series
- Ultimate Linux Guide for Windows users
- Learning the Shell
- DigitalOcean Linux Basics Tutorials: beginner oriented tutorials
- Introduction to Linux Basics
- Linux Command Line Primer
- How To Use
ps
,kill
,nice
to Manage Linux Processes
References#
- commandlinefu: command-line gems/one liners
- archlinux wiki: unusually comprehensive with high signal/noise ratio
- Introduction to Linux Technology and Philosophy by Jeremy Hajek: (github source)
- The Linux Command Line by William Shotts
- Bite Size Linux by Julia Evans
Help/Manuals#
- ManKier explain: concise pretty man pages with an API
- explainshell: match command-line arguments to their help text
- tldr pages: more concise man pages
- manned: man page versions across a range of distributions
- ubuntu: ubuntu man pages and documentation
Communities#
Tools#
Filesystem#
which
: display executable's locationtype
: describe a command-
rm
: delete directory
Bashrm -rf
-
find
: find file/directory recursively
Bashfind . -iname _gsdata_ -exec rm {} + find . -type d -name _gsdata_ -print -exec rm -rfv {} + find . -type f \( -name .DS_Store -o -name Thumbs.db \) -print -exec rm {} +
-
tree
: show directory structure as tree
Bashtree -d [dir] # list directories only tree -ah # include hidden, human readable tree -fp -L <n> # include paths, permissions, limit up to N levels
-
df
: display free disk space
Bashdf -lhT # show usage of all mounted filesystems
-
du
: disk usage/size of directory
Bashdu -sh fooDir/
Package Management#
Command | Description |
---|---|
apt list --installed |
list installed pkgs |
sudo apt policy |
list installed `repositories' |
apt show <pkg> |
show dependencies of pkgs |
sudo apt install <pkg> |
install <pkg> |
sudo apt remove <pkg> |
uninstall <pkg> |
sudo apt autoremove |
uninstall unused dependencies e.g. <pkg> leftover deps that were auto installed but longer required |
sudo apt purge <pkg> |
uninstall <pkg> configuration files e.g. located under /etc |
Console Management#
-
env
: print the environment variables -
tail
: displays the last part of a file
tail -f [file] # outputs last lines in realtime
tail -n 100 [file] # outputs last n lines
-
bind
: configure keybinds for terminal -
bind
: query keybinds
Bashbind -l # List all bindable readline functions bind -v # List variables and values bind -psX # List keys bound to readline functions, macros, and shell executions bind -q <fn> # Query about which keys invoke the named function
-
bind
: change keybinds
Bashbind -u <fn> # Unbind all keys which are bound to the named function bind -r <keys> # Remove the binding for <keys> bind -f <filename> # Read key bindings from <filename> bind -x '"<keys>": "<cmds>"' # Execute <cmds> when <keys> is entered
-
less
: console pager
Navigation Key | Action |
---|---|
down /up ,j /k |
move down/up one line |
space bar /b |
move down/up one page |
left /right |
scroll left/right horizontally |
g /G ,Home /End |
go to start/end line |
/pattern /?pattern |
search forward/backward for pattern |
n /N |
go to next/previous occurrence |
q |
Quit less |
Networking#
- list connections
netstat --all | head -n 15
sudo netstat --tcp --udp --listening --program --numeric --extend
- query interaces
ip a
netstat -ie
Permissions#
stat
: pretty print stats about file
stat [file]
permission attributes
.rwxrwxrwx path/to/file
││││││││││
└│││││││││─ file type attribute
│││││││││ -: file
│││││││││ d: directory
│││││││││ l: symbolic link
│││││││││ c: character special file representing byte stream device e.g. /dev/null
│││││││││ b: block special file representing block stream device e.g. hard drive
│││││││││
├││││││││─ u: user owner
└││││││││─ r: read
└│││││││─ w: write
└││││││─ x: execute
││││││
├│││││─ g: group owner
└│││││─ r: read
└││││─ w: write
└│││─ x: execute
│││
├││─ o: other
└││─ r: read
└│─ w: write
└─ x: execute
-
id
: display user identity -
umask
: set the default file permissions -
chmod
: change permissions to folders and files
find . -type d -exec chmod 755 {} +
find . -type f -exec chmod 644 {} +
chmod
recursively to 775/664
chmod -R a=,a+rX,u+w,g+w /some/path/here
^ ^ ^ ^ adds write to group
| | | adds write to user
| | adds read to all and execute to all folders (which controls access)
| sets all to `000`
chown
: change ownership to user/group recursively
chown -R user:group /some/path/here
Process Management#
-
id
: find UID/GID for user
Bashid [username]
-
ps
: list running processes
Bashps aux pstree
-
pkill
: kill process by name
Bashpkill chrome
Services#
-
systemctl
: manage services/daemons -
systemctl start [name.service]
systemctl stop [name.service]
systemctl restart [name.service]
systemctl reload [name.service]
systemctl status [name.service]
systemctl is-active [name.service]
systemctl list-units --type service --all
- unit types
Bash$systemctl list-unit --type=help service # service target # group of units mount # filesystem mountpoint automount # filesystem auto-mountpoint device # kernel device names, which you can see in sysfs and udev path # file or directory scope # external processes not started by systemd slice # a management unit of processes snapshot # systemd saved state socket # IPC (inter-process communication) socket swap # swap file timer # systemd timer
-
list services
Bashsystemctl | grep running # Loaded+Active systemctl --no-pager | grep running | column -t # For more readable output systemctl list-units --no-pager --all # Loaded services: Active+Pending+Failed+Inactive+Dependents systemctl list-units --no-pager --all --state=inactive # Loaded services: Inactive systemctl list-units --no-pager --type=service # Loaded services: Active+Pending+Failed systemctl list-units --no-pager --type=service --state=running # Loaded services: Running systemctl list-unit-files --no-pager --type=service # Installed services: Loaded+Unloaded
-
start/stop/restart the specified service
Bashsystemctl start [service] systemctl stop [service] systemctl restart [service]
-
enable/disable service to auto start at boot time
Bashsystemctl enable [service] systemctl disable [service]
-
show/check service current status/runtime information
Bashsystemctl status [service] systemctl is-active [service] systemctl is-enabled [service] systemctl is-failed [service] systemctl list-dependencies [service]
Diagnostics#
syslog
: a protocol for transferring log messages-
dmesg
: command that prints kernel logs
Bashdmesg -H # command that prints kernel logs
-
journalctl: service that collects logs from
systemd
Bashjournalctl # all boot log entries journalctl -k # all boot kernel entries journalctl -b # current boot entries journalctl -b -k # current boot kernel entries journalctl -b --priority=err # all error or higher logs since last boot journalctl -b --priority=err..warning # all warning to notice since last boot # Log Levels # emerg (0): system is unusable # alert (1): action must be taken immediately # crit (2): critical conditions # err (3): error conditions # warn (4): warning conditions # notice (5): normal but significant condition # info (6): informational # debug (7): debug-level messages