Ask HN: What are your favorite one-liner shell commands you use?

8 rajkumarsekar 7 7/17/2025, 4:45:21 AM
There are plenty of lists online with fancy or obscure one-liners, but I’m curious about the ones people actually use day to day.

Comments (7)

geocrasher · 1h ago
Daily, and with enough general usability to share? I have a few. They are basic. You can probably figure out what industry I work in.

    df -h /; echo "----"; for fattable in $(find /var/lib/mysql/ -name *.ibd -size +1G -exec ls -lh {} \; | awk '{ print $9 }' );do echo BEFORE " " $(ls -lh $fattable| awk '{ print $5" " }'); db=$(echo $fattable| cut -d/ -f5); otable=$(echo $fattable| cut -d/ -f6| cut -d. -f1); echo mysql -qbse \"use $db \; optimize table  $otable\;\"|bash;echo AFTER" "" " $(ls -lh $fattable| awk '{ print $5" " }');echo "----" ;done; echo "----"; df -h /

    apachectl fullstatus | grep ^[0-9]| awk '{ print $12" "$14 }' | sort | uniq -c| sort -n |tail

    
    tail -100000 /var/log/nginx/access.log | sed "/$(hostname -i)/d" | awk '{ print $1" "$7 }' | sort | uniq -c | sort -n | tail -050


    find . -mtime -1 | cut -d/ -f2 |uniq

  
    pkill lsphp; sleep 2; while true; do sleep .4; strace -p $(ps aux | grep [i]ndex| awk '{ print $2 }' | head -01); done


    for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file 2>/dev/null; done| awk '{ print $2" "$3" "$1 }'  | sort -n | tail -20


    find . -size +200M -exec ls -lh {} \; | awk '{ print $5" "$9 }' 


    awk '{ print $4 }' 

https://explainshell.com/
jauco · 1h ago
I rarely use the exact samme command multiple times. But here are some bash pipe segments I enjoy.

using pv (pipeviewer) instead of cat to get a progress bar when grepping huge files.

Using httpie instead of curl so I can remember the flags.

The power of find -exec to run commands on a lot of specific files. The nice part is you can run it without exec first to see if you have the right set of files.

If you do a loop you can echo -e “$somevar\r” and then each write will overwrite the previous line so you screen doesn’t fill up but you do get a feel for the progress (to make it nice you need to pad with spaces, google for echo carriage return to learn more)

TheNewAndy · 1h ago
Not really a shell one liner, but ctrl+r (a readline command to do an incremental search backwards through history) is something that has been present on every shell I've used for decades without realising it, One day I decided to take the time to read all the magic readline commands because I wanted a way to quickly edit the N-th argument of a command with lots of arguments, and there were way too many of them. There were so many commands that I had no hope of remembering them all, but I figured I could just remember a few useful ones - and ctrl+r was one of them (ctrl+w and alt+b were the other two)

More to the letter of the question, I use "cd -" frequently, "ps -e | grep some_process_i_would_like_the_pid_for", and while I don't use it frequently, I didn't know about "ssh-copy-id" for a long time, and would do it manually with a text editor in the past. Sorry if they are not sufficiently fancy - but for things to get used day to day for me, they will need to be short and sweet.

vinhnx · 1h ago
I also same here. Has since I discovered Ctrl+R, and equipped it with fzf (https://github.com/junegunn/fzf), every terminal command is in my hand, I can fuzzy search and not need to remember the exact command. This really saved me a lot of times.
xiconfjs · 1h ago
:(){ :|:& };:
yjftsjthsd-h · 14m ago
Do you, ah, find yourself using that a lot?:)
willprice89 · 1h ago
dd if=/dev/zero bs=1M count=10 | nc -N -l -p 12345

Great simple test for network speed on a box without speedtest-cli or other tools installed.