Skip to content

Instantly share code, notes, and snippets.

@HariSekhon
Created June 7, 2025 21:23
Show Gist options
  • Save HariSekhon/0f88fc999572133b9c8a26a0b8b67f39 to your computer and use it in GitHub Desktop.
Save HariSekhon/0f88fc999572133b9c8a26a0b8b67f39 to your computer and use it in GitHub Desktop.
shell-one-liners.md from HariSekhon/Knowledge-Base repo: https://github.com/HariSekhon/Knowledge-Base

Shell One Liners

Because who doesn't love shell tricks.

For far more serious tricks see the DevOps-Bash-tools repo.

System & Shell

Show CPU Cores

nproc

Show your $PATH entries one per line

echo $PATH | tr ':' '\n'

Log all Commands in a Shell Session

script logfile.txt

Create a Ram Disk

mkdir /tmp/ramdisk && mount -t tmpfs -o size=512m tmpfs /tmp/ramdisk

Dates

Get Current Epoch

Seconds since Unix birth 1st Jan 1970:

date '+%s'

Convert Epoch Seconds to Human Readable Date

date -d @1741942727

Files & Strings

Generate ASCII Art

brew install figlet
figlet "Hari Sekhon"
  _   _            _   ____       _    _
 | | | | __ _ _ __(_) / ___|  ___| | _| |__   ___  _ __
 | |_| |/ _` | '__| | \___ \ / _ \ |/ / '_ \ / _ \| '_ \
 |  _  | (_| | |  | |  ___) |  __/   <| | | | (_) | | | |
 |_| |_|\__,_|_|  |_| |____/ \___|_|\_\_| |_|\___/|_| |_|

Reverse a String

echo "testing" | rev

Reverse the lines of a file

More portable than tac:

tail -r file.txt

Shuffle Lines of a File

shuf file.txt

Split Big File into 10MB chunks

split -b 10M bigfile split_

Show Files Open by a Process

lsof -p "$(pidof bash)"

Generate a Random Password

brew install pwgen

Generates a bunch of passwords to choose from

pwgen

Or generate a single one of length 20 chars and avoid any ambiguous chars:

pwgen -1 -s -B 20

Base64 Secrets to avoid funny characters

Useful in things like GitHub Actions CI/CD.

base64

use --decode switch instead of -d for better portability between your Mac and Linux:

base64 --decode

Network

Listen Open TCP/UDP Ports

netstat -lntpu

Check if a Port is Open

nc -zv localhost 22
localhost [127.0.0.1] 22 (ssh) open

Get your Public IP Address

curl ifconfig.co

Quickly Serve Local Files over HTTP

python3 -m http.server 8000

Top for Network Processes

:octocat: raboof/nethogs

brew install nethogs
sudo nethogs

Disk

Find Biggest Files Taking Up Space

This searches only the current directory and only on the current partition, not sub-mount-points.

du -max . | sort -k1n | tail -n 1000

See also for a GUI alternative:

Mac

Use GNU CoreUtils

For cross-platform portability of your commands and scripts since GNU CoreUtils have better features than their BSD counterparts, and less bugs in advanced usage in my experience.

brew install coreutils

This will install GNU Core Utils prefixed with g to not clash with the native BSD counterparts.

Mask BSD Core Utils with GNU Core Utils

You can wrap them in functions (I do this in scripts and libraries eg. DevOps-Bash-tools:

grep(){
  command ggrep "$@"
}

sed(){
  command sed "$@"
}

Now when the script calls grep it gets the better functional version for cross-platform compatibility between Mac and Linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment