Skip to content

Instantly share code, notes, and snippets.

@nd3w
Last active May 9, 2026 02:27
Show Gist options
  • Select an option

  • Save nd3w/dfaac200e8a0c1b12ac8fda889f841f2 to your computer and use it in GitHub Desktop.

Select an option

Save nd3w/dfaac200e8a0c1b12ac8fda889f841f2 to your computer and use it in GitHub Desktop.
A Collection of Useful Linux Commands
Backup with bunzip
tar cvfj file.tar.bz2 *
tar cvfj file.tar.bz2 /path/to/directory_to_backup/
Extract from bunzip to directory 'archive'
tar -xvf archive.tar.bz2 ./
Compress file to XZ file (Lossless Data Compression Tool) deleting the original file
xz file.sql
Compress file XZ file (Lossless Data Compression Tool) without deleting the original file
xz -k file.sql
xz -k -8 file.sql
xz -k --best file.sql
xz -k --best --memlimit-compress=10% file.sql
Extract XZ file (Lossless Data Compression Tool)
xz -d file.sql.xz
Archive multiple files with XZ
tar cJvf archive.tar.xz *.csv
Decompress archive with XZ
tar xvf archive.tar.xz
Compress multi directories into each own compressed files
for i in */; do zip -r "${i%/}.zip" "$i"; done
for i in */; do tar -czvf "${i%/}.tar.gz" "$i"; done
for i in */; do 7z a "${i%/}.7z" "$i"; done
Untar multiple tarballs using bash for loop
for f in *.tar.bz2; do tar -xvf "$f"; done
List file and its subdirectories, filename only
ls -1R > lsir.txt
List files sort by last modified date
ls -ltr
List file and its size separate by a tab
ls -ls | awk '{printf $10"\t"$6/1000000"\n"}' > list.txt
Find files after a certain date
find . -type f -newermt '2024-04-01 00:00:00'
Find a type of file in current directory recursively
find . -name '*.xml'
Switch between 2 directories using `cd -`. Each time you run "cd -" it will flip between the previous two directories you navigated to, making it very fast and easy to move back and forth.
cd ~
cd /tmp
cd -
View a file using `cat` with line numbers
cat -sb file.txt
Copy directories with unchange modification times, owners, and permissions
cp -a docs backup-docs
Copy a file into a directory and its subdirectories:
sudo find <target-dir> -type d -exec cp <the file> {} \;
Change permission files recursively:
sudo find /var/www/html/ -type f -exec chmod 644 {} +
Change permission directory and its subdirectories recursively:
sudo find /var/www/html/ -type d -exec chmod 775 {} +
Change ownership on directory with a lots of files
sudo find /path/to/dir -type f -exec chown www-data:www-data {} \;
Delete a certain type of files recursively:
sudo find ./ -name '*.php' -exec rm -rf {} \;
or
sudo find . -name "*.php" -delete
Delete a directory with a collection of files (could delete 12 millions of files):
sudo find /var/www/web_system/session -name "sess_*" -delete
Disc usage for top level dirs only:
sudo du -h -d1 /
Total disc usage on a dir:
sudo du -sh /root/test
Total disc usage on a dir in kilobytes (Kb):
sudo du -shk /root/test
Biggest 10 dirs in a given path:
du -a -h /path | sort -h -r | head -n 10
Find files containing string 'happy birth' with matching line
grep "happy cat" /home/tom/*.txt
Find files containing string 'happy birth' with file name only
grep -H "happy cat" /home/tom
Find files containing string 'happy birth' with file name only (case insensitive)
grep -H -i "happy cat" /home/tom
Find text in files in a directory recursively
grep -rH -i "Johnny" DirName/
Find text in files in a directory recursively, list filename only
grep -rHl -i "Johnny" DirName/
Find text in files in a directory recursively, supress error message
grep -rs "Johnny" .
Replace text in all PHP files in a directory:
sed -i "s|<script type='text/javascript'>|<script type='text/javascript' nonce=\"<?php echo \$nonce; ?>\">|g" *.php
Replace text in all PHP files in a directory:
Replace all occurrence of <br> to <br /> in files in a directory
find ./ -type f -exec sed -i 's/<br>/<br \/>/g' {} \;
Replace all occurrence of <br> to <br /> in HTML files recursively
find . -type f -name "*.html" -print0 | xargs -0 sed -i 's|<br>|<br />|g'
Find out a file's MIME type
file --mime-type image.png
file -b --mime-type image.png
file -i FILE_NAME
Empty a text file
sudo truncate -s 0 theFile
Remove duplicate line
sort file.txt > file-sorted.txt
uniq file-sorted.txt file-remove-duplicate.txt
Find unique lines in a file and sort them
sort file.txt | uniq | less
Find files less than 200 Kb recursively, omit -delete to test it. (G for giga; M for mega; k for kilo)
find /mnt/ADrive/APath/ -type f -size -200k -delete
Make a new directory and its subdirectories directly
mkdir -p ./new/dir/sub_dir
Rename - add prefix to all files (from 01.txt to PRE_01.txt)
rename 's/^/PRE_/' *
Rename files and replace dots with spaces (sadly with files' extensions too!)
rename 's/\./ /g' *
And rename back the extension
rename 's/ mp4/\.mp4/g' *
rename 's/ srt/\.srt/g' *
Rename files starts with 'note-', e.g.: note-worthy.txt, to 'nota-'
rename 's/note-/nota-/g' *
Rename all files to capitalize (e.g.: FILENAME FIRST.txt to Filename First.txt
rename 's/.*/lc($&)/e; s/(^| )./uc($&)/ge' *
Add extension html to all files in current directory
find . -type f -print0 | xargs -0 -I{} mv "{}" "{}".html
Remove files when 'rm -f' fails because there are too many files
find . -name "*.pdf" -print0 | xargs -0 rm
find . -name "*.pdf" -print0 | xargs -0 sudo rm
Count how many files in a directory recursively
find ./directory_name -type f | wc -l
Display first 100 lines of a text file
head -100 filename.log
Display last 100 lines of a text file
tail -100 filename.log
Compare 2 big text files
diff -U 0 a b
Compare 2 big text files and save it to a file
diff -U 0 a b >> filename.diff
Print directories names and their sub dirs:
tree -d
Print directories names and their sub dirs for two top levels:
tree -d -L 2
Convert a PDF's file first page (index 0) into an image format like PNG or JPG
convert input.pdf[0] cover.png
Print all chips temperature:
sensors
Print drive(s) temperature:
sudo hddtemp /dev/sda (for drive /dev/sda)
sudo hddtemp /dev/sdb (for drive /dev/sdb)
sudo hddtemp /dev/sd? (for all drive)
Global replace in Vim
:%s/search_string/replacement_string/g
Replace two carriage returns with </p><p>:
:%s/\n\n/<\/p><p>/g
Replace a carriage return with a space:
:%s/\n/ /g
Replace hex 91 and 92 characters in Vim
:%s/[\x91\x92]/'/g
Save file as another file in Vim
:w filename.ext
How to search previous commands in Vim
Press : and use up or down arrow
Search for non ASCII characters
/[^\x00-\x7F]
Select block and indent and undent in Vim
- Enter visual mode: type 'Shift+V'
- Move the cursor to select using 'j' or 'k' etc
- Use key '>' to indent or key '<' to unindent
Download list of files in a text file that gentle to server
wget --input-file=list.txt \
--wait=2 \
--random-wait \
--limit-rate=500k \
--tries=3 \
--timeout=30 \
--no-clobber \
--quiet \
--show-progress
Display all partitions/drives
sudo lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL
Make a bootable USB flash disc
sudo dd if=/home/ISO/YourDistro.ISO of=/dev/sdx
Where /dev/sdx is your pendrive. Make sure it is unmounted.
Disable/enable touchpad
View ID prop:
# xinput | grep Touchpad
Result:
ETPS/2 Elantech Touchpad id=14 [slave pointer (2)]
To disable:
# xinput disable 14
To enable:
# xinput enable 14
Send POST request using cURL
curl --location --request POST 'https://example.com/api/login.php' \
--header 'Authorization: Bearer Bearer zw4yofevyziuosxucoqb' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "foo",
"password": "secret"
}'
Restart XFCE4 panel
xfce4-panel -r
Convert multiple png image files to jpg
mogrify -format jpg *.png
Resize all files to certain dimension
mogrify -resize 960x528 *.png
Resize all files to certain width and preserved ratio for height
mogrify -resize 960 *.png
Resize all files to certain dimension but will preserved ratio
mogrify -resize 960x528! *.png
Compress PDF file
convert -density 150 -quality 60 -compress jpeg uncompressed.pdf compressed.pdf
Print directories and files consuming the most disk space
du -a -x {/dir1/} | sort -n -r | head -n 10
List a specific font family
fc-list | grep -i font_name
Daily Heroes:
• ps aux | grep {process} - Find that sneaky process
• lsof -i :{port} - Who's hogging that port?
• df -h - The classic "we're out of space" checker
• netstat -tulpn - Network connection detective
• kubectl get pods | grep -i error - K8s trouble finder
• kill -9 {pid} - Terminate stubborn processes
• du -sh * - Disk usage per directory
• whoami - Confirm your user identity
• uptime -s - System boot time
• watch -n 1 {command} - Run command repeatedly
Log Warriors:
• tail -f /var/log/* - Real-time log watcher
• journalctl -fu service-name - SystemD log stalker
• grep -r "error" . - The error hunter
• zcat access.log.gz | grep "500" - Compressed log ninja
• less +F - The better tail command
• cat /var/log/syslog | grep "fail" - Spot system failures
• awk '/error/ {print}' logfile - Extract error lines
• find /var/log -name "*.log" - Locate all log files
System Detectives:
• htop - System resource storyteller
• iostat -xz 1 - Disk performance poet
• free -h - Memory mystery solver
• vmstat 1 - System vital signs
• dmesg -T | tail - Kernel's recent gossip
Network Ninjas:
• curl -v - HTTP conversation debugger
• dig +short - Quick DNS lookup
• ss -tunlp - Socket statistics simplified
• iptables -L - Firewall rule reader
• traceroute - Path finder
File Jugglers:
• find . -name "*.yaml" -type f - YAML hunter
• rsync -avz - Better file copier
• tar -xvf - The unzipper (yes, we all google this)
• ln -s - Symlink wizard
• chmod +x - Make it executable
Performance Profilers:
• strace -p {pid} - System call spy
• tcpdump -i any - Network packet sniffer
• sar -n DEV 1 - Network stats watch
• uptime - Load average at a glance
• top -c - Classic process viewer
Quick Fixes:
• sudo !! - Run last command with sudo
• ctrl+r - Command history search
• history | grep - Command time machine
• alias - Command shortcut maker
• watch - Command repeater
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment