A Linux Cheat-sheet So Good, You'll Want to Save Itby@levorak
886 reads
886 reads

A Linux Cheat-sheet So Good, You'll Want to Save It

by CarolinaJanuary 18th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

List of Linux tricks that you might know
featured image - A Linux Cheat-sheet So Good, You'll Want to Save It
Carolina HackerNoon profile picture

Over the years I have faced several linux challenges that I have solved by googling or thanks to beautiful people who have shared their knowledge.

This is a brief compilation of useful tricks grouped in different categories: Files management, searching, bash scripting, networking, compressed files management, packet management, server information and miscellaneous.

📝📁Files management


  • Delete first line:
    sed -i '1d' file.txt
  • Delete all empty lines:
    sed '/^$/d' file.txt
  • Delete first two characters from each line:
    cat file.txt | sed 's/^..//'
  • Delete comments and empty lines:
    sed '/ *#/d; /^$/d' file.txt
  • Delete carriage return \r:
    cat input.txt|tr -d "\r" > output.txt


  • Replace string1 with string2 sed -d 's/string1/string2/g' file.txt
  • Replace space by - :
    sed ':a;N;$!ba;s/\n/\'\'\-\'/g''
  • Replace replace line break with space:
    sed ':a;N;$!ba;s/\n/ /g'


  • Add - at the beginning of the line
    sed -d s/^/\-/ file.txt 
  • Add - at the end of the line
    sed -d s/$/\-/ file.txt


  • Filter lines starting with a number:
    cat file.txt |grep ^[0-9]
  • Filter multiple words:
    grep -E 'string1|string2' file.txt
  • Filter multiple words old shells:
    grep -e 'string1' -e 'string2' file.txt
  • Print the third and tenth words from file:
    cat file.txt | awk '{print $3,$10}'
  • Print the third word from file separated by - :
     cat file.txt| awk -F"-" '{print $3}'


  • Split a large file every 2000 lines:
    split -l 2000 input.txt output.txt
  • View only the lines that contain string1:
    sed -n '/string1/p' files.txt 
  • Convert lowercase to uppercase:
    cat file.txt | tr '[:lower:]' '[:upper:] '
  • Copy permissions from a reference file:
    chmod --reference=reference_file new_file
  • Copy ownership from a reference file:
    chown --reference=reference_file new_file

🧮Let's count

  • Files on the directory:
    ls | wc -l
  • Lines in a file:
    wc -l file.txt
  • Words in a file:
    wc -s file.txt
  • Length of the longest line in a file:
    wc -L file.txt
  • Number of files and directories in a directory:
    tree -a 


  • Files less than two days old
    find -mtime -2
  • Files older than 5 days:
    find -mtime +5
  • Files created today:
    find ./ -type f -daystart -ctime -1
  • Directory by name: 
    find / -type d -name 
  • Find files by content:
    find ./ -type f -exec grep -H '
    ' {} \;

👨‍💻👩‍💻Bash scripting basics

For syntax

for i in `seq 1 90` do
echo $i

Read file line per line

while IFS='' read -r line || [[ -n "$line" ]]; do
echo "Text read from file: $line"
done < "$1"

Execution: ./ file_to_read.txt

Read file word per word

while IFS='' read -r line || [[ -n "$line" ]]; do
for word in $line     
echo $word
done < "$1"
Execution: ./ file_to_read.txt

Check if a file exists

if [ -f $path/to/file.txt ]; then
echo "exist"
echo "no exist"


Check ip reachability

ping -w1 -c3
 >/dev/null 2>&1 && echo "ON" || echo "OFF"


  • Ping from a defined interface:
    ping -I
    interface_name ip
  • Rename interface:
    link set
  • Show IP addresses without infconfig :
    ip address show


  • Traffic capture with tshark:
    tshark -i 
    .151 -f "host
    and port
  • Traffic capture with tcpdump:
    tcpdump -i
    dst port port
    -w capture.pcap
  • Store traffic output:
    tcpdump -ni eth0 -w capture.pcap
  • Read traffic capture form the CLI:
    tcpdump -ttttnnr capture.pcap

Port scanning 👀

  • Check if 5050 port is open:
    nc -zvn
    ip 5050
  • Check if 5050 and 5055 are open:
    nc -zvn 
    ip 5050 5055
  • Check if range of ports:
    nc -zvn 
    ip 5050-5055
  • Connect to ip on port 5050 and send content of
    ip 5050 

Listening 👂👂

  • Listen for TCP connections on port 5050:
    netcat -l 5050
  • Listen for UDP connections on port 5050:
    netcat -ul 5050
  • Listen for TCP connections on port 5050 and store data received in to "received.txt":
    netcat -l 5050 > received.txt
  • Listen for TCP connections on port 5050 and continue listening after client has disconnected:
    netcat -k -l 5050

🗃️Compressed files management

.tar.gz files

tar -czvf compressed.tar.gz /direcory/to/compress/

tar -xzvf compressed.tar.gz

.tar files

tar -cvf compressed.tar /direcory/to/compress/

tar -xvf compressed.tar

.gz files

gzip file.txt

 gzip -d file.txt.gz

.zip files

zip /direcory/to/compress/


Bonus 🎉
Read compressed file without decompress:

zcat compressed.gz

Filter compressed file without decompress:
zgrep -n 

Split compressed file in parts of 10m size:
split -b 10m file.tar.gz "file.tar.gz.part"

📦Packet management (YUM and DNF)

  • What provides a package
    yum whatprovides package_name
    dnf provides package_name
  • Package description:
    yum info package_name
    dnf info package_name
  • Update security packages:

    yum update --security
    dnf update --security
  • List dependencies from a package example httpd:
    yum deplist
    dnf repoquery --deplist httpd
  • Download all packages and dependencies needed to install something, example FTP:
    yumdownloader --resolve FTP
    dnfdownload --resolve FTP
  • Remove orphan packages:
    package-cleanup --orphans
    package-cleanup --oldkernels
    dnf list extras
    dnf remove $( dnf repoquery --installonly --latest-limit -1 -q )
  • List pending security updates:
    yum list-security
    dnf updateinfo list --security

ℹ️Server info

  • Number of processors:
    grep processor /proc/cpuinfo | wc -l
  • Number of cores:
    dmidecode -t processor | grep "Core Count"
  • Number of enabled cores:
    dmidecode -t processor | grep "Core Enabled"
  • RAM memory:
    free -m | head -2 | tail -1 | awk {'print $4'}
  • Free space:
    df -h | awk {'print $3'} | head -3 | tail -1
  • Release version:
    cat /etc/*release*


  • Execute a command as another user:
    sudo -u
    user command
  • Clean cache:
    echo 3 > /proc/sys/vm/drop_caches
  • Create symbolic link:
    ln -s
    /file_or_directory_path /direct_access_path
  • Create FIFO files:
  • Know more information about and image from CLI:
    identify -verbose
  • Pretty print json files in CLI:
    cat file.json | jq
  • Print only values of the key my_key from json file:
     jq '.[] | .my_key' file.json

Hope this was useful for you. If you want more don't hesitate visit my github