Skip to content

Instantly share code, notes, and snippets.

@IngmarBoddington
Last active August 23, 2020 16:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IngmarBoddington/4226355 to your computer and use it in GitHub Desktop.
Save IngmarBoddington/4226355 to your computer and use it in GitHub Desktop.
General Linux / Bash Terminal Reference, commonly used commands and file locations. Key: <value> type of value will be hinted by string when relevant | [<optionalValue>] | <file(s)> means single filename or multiple space delimited filenames | <int>
Command History
===============
!! - Repeat previous command
!!:/s/<search>/<replace>/g - Repeat previous command with replaced string value
sudo !! - Repeat previous command with sudo
history - Show bash history
!<int> - Run a labelled command from history command
!<command> a - Re-run last use of command
<command>:p - Print rather than run (e.g. !!:p, to print last run command)
^<search>^<replace> - Run previous command replacing search with replace (for typos)
<command> !* - Use first argument from last command
<command> !:n - Use nth argument from last command
<command> !$ - Use last argument from last command
Shortcuts
===================
Ctrl + r - Search history
Up / Down - Navigate history
Tab - Autocomplete (double tap for matches if more than one)
Ctrl + e - Goto end of line
Ctrl + a - Goto start of line
Ctrl + d - Delete
Ctrl + w - Clear word before cursor
Ctrl + u - Clear current line
Ctrl + k - Clear from cursor
Ctrl + l - Clear screen
Ctrl + c - Cancel current process
Command Basics
==============
sudo <command> - Issue command with root / sudoer privileges
man <command> - Manual page for command
<command> & - Spin off command as background process
Ctrl + z - Suspend current process
bg - Send suspended process to background process
fg - Bring background process to foreground
alias a="command" - Set an alias (command shortcut)
nohup <command> & - Run command in background with hangup signals ignored
<command> | at <HH:MM> - Run command at specified time
<command1> | <command2> - Pipe output from command1 to command2
<command1> | xargs <command2> - Use output from command1 as arguments for command2
<command> > <outfile> - Redirect stdout to outfile
<command> >> <outfile> - Redirect stdout to outfile(append rather than overwrite)
<command> < <infile> - Redirect stdin to command
<command> 2> <outfile> - Redirect stderr to outfile
<command> 2>&1 - Redirect stderr to stdout
<command> &> <outfile> - Redirect stdout and stderr to outfile
<command> <text>{1,2} - Expands to <command> <text>1 <text>2
<command> <text>{,2} - Expands to <command> <text> <text>2
clear - Clears terminal window
Special Characters
==================
\n - New line
\t - Tab
* - Represent wildcard in arguments (glob)
-- - Use to escape any further dashes in a command
. - Current working directory
.. - Parent directory
- - Last working directory
~ - Home directory
; - Command terminator / seperator
Basic Commands
==============
id - Show current user info
whoami - Show current username
finger - Show system users info
w [<username>] - Show logged in users (or specified user) and what they are currently doing
su <user> - Switch user
echo <string> - Send string to stdout
shutdown -h now - Shutdown (halt) now
shutdown -r now - Shutdown (restart) now (can just use 'reboot' on some systems
env - Display current environment variables and values
hostname [<new>] - Show or with argument set hostname
uname -a - Display kernel and OS version
date - Show system date / time
time <command> - Track time for command to complete
time cat - Start stopwatch
top - System resource usage statistics
ps - Show running processes for current user
ps -e - Show running processes for all users
ps -ef - Show running processes for all users with expanded information
kill <processId(s)> - Kill a process
killall <name> - Kill a process by name
fuser <file> - Find process which is using a file
strace -t <command> - Trace execution path of command with timestamps
strace -t -p <pid> - Trace a process with timestamps and optionally write to outfile
df -h - Disk usage statisics (all disks)
du -sh - Size of current directory / contents
du -hsx * | sort -rh | head -10 - List top 10 size hogs
screen
- Launch screen (multi terminal manager, identified with ints)
- Ctrl-a C to create a new screen
- Crtl-a K to kill current screen
- Ctrl-a " lists screens
- Ctrl-a <int> to switch to specified screen
pwd - Display current working directory
cd <directory> - Change current working directory
ls - List contents of current working directory
ls -l - List contents of current working directory (one per line with more info)
ls -A - List contents of current working directory (including hidden files)
ls -lrt - List contents of current working directory (+info, in reverse order of last touch)
lsof - List open files in current working directory
mkdir <directory> - Create directory
rmdir <directory> - Remove directory
rm <file> - Delete file
rm -r <directory> - Delete directory and contents (recursive)
rm -rf <directory> - Delete directory and contents (recursive), without warnings
cp <file> <destination> - Copy file
cp -r <directory> <destination> - Copy directory
mv <file> <destination> - Move (or rename) file or directory
><file> - Truncate file
ln <target> - Create (hard) symlink to target in current working directory
ln -s <target> - Create (symbolic) symlink to target directory in current working directory
touch <file> - Create empty file / update last modified timestamp
basename <path> - Returns last part of path
mount -a - Mount everything in /etc/fstab
find [<path>] <expression>
- Find file matching expression
- Optional path points to root of search
- e.g. find . -name error.html //find all files named error.html in current directory and descendants
Text Commands
=============
vi <filename> - Edit / create file using vi
vim <filename> - Edit / create file using vim
nano <filename> - Edit / create file using nano
less <file(s)> - Display file contents, line by line scroll with Enter, F to trace, Q to quit
more <file(s)> - Display file contents, line by line scroll with Enter (basic / slower version of less)
cat <file(s)> - Concatenate files and send to stdout
cat -n <file(s)> - Concatenate files and send to stdout with line numbers
cat ><file> - Redirect stdin to file, Ctrl-C to finish (does not include last line in file)
cat >><file> - Same as above but appends rather than overwrites
head [-<int>] <file(s)> - Show top 10 lines of file (or specify number optionally)
tail [-<int>] <file(s)> - Show last 10 lines of file (or specify number optionally), add -F to trace
wc <file(s)> - Print byte, word, and line counts for a file (and filename), use -c, -l, -w to return byte, line and work count only (and filename)
nl <file(s)> - Add line numbers
tr <set> <set> - Replace values from stdin, print to standard out, e.g. tr [da] [bc] would replace d with b and a with c
tr -d <set> - Same as above except removes characters without replace
tr -s <set> - Same again except removes duplications of characters in set
printf <format> <value> - Use to format values, full list of modifiers: http://ss64.com/bash/printf.html
for file in `ls`; do sed 's/find/replace/g' $file > /tmp/tmp; mv /tmp/tmp $file; done;
- Replace all matched text in all files in dir
expand -t 4 <filename> > /tmp/tmp; mv /tmp/tmp <filename>
- Replace tabs with 4 spaces, move file over temp version created in first command
unexpand -t 4 <filename> > /tmp/tmp; mv /tmp/tmp <filename>
- Opposite of previous commands
cut [-b <int|range>] [-c <int|range>] [-d <int|range> [-f <int|range>]] <file(s)>
- Cut a file into columns for standard out
- -b option to select a byte position / range
- -c option to select a character position / range
- -f option to select a field position / range (where field delimiter specified using -d)
sort <file(s)> - Sort lines from file(s), use -u to only sort unique lines
grep <string> <file(s)>
- Find a string in file(s)
- Use -i to make case insensitive
- Use -e for regex as string
- Use -B to specify number of lines to grab before match
- Use -A to specify number of lines to grab before match
egrep <pattern> <file(s)>
- Use regex for matching
zgrep <string> <file(s)>
- Same as grep but works with compressed files
awk <program> <file(s)>
- Find and replace / sorting / filtering using the awk language
- <program>
'<pattern> { <action> }
.....'
- omit pattern to match every line, omit action to print nothing, omit action and brackets to print whole matched line
- actions should be terminated with semicolons
- pattern can use /regEx/, &&, ||, !, ternary operator (a?b:c), range (a,b), before file read (BEGIN), after file read (END)
- Can match with special operators using ~ e.g. awk '$4 ~ /regex/ {action} //Do action if 4th field matched regex
- action can be a combination below as well as basic arithmetic:
- { print $X }
- if X is 0 then print whole line, else print Xth item on line
- { print <special>}
- NR prints line number
- NF print number of fields in line
- FILENAME prints current input filename
- e.g. $NR is last field
- Can use literal and escape chars e.g. "This\tis\ttext\twith\ttabs"
- Variable assignment: var=value;
- Operators ++, --, +, -, *, /
awk -f <script> <file(s)
- Same as above only reads program from file
awk '!a[$0]++' file
- Remove duplicate lines
egrep "Notice|NOTICE|Strict|STRICT" <file> | sed 's/^.\{22\}//g' | LC_ALL=C sort | uniq -c | sort -rn | head -n 100
- Find top 100 NOTICE / STRICT errors in a PHP error log
Archiving Commands
==================
tar -cvf <archive> <files> - Create archive, add z for compression, add --exclude=<glob>before <files> for exclusions
tar -xvf <archive> [<location>] - Expand archive (optionally to destination), add z for decompression
tar -t <archive> - List file contents of archive
gzip <file> - Compress file
gzip -d <file> - Decompress file
Networking Commands
===================
wget <address> - Fetch file from web address
ssh [<user>]@<host> - SSH to host as user
scp [[user@]host1:]<file1> [[user@]host2:]<file2>
- Secure file copy using ssh
- Use -r flag for copying directories
/etc/init.d/network <restart|start|stop> - Control networking
ifconfig - Display and set networking / interface settings
ping <host> - Standard icmp ping
traceroute <host> - Routing info / connection debug
mtr <host> - Advanced traceroute / ping
netstat - Networking status
netstat -a - List all listening ports
netstat -tp - List services by PID
nslookup <host> - DNS lookup
nmap <host> - Port scanning
mount -t cifs -o username="[username]",password"[Password]" //[IP]/[folder] [folder]
- Mount network drive
route -n - Show routing
route del <-host|-net> <ip> netmask <netmask>
- Delete route
route add <-host|-net> <ip> netmask <netmask> gw <ip> dev <interface>
- Add route
route add <-host|-net> gw <ip> dev <interface>
- Add default route
Permission Commands
===================
chmod <maskOrMod> <file(s)> - Alter file permissions
chmod -r <maskOrMod> <file(s)> - Alter file permissions recursively
Modifications in form /(u+g+o+a+)([\+|\-])(r+w+e+)/ where:
- u is current user
- g is group
- o is others
- a is all
- + to add permissions
- - to remove permissions
- r is read
- w is write
- e is execute
Mask values:
- 4 is read
- 2 is write
- 1 is execute
- e.g. 655 is user (read / write), group (read / execute), others (read / execute)
chgrp <group> <file> - Change group of a file
useradd -a -G <group> <user> - Add user to group
Important Files (Vary on system / versions)
===============
~/.bashrc - Loaded on new terminal
~/.bash_profile - Loaded on new terminal
/dev/null - Blackhole (redirect here to null)
/etc/fstab - The place for mounting, e.g. //<server>/<folder> <localfolder> cifs auto,username-[username],password=[password],uid=0,gid=500 0 0
/etc/hosts - The place for hardcoding DNS entries
/var/log/ - Log files
/etc/sudoers - Sudoers settings
/etc/rc.d/rc.local - Startup script location
/usr/local/bin - Executable scripts (to make available as system wide commands)
/etc/httpd/conf.d/httpd.conf - Apache config
/etc/apache2/apache2.conf - Apache2 config
/etc/yum.conf - Yum config
/etc/network/interfaces - Network interfaces config
Aptitude (Package Management for Debian, Ubuntu, Mint...)
========================================================
apt-cache search <package> - Search apt for package, use ^package$ for exact match, use "package" for description search
apt-cache show <package> - Show basic package info
apt-cache showpkg <package> - Show detailed packaged info (including dependancies)
apt-cache depends <package> - Show dependancies for package
apt-get install <package> - Install a program / package
apt-get --purge remove <pkg> - Completely remove a program / package
apt-get autoclean - Erase old downloaded archive files
apt-get --purge autoremove - Remove automatically all unused packages
apt-get update - Update package index
apt-get upgrade - Update installed packages
Yum (Package Management for Red Hat, Fedora...)
===============================================
yum clean all - Clean yum cache
yum list <package> - Search for package
yum install <package> - Install package
yum downgrade <package> - Rollback package to previous version
yum check-update - Check for updates for installed packages
yum update - Update installed packages
yum update <package> - Update package
RPM (Package management)
======================
rpm -ivh <file> - Install
rpm -Uvh <file> - Upgrade rpm
rpm -ev <name> - Remove package
Minicom (Serial Connector)
==========================
minicom - Launch
minicom -s - Setup
minicom -c <file> - Launch with output capture file
Apache2 Commands
===============
/etc/init.d/apache2 <restart|start|stop> - Control web server (apache2)
a2enmod <module> - Enable module
a2dismod <module> - Disable module
a2ensite <site> - Enable site
a2dissite <site> - Disable site
Cron
====
crontab -e - Edit user cronjobs
crontab -l - Display user cronjobs
/etc/init.d/cron <restart|start|stop>
/etc/init.d/crond <restart|start|stop>
- Control cron service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment