Skip to main content

Linux Cheat Sheets commands

·2108 words·10 mins· ·
Table of Contents

Linux Cheat Sheets commands
#

This contains different heloers for commands on a linux os.

Unix Command-Line Utilities Cheat-Sheet
#

Using Cron and placing crontab files
#

CRON Fields
#

FieldAllowed ValuesDescription
MINUTE0-59Trigger every MINUTE minute(s)
HOUR0-23Trigger every HOUR hour(s)
DAY OF MONTH1-31Trigger on specific DAY of the month
MONTH1-12Trigger in MONTH month(s)
DAY OF WEEK0-6 (Sun-Sat)Trigger on specific DAY OF WEEK, where Sunday = 0

Special Characters in CRON
#

Special CharacterDescription
*Represents all possible values for the field
,Separates items in a list
-Specifies a range of values
/Specifies increments

CRON Expression Examples
#

Cron ExpressionDescription
* * * * *Executes every minute
0 * * * *Executes on the hour every hour
0 0 * * *Executes at midnight every day
0 0 1 * *Executes at midnight on the first of every month
30 20 * * 6Executes at 8:30 PM every Saturday
*/5 * * * *Executes every five minutes
0 0 8-10 * * *Executes on the hour every hour from 8 AM to 10 AM

Executing a Script and Redirecting Output to a Log File
#

0 5 * * * /path/to/script.sh >> /path/to/logfile.log 2>&1

This CRON job example runs a script located at /path/to/script.sh every day at 5 AM, appending both standard output and standard error to /path/to/logfile.log.

Common CRON Directories and Files
#

Location/FileDescription
/etc/crontabSystem-wide crontab file where administrators can define CRON jobs.
/etc/cron.dDirectory for additional system-wide cron job files. CRON jobs in this directory allow for easier management of individual cron job configurations.
/etc/cron.dailyDirectory for scripts that need to run daily.
/etc/cron.hourlyDirectory for scripts that need to run hourly.
/etc/cron.weeklyDirectory for scripts that need to run weekly.
/etc/cron.monthlyDirectory for scripts that need to run monthly.
/var/spool/cron/crontabsDirectory where individual user crontab files are stored. Users can schedule their personal jobs using the crontab command.
/etc/cron.allowFile containing usernames that are allowed to use crontab. If this file exists, only users listed in it can schedule cron jobs.
/etc/cron.denyFile containing usernames that are denied access to crontab. If cron.allow does not exist, all users except those listed in cron.deny can schedule jobs.
Example of Adding a System-Wide CRON Job
#

To add a system-wide CRON job, you might place a file in /etc/cron.d/. Here’s an example of what the contents might look like:

# Example CRON job in /etc/cron.d/myjob
0 5 * * * root /usr/local/bin/daily-task.sh

MySQL Cheat-Sheet
#

Connect to MySQL
#

CommandDescription
mysql -u root -pConnect to MySQL as root user
mysql -u <user> -pConnect to MySQL as a specific user
mysql -u root -p -h <host>Connect to MySQL on a specific host

Backup and Restore
#

CommandDescription
mysqldump -u root -p <database> > backup.sqlBackup a database to a file
mysql -u root -p <database> < backup.sqlRestore a database from a file

PostgreSQL Cheat-Sheet
#

Connect to PostgreSQL
#

CommandDescription
psql -U postgresConnect to PostgreSQL as the postgres user
psql -U <user> -d <database>Connect to PostgreSQL as a specific user and database
psql -U <user> -d <database> -h <host>Connect to PostgreSQL on a specific host

PostreSQL CLI Commands
#

CommandDescription
\c <database>Connect to a specific database
\password <user>Change password for a specific user
\lList all databases
\d+Show detailed information about various database objects
\dtList all tables in the current database
\duList all users
\dfList all functions
\dvList all views
\dnList all schemas
\dpList all permissions
\diList all indexes
\dsList all sequences
\d+Show detailed information about various database objects
\qQuit psql
\xToggle expanded output

Backup and Restore
#

CommandDescription
pg_dump <database> > backup.sqlBackup a database to a file
psql <database> < backup.sqlRestore a database from a file

Patterns
#

Grep Cheat-Sheet
#

CommandDescription
grep 'pattern' fileSearch for a pattern in a file
grep -i 'pattern' fileCase insensitive search
grep -r 'pattern' dirRecursively search for a pattern in all files under the directory
grep -v 'pattern' fileInvert match to display lines that do not contain the pattern
grep -n 'pattern' fileDisplay the line numbers with the output
grep -c 'pattern' fileCount the number of lines that match the pattern
grep -l 'pattern' *Show only the names of files with matching lines
grep -L 'pattern' *Show only the names of files without matching lines
grep -o 'pattern' fileShow only the part of a line matching the pattern
grep 'pattern1|pattern2' fileSearch for lines matching pattern1 or pattern2
grep -A 3 'pattern' fileDisplay 3 lines after the matching line
grep -B 3 'pattern' fileDisplay 3 lines before the matching line
grep -C 3 'pattern' fileDisplay 3 lines before and after the matching line
grep '^pattern' fileMatch lines beginning with ‘pattern’
grep 'pattern$' fileMatch lines ending with ‘pattern’
grep '^[^#]' fileIgnore lines starting with ‘#’ (comments)
`grep -E ‘patt(ern1ern2)’ file`
grep -w 'pattern' fileMatch whole word ‘pattern’
grep -f patterns.txt fileUse patterns from the file, one per line
grep -e 'pattern1' -e 'pattern2' fileSearch for multiple patterns

SED Cheat-Sheet
#

CommandDescription
sed 's/pattern/replacement/' fileReplace the first occurrence of a pattern in each line
sed -i 's/pattern/replacement/' fileReplace all occurrences of a pattern in the file (in-place editing)
sed '/pattern/d' fileDelete lines that match the pattern
sed '2d' fileDelete the second line of the file
sed '2,$d' fileDelete from the second line to the end of the file
sed 's/pattern/replacement/g' fileReplace all occurrences of a pattern in each line
sed -n 'p' filePrint the output (useful with -n to suppress other output)
sed '/pattern/p' filePrint only lines that match the pattern
sed '1,5s/pattern/replacement/' fileApply the substitution to lines 1 to 5 only
sed -e 'command1' -e 'command2' fileApply multiple editing commands in sequence
sed '5q' filePrint until the 5th line of the file then quit
sed 's/[a-z]/\U&/g' fileConvert lowercase letters to uppercase

SORT Cheat-Sheet
#

CommandDescription
sort fileSort lines of text alphabetically in a file
sort -n fileSort numerically (useful for sorting numbers)
sort -r fileReverse the results of sorts (descending order)
sort -o output_file fileWrite the result to the output_file instead of standard output
sort -k 2 fileSort a file based on the second column of data
sort -u fileSort and remove duplicate lines
sort -t':' -k 3n fileSort a file using ‘:’ as a delimiter and numerically by the third column
sort -f fileIgnore case while sorting
sort -m file1 file2Merge already sorted files file1 and file2
sort -c fileCheck whether the file is already sorted; do not sort

AWK Cheat-Sheet
#

CommandDescription
awk '/pattern/ {print $1}'standard Unix shells
awk '/pattern/ {print "$1"}'compiled with DJGPP, Cygwin
awk "/pattern/ {print \"$1\"}"GnuWin32, UnxUtils, Mingw
awk '1;{print ""}'double space a file
awk 'BEGIN{ORS="\n\n"};1'double space a file
awk 'NF{print $0 "\n"}'double space a file which already has blank lines
awk '1;{print "\n"}'triple space a file
awk '{print FNR "\t" $0}' files*precede each line by its line number
awk '{print NR "\t" $0}' files*precede each line by its line number for all files together
awk '{printf("%5d : %s\n", NR,$0)}'number each line of a file
awk 'NF{$0=++a " :" $0};1'number each line of a file, but only print numbers if line is not blank
awk 'END{print NR}'count lines (emulates “wc -l”)
awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}'print the sums of the fields of every line
awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s}'add all fields in all lines and print the sum
awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'print every line after replacing each field with its absolute value
awk '{for (i=1; i<=NF; i++) $i = ($i < 0) ? -$i : $i; print }'print every line after replacing each field with its absolute value
awk '{ total = total + NF }; END {print total}' fileprint the total number of fields (“words”) in all lines
awk '/Beth/{n++}; END {print n+0}' fileprint the total number of lines that contain “Beth”
awk '$1 > max {max=$1; maxline=$0}; END{ print max, maxline}'print the largest first field and the line that contains it
awk '{ print NF ":" $0 }'print the number of fields in each line, followed by the line
awk '{ print $NF }'print the last field of each line
awk '{ field = $NF }; END{ print field }'print the last field of the last line
awk 'NF > 4'print every line with more than 4 fields
awk '$NF > 4'print every line where the value of the last field is > 4

Network
#

Ethtool Cheat-Sheet
#

Displaying Information
#

CommandDescription
ethtool <interface>Display information about a specific network interface
ethtool -i <interface>Display driver information
ethtool -a <interface>Display all settings
ethtool -k <interface>Display offload settings
ethtool -c <interface>Display coalescing settings
ethtool -g <interface>Display ring buffer settings
ethtool -l <interface>Display large receive offload settings
ethtool -S <interface>Display statistics
ethtool -t <interface>Test the network interface for offloading capabilities
ethtool -T <interface>Display time stamping settings
ethtool -x <interface>Display channel settings
ethtool -P <interface>Display permanent MAC address
ethtool -N <interface>Display offload settings
ethtool -u <interface>Display bus information
ethtool -d <interface>Display register dump
ethtool -g <interface>Display ring buffer settings

Setting Parameters
#

CommandDescription
ethtool -G <interface>Set ring buffer settings
ethtool -L <interface>Set large receive offload settings
ethtool -A <interface>Set pause parameters
ethtool -C <interface>Set coalescing settings
ethtool -K <interface>Set offload settings
ethtool -N <interface>Set offload settings
ethtool -p <interface>Blink the LED on the network interface
ethtool -r <interface>Reset the network interface

ARP in Linux
#

CommandDescription
arpView the ARP table
arp -aView the ARP table
arp -nView the ARP table (don’t resolve names)
arp -d <ip>Delete an entry from the ARP table
arp -s <ip> <mac_address>Add an entry to the ARP table
arp -i <interface> -s <ip> <mac_address>Add an entry to the ARP table for a specific interface
arp -i <interface> -d <ip>Delete an entry from the ARP table for a specific interface
arp -i <interface> -nView the ARP table for a specific interface
arp -i <interface> -aView the ARP table for a specific interface
ip neigh showView the ARP table
ip neigh show <ip>View the ARP table for a specific IP address
ip neigh add <ip> lladdr <mac_address> dev <interface>Add an entry to the ARP table
ip neigh change <ip> lladdr <mac_address> dev <interface>Change an entry in the ARP table
ip neigh del <ip> dev <interface>Delete an entry from the ARP table
ip neigh flush dev <interface>Flush the ARP table for a specific interface
ip neigh flush allFlush the ARP table
ip -s neigh showShow ARP statistics
ip -s neigh flush allFlush the ARP cache

Linux One-Liners
#

This table provides useful one-liner commands for Linux users, ideal for system administration and development tasks.

CommandDescription
ps auxf | sort -nr -k 3 | head -10Display a tree of system processes, sorted by memory usage.
df -hCheck available disk space on all mounted filesystems in a human-readable format.
topMonitor real-time system processes.
lsof -p $$List all open files by a specific process (replace $$ with the process ID).
wc -l filenameCount the number of lines in a file named filename.
sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 fileSort a list of IP addresses stored in a file.
sort file | uniq -c | sort -nr | headFind the most frequently occurring lines in a file, useful for log analysis.
netstat -tulnCheck all listening ports on the system.
wget --mirror -p --convert-links -P ./LOCAL-DIR website-urlDownload a website and all of its assets for offline viewing, storing in LOCAL-DIR.
tar czf backup.tar.gz /path/to/directoryCreate a compressed backup of a directory.
newuser="xyzuser" && sudo useradd -m $newuser && echo "$newuser ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/$newuserCreate a new user defined by newuser, and add a sudoers file for them with NOPASSWD enabled.
ls -lhtr --color=alwaysSort by last writen to reverse
Kristian
Author
Kristian