Skip to main content

Unix Commands - A Cheat Sheet

· 11 min read
Deniz Colak

Unix is a powerful operating system that provides developers with a vast array of tools and commands for working with files, directories, and data on the command line. As a developer, mastering unix commands can help you automate repetitive tasks, manipulate large volumes of data, and become more productive.

This Unix commands cheat sheet provides developers with a comprehensive guide and advanced Unix commands with examples and tips to use each command effectively.

Basic Commands

CommandDescription
cdChange directory
pwdPrint working directory
lsList contents of directory
mkdirMake a new directory
rmdirRemove a directory
cpCopy a file or directory
mvMove a file or directory
rmRemove a file
catPrint the contents of a file
touchCreate an empty file or update the modification time of a file
lessView the contents of a file one screen at a time
headPrint the first few lines of a file
tailPrint the last few lines of a file
grepSearch for a pattern in a file
sedStream editor for filtering and transforming text
awkPattern scanning and processing language
psDisplay information about running processes
topDisplay system resource usage and running processes
killSend a signal to a process
sudoExecute a command as another user
suSwitch to another user account
apt-getInstall or manage software packages on Ubuntu and Debian
yumInstall or manage software packages on Red Hat and CentOS
systemctlControl the systemd system and service manager
ifconfigConfigure network interfaces and display network information
pingTest network connectivity to a host
nslookupQuery the DNS server for a domain name
netstatDisplay network connections and statistics
sshSecurely log in to a remote system
scpSecurely copy files between systems
wgetDownload files from the web
curlTransfer data from or to a server
echoPrint text to the screen or a file
dateDisplay the current date and time
historyDisplay the command history
chmodChange file permissions
chownChange file ownership
tarCreate or extract tar archives
zipCreate or extract zip archives
rm -rRemove a directory and its contents recursively
rm -fForce removal of a file or directory
rm -iInteractive removal of a file or directory
findSearch for files in a directory hierarchy
locateSearch for files by name
whichDisplay the path to a command
CommandDescription
cdChange directory
pwdPrint working directory
lsList contents of directory
mkdirMake a new directory
rmdirRemove a directory
cpCopy a file or directory
mvMove a file or directory
rmRemove a file

File Management Commands

CommandDescription
catPrint the contents of a file
touchCreate an empty file or update the modification time of a file
lessView the contents of a file one screen at a time
headPrint the first few lines of a file
tailPrint the last few lines of a file
grepSearch for a pattern in a file
sedStream editor for filtering and transforming text
awkPattern scanning and processing language

System Management Commands

CommandDescription
psDisplay information about running processes
topDisplay system resource usage and running processes
killSend a signal to a process
sudoExecute a command as another user
suSwitch to another user account
apt-getInstall or manage software packages on Ubuntu and Debian
yumInstall or manage software packages on Red Hat and CentOS
systemctlControl the systemd system and service manager

Network Commands

CommandDescription
ifconfigConfigure network interfaces and display network information
pingTest network connectivity to a host
nslookupQuery the DNS server for a domain name
netstatDisplay network connections and statistics
sshSecurely log in to a remote system
scpSecurely copy files between systems
wgetDownload files from the web
curlTransfer data from or to a server

Miscellaneous Commands

CommandDescription
echoPrint text to the screen or a file
dateDisplay the current date and time
historyDisplay the command history
chmodChange file permissions
chownChange file ownership
tarCreate or extract tar archives
zipCreate or extract zip archives

Advanced File Management Commands

CommandDescription
rm -rRemove a directory and its contents recursively
rm -fForce removal of a file or directory
rm -iInteractive removal of a file or directory
findSearch for files in a directory hierarchy
duDisplay disk usage of files and directories
dfDisplay free disk space

Input and Output Commands

CommandDescription
grepSearch for a pattern in a file or input
awkProcess text files and perform operations on input
sedStream editor for filtering and transforming text
sortSort lines of text files
uniqReport or omit repeated lines in a file
teeRedirect output to a file and to the screen

Text Manipulation Commands

CommandDescription
cutSelect portions of text from lines of input
pasteMerge lines of files
head -nPrint the first n lines of a file
tail -nPrint the last n lines of a file
trTranslate or delete characters
wcCount the number of lines, words, or characters in a file
awkPattern scanning and processing language

Pipeline and Redirection Commands

CommandDescription
``
>Redirect output to a file
>>Append output to a file
<Redirect input from a file

Advanced Commands with Examples

awk

awk is a powerful tool for processing text files and performing operations on input. Here are some examples of how awk can be used:

  • Print the first column of a space-separated file:
awk '{print $1}' file.txt

  • Sum the second column of a comma-separated file:
awk -F ',' '{sum += $2} END {print sum}' file.csv

  • Print lines that match a pattern:
awk '/pattern/' file.txt

grep

grep is a tool for searching for a pattern in a file or input. Here are some examples of how grep can be used:

  • Search for a pattern in a file:
grep 'pattern' file.txt

  • Search for a pattern in all files in a directory:
grep 'pattern' /path/to/directory/*

  • Search for a pattern in all files in a directory and its subdirectories:
grep -r 'pattern' /path/to/directory/

wc

wc is a tool for counting the number of lines, words, or characters in a file. Here are some examples of how wc can be used:

  • Count the number of lines in a file:
wc -l file.txt

  • Count the number of words in a file:
wc -w file.txt

  • Count the number of characters in a file:
wc -c file.txt

du

du is a tool for displaying disk usage of files and directories. Here are some examples of how du can be used:

  • Display the size of a file or directory:
du -h file.txt

  • Display the size of all files and directories in the current directory:
du -h *

  • Display the size of all files and directories in a directory and its subdirectories:
du -h /path/to/directory/

sort

sort is a tool for sorting lines of text files. Here are some examples of how sort can be used:

  • Sort a file alphabetically:

sort file.txt

  • Sort a file numerically:

sort -n file.txt

  • Sort a file in reverse order:

sort -r file.txt

uniq

uniq is a tool for reporting or omitting repeated lines in a file. Here are some examples of how uniq can be used:

  • Print only unique lines in a file:

uniq file.txt

  • Print only repeated lines in a file:

uniq -d file.txt

  • Print only lines that occur exactly N times in a file:

uniq -c file.txt | grep '^[ \t]*N'

tar

tar is a tool for archiving and compressing files and directories. Here are some examples of how tar can be used:

  • Create a tar archive of a directory:

tar -cvf archive.tar /path/to/directory/

  • Extract a tar archive:

tar -xvf archive.tar

  • Create a compressed tar archive:

tar -czvf archive.tar.gz /path/to/directory/

sed

sed is a stream editor for filtering and transforming text. Here are some examples of how sed can be used:

  • Replace a string in a file:

sed -i 's/old-string/new-string/g' file.txt

  • Delete lines that match a pattern in a file:

sed -i '/pattern/d' file.txt

  • Insert a line before a matching pattern in a file:

sed -i '/pattern/i new-line' file.txt

find

find is a tool for searching for files and directories based on various criteria. Here are some examples of how find can be used:

  • Find all files with a certain name pattern:

find /path/to/directory/ -name '*.txt'

  • Find all files modified within the last N days:

find /path/to/directory/ -mtime -N

  • Find all files larger than a certain size:

find /path/to/directory/ -size +10M

cut

cut is a tool for cutting out sections from each line of a file. Here are some examples of how cut can be used:

  • Cut out the first column of a tab-separated file:

cut -f 1 file.tsv

  • Cut out the first N characters of each line of a file:

cut -c 1-N file.txt

  • Cut out a range of characters of each line of a file:

cut -c N-M file.txt

head and tail

head and tail are tools for displaying the first or last N lines of a file. Here are some examples of how head and tail can be used:

  • Display the first 10 lines of a file:

head file.txt

  • Display the first N lines of a file:

head -n N file.txt

  • Display the last 10 lines of a file:

tail file.txt

  • Display the last N lines of a file:

tail -n N file.txt

xargs

xargs is a tool for passing arguments to a command from standard input. Here are some examples of how xargs can be used:

  • Find all files with a certain name pattern and delete them:

find /path/to/directory/ -name '*.txt' | xargs rm

  • Count the number of lines in all files in a directory:

find /path/to/directory/ -type f -print0 | xargs -0 wc -l