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
Command | Description |
---|---|
cd | Change directory |
pwd | Print working directory |
ls | List contents of directory |
mkdir | Make a new directory |
rmdir | Remove a directory |
cp | Copy a file or directory |
mv | Move a file or directory |
rm | Remove a file |
cat | Print the contents of a file |
touch | Create an empty file or update the modification time of a file |
less | View the contents of a file one screen at a time |
head | Print the first few lines of a file |
tail | Print the last few lines of a file |
grep | Search for a pattern in a file |
sed | Stream editor for filtering and transforming text |
awk | Pattern scanning and processing language |
ps | Display information about running processes |
top | Display system resource usage and running processes |
kill | Send a signal to a process |
sudo | Execute a command as another user |
su | Switch to another user account |
apt-get | Install or manage software packages on Ubuntu and Debian |
yum | Install or manage software packages on Red Hat and CentOS |
systemctl | Control the systemd system and service manager |
ifconfig | Configure network interfaces and display network information |
ping | Test network connectivity to a host |
nslookup | Query the DNS server for a domain name |
netstat | Display network connections and statistics |
ssh | Securely log in to a remote system |
scp | Securely copy files between systems |
wget | Download files from the web |
curl | Transfer data from or to a server |
echo | Print text to the screen or a file |
date | Display the current date and time |
history | Display the command history |
chmod | Change file permissions |
chown | Change file ownership |
tar | Create or extract tar archives |
zip | Create or extract zip archives |
rm -r | Remove a directory and its contents recursively |
rm -f | Force removal of a file or directory |
rm -i | Interactive removal of a file or directory |
find | Search for files in a directory hierarchy |
locate | Search for files by name |
which | Display the path to a command |
Navigation Commands
Command | Description |
---|---|
cd | Change directory |
pwd | Print working directory |
ls | List contents of directory |
mkdir | Make a new directory |
rmdir | Remove a directory |
cp | Copy a file or directory |
mv | Move a file or directory |
rm | Remove a file |
File Management Commands
Command | Description |
---|---|
cat | Print the contents of a file |
touch | Create an empty file or update the modification time of a file |
less | View the contents of a file one screen at a time |
head | Print the first few lines of a file |
tail | Print the last few lines of a file |
grep | Search for a pattern in a file |
sed | Stream editor for filtering and transforming text |
awk | Pattern scanning and processing language |
System Management Commands
Command | Description |
---|---|
ps | Display information about running processes |
top | Display system resource usage and running processes |
kill | Send a signal to a process |
sudo | Execute a command as another user |
su | Switch to another user account |
apt-get | Install or manage software packages on Ubuntu and Debian |
yum | Install or manage software packages on Red Hat and CentOS |
systemctl | Control the systemd system and service manager |
Network Commands
Command | Description |
---|---|
ifconfig | Configure network interfaces and display network information |
ping | Test network connectivity to a host |
nslookup | Query the DNS server for a domain name |
netstat | Display network connections and statistics |
ssh | Securely log in to a remote system |
scp | Securely copy files between systems |
wget | Download files from the web |
curl | Transfer data from or to a server |
Miscellaneous Commands
Command | Description |
---|---|
echo | Print text to the screen or a file |
date | Display the current date and time |
history | Display the command history |
chmod | Change file permissions |
chown | Change file ownership |
tar | Create or extract tar archives |
zip | Create or extract zip archives |
Advanced File Management Commands
Command | Description |
---|---|
rm -r | Remove a directory and its contents recursively |
rm -f | Force removal of a file or directory |
rm -i | Interactive removal of a file or directory |
find | Search for files in a directory hierarchy |
du | Display disk usage of files and directories |
df | Display free disk space |
Input and Output Commands
Command | Description |
---|---|
grep | Search for a pattern in a file or input |
awk | Process text files and perform operations on input |
sed | Stream editor for filtering and transforming text |
sort | Sort lines of text files |
uniq | Report or omit repeated lines in a file |
tee | Redirect output to a file and to the screen |
Text Manipulation Commands
Command | Description |
---|---|
cut | Select portions of text from lines of input |
paste | Merge lines of files |
head -n | Print the first n lines of a file |
tail -n | Print the last n lines of a file |
tr | Translate or delete characters |
wc | Count the number of lines, words, or characters in a file |
awk | Pattern scanning and processing language |
Pipeline and Redirection Commands
Command | Description |
---|---|
` | ` |
> | 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