Skip to main content

Essential Linux Commands: A Growing Cheat Sheet for Everyday Use

·977 words·5 mins
Linux Bash CLI Command Line Terminal Unix Debian
Author
Emre Hayta
Table of Contents

Introduction
#

Linux offers a multitude of powerful commands that make working on the command line efficient and effective. Whether you are managing files, monitoring system resources, or networking, knowing the right commands can save you a lot of time. This article introduces essential Linux commands and will be regularly updated with new commands and useful tips.


File and Directory Management
#

ls
#

ls -lahList files and folder
ls file*List only entries beginning with file
ls *.txtList only entries ending with .txt
-lLong format: permissions, owner, group, size
-aInclude all files: hidden files
-hHuman-readable: KB, MB, or GB format

cd
#

cd /path/to/folderChange to the specified directory
cd ..Go one directory up (parent directory)
cd -Switch to the previous directory
cd ~Switch to the home directory

cp
#

cp file destinationCopy a file to the destination
cp -r folder destinationRecursively copy a folder
cp -i source destinationPrompt before overwriting
cp -u source destinationCopy only if source is newer than destination

mv
#

mv source destinationMove or rename a file/folder
mv -i source destinationPrompt before overwriting
mv -u source destinationMove only if source is newer than destination

File and Text Manipulation
#

cat
#

cat fileDisplay the contents of a file
cat file1 file2Concatenate and display multiple files
cat > fileCreate a new file and write to it
cat >> fileAppend to an existing file

grep
#

grep 'pattern' fileSearch for a pattern in a file
grep -i 'pattern' fileCase-insensitive search
grep -r 'pattern' folderRecursively search in a folder
grep -v 'pattern' fileDisplay lines not matching the pattern

head#

head fileDisplay the first 10 lines of a file
head -n 20 fileDisplay the first 20 lines of a file
head -c 50 fileDisplay the first 50 bytes of a file
head -v fileAlways show file name before output

tail
#

tail fileDisplay the last 10 lines of a file
tail -n 20 fileDisplay the last 20 lines of a file
tail -f fileFollow and display appended data in real-time
tail -c 50 fileDisplay the last 50 bytes of a file

Advanced Text Manipulation
#

awk
#

awk '{print $1}' filePrint the first column of a file
awk -F, '{print $2}' fileUse a comma as the field separator and print the second column
awk '/pattern/' filePrint lines that match a specific pattern
awk 'NR==1 {print $0}' filePrint only the first line of the file

sed
#

sed 's/old/new/g' fileReplace all occurrences of “old” with “new” in a file
sed -n '1,5p' filePrint only lines 1 to 5 of a file
sed '/pattern/d' fileDelete lines that match a specific pattern
sed -i 's/old/new/g' fileIn-place replace “old” with “new” in the file

System Information and Process Management
#

top
#

topDisplay real-time system resource usage
top -u userShow processes for a specific user
top -p PIDMonitor a specific process by PID
top -n 1Display output once and exit

htop
#

htopInteractive process viewer with more details
htop -u userShow processes for a specific user
htop -p PIDMonitor a specific process by PID
htop --treeDisplay processes in a tree view

df
#

dfDisplay disk space usage for all mounted filesystems
df -hShow disk space usage in human-readable format (e.g., MB, GB)
df -TDisplay filesystem type
df -iShow inode usage instead of block usage

Networking
#

ping
#

ping hostname_or_ipSend ICMP echo requests to test network connectivity
ping -c 4 hostname_or_ipSend 4 ICMP echo requests and stop
ping -i 2 hostname_or_ipSet the interval between sending packets to 2 seconds
ping -t hostname_or_ipPing continuously (until stopped manually)

ifconfig
#

Install: sudo apt install net-tools
ifconfigDisplay network interfaces and their configurations
ifconfig eth0Show details for a specific interface (e.g., eth0)
ifconfig eth0 upEnable the specified interface
ifconfig eth0 downDisable the specified interface

curl
#

Install: sudo apt install curl
curl urlFetch the content of the specified URL
curl -O urlDownload a file from the URL
curl -I urlFetch only the headers of the specified URL
curl -d "data" urlSend POST data to the specified URL

netstat
#

netstatDisplay network connections, routing tables, interface statistics
netstat -tulnShow listening TCP and UDP ports
netstat -iDisplay network interface statistics
netstat -rDisplay the kernel routing table

nc (netcat)
#

nc -l 12345Listen on port 12345 for incoming connections
nc -v hostname_or_ip 12345Connect to a specified host and port
nc -zv hostname_or_ip 80Check if a specific port is open (e.g., port 80)
nc -w 5 hostname_or_ip 12345Set a timeout of 5 seconds for the connection

telnet
#

Install: sudo apt install telnet
telnet hostname_or_ip portOpen a connection to a specific host and port
telnet localhost 25Test connection to a local mail server (port 25)
telnet hostname_or_ipOpen a connection to a specific host (default port 23)
telnet ?Display available telnet commands

dig
#

Install: sudo apt install dnsutils
dig domainPerform a DNS lookup for the specified domain
dig +short domainDisplay a simplified output (just the IP)
dig @dns_server domainPerform a DNS lookup using a specific DNS server
dig -x ip_addressPerform a reverse DNS lookup for the IP address

nslookup
#

nslookup domainQuery DNS information for the specified domain
nslookupEnter interactive mode to perform multiple queries
nslookup domain dns_serverQuery the domain using a specific DNS server
nslookup -type=any domainQuery for all available DNS record types

Updates
#

  • September 27, 2024: Added section on networking commands (ping, curl).
  • October 15, 2024: Added new examples for grep and awk.
  • October 18, 2024: Updated structure of blog article