Last active
January 1, 2024 11:08
-
-
Save wllmsash/a7d272b4b79475cf920d1e996e7fa99a to your computer and use it in GitHub Desktop.
Useful commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Show free and used disk space | |
# -a: Include all non-standard file systems | |
# -h: Human readable sizes | |
df -ah | |
# Show disk usage of a file or directory (recursive) | |
# -h: Human readable sizes | |
# -s: Summarize results | |
du -hs /path/to/directory | |
# Show disk usage of a file or directory and its subdirectories (recursive) | |
# -h: Human readable sizes | |
# -d: Max depth N, d1 = max depth 1 | |
# -s: Summarize results | |
du -hd1s /path/to/directory | |
# Delete everything in a directory but not the directory itself | |
find /path/to/directory -mindepth 1 -delete | |
# Calculate a SHA256 checksum of the current directory contents (recursive) | |
# Ref: https://unix.stackexchange.com/a/35847 | |
find | LC_ALL=C sort | tar -cf - --no-recursion -T - | sha256sum | |
# Run command in parallel | |
# Example runs `sh -c 'sleep 1 && echo {}'` 50 times in batches of 10 | |
seq 1 50 | xargs -I {} --max-procs 10 sh -c 'sleep 1 && echo {}' | |
# Port forwarding | |
ssh -L <local_port>:localhost:<remote_port> [<user>@]<host> | |
# Port forwarding with Google Cloud SDK | |
# Example: gcloud compute ssh example -- -L 0.0.0.0:8080:localhost:80 | |
gcloud compute ssh [<user>@]<instance> -- -L <local_bind_address>:<local_port>:<remote_host>:<remote_port> | |
# Port forwarding with Google Cloud SDK on Windows (the -- needs escaping with `) | |
# Example: gcloud compute ssh example `-- -L 0.0.0.0:8080:localhost:80 | |
gcloud compute ssh [<user>@]<instance> `-- -L <local_bind_address>:<local_port>:<remote_host>:<remote_port> | |
# Count open files for a process | |
lsof -p $(pidof <process_name>) | wc -l | |
# Force line buffering in pipelines | |
# Ref: https://www.gnu.org/software/libc/manual/html_node/Buffering-Concepts.html | |
# Ref: https://mywiki.wooledge.org/BashFAQ/009 | |
# Ref: https://unix.stackexchange.com/questions/97294/unbuffer-or-stdbuf-for-removing-stdout-buffering | |
{ echo "a"; sleep 10; } | grep a | grep a # The first grep is not using line buffering for stdout as it's not writing to a tty. The second grep is. | |
{ echo "a"; sleep 10; } | stdbuf -oL grep a | grep a # Force the first grep to use line buffering for stdout; or | |
{ echo "a"; sleep 10; } | unbuffer -p grep a | grep a # Use the unbuffer command to create a psuedo-tty, enabling line buffering for stdout. -p tells unbuffer to read from stdin. | |
# Infinite loop with randomized output | |
while true; do sleep 1; if test $(cat /dev/urandom | od -An -i -N1 | tr -d ' ') -ge 128; then echo '{ "some": { "json": "object" } }'; else echo 'some_string'; fi; done; | |
# Print 1024 lines of random bytes | |
n=$(printf '%d\n' '1024'); for i in $(seq 1 $n); do printf "%0$(printf $n | wc -c)d: %s\n" "$i" "$(cat /dev/urandom | xxd -p | head -c 32)"; done; | |
# Parallel processing example | |
seq 1 128 | parallel '{ test $(expr {} % 2) -eq 0 || printf "%-3d is odd\n" "{}"; }; { test $(expr {} % 2) -eq 0 && printf "%-3d is even\n" "{}"; };' | |
# Parallel processing example | |
# Run 2 background jobs and wait for them to finish | |
# -j0 runs infinite jobs so the sleeping jobs don't bottleneck parallel on the number of cores | |
seq 1 128 | parallel -j0 '{ test $(expr {} % 2) -eq 0 || printf "%-3d is odd\n" "{}"; } & { test $(expr {} % 2) -eq 0 && sleep 1 && printf "%-3d is even\n" "{}"; } & wait;' | |
# Validate JSON | |
jq --raw-input 'fromjson | empty' 2>/dev/null; test $? -eq 0; echo $?; | |
# Using file descriptors for redirections | |
# Ref: https://unix.stackexchange.com/questions/37660/order-of-redirections | |
# Named pipe to anonymous pipe | |
# Ref: https://superuser.com/questions/184307/bash-create-anonymous-fifo | |
# Fix Google Cloud SDK SSH | |
# -T to disable the forced pseudo TTY allocation through -t passed by default | |
gcloud compute ssh <instance> -- -T |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment