/one-liners

Useful bash one-liners

One-liners

Useful bash one-liners.

References:

Styles

New lines in Bash

https://unix.stackexchange.com/questions/253518/where-are-bash-line-continuations-after-and-documented

A newline is ignored in a few contexts where there is manifestly an unterminated command. These contexts include after a control operator (&&, ||, |, &, ;, ;;, but not !). I don't see this documented in the bash manual. In POSIX, it's specified via the grammar rules . Wherever the rules have linebreak, you can have zero or more line breaks.

Comments in a multi-line bash command

https://superuser.com/questions/238791/comments-in-a-multi-line-bash-command

Put the pipes at the end of line with the comments after it:

echo 'foo' |
    sed 's/f/a/' | # change first f to a 
    sed 's/o/b/' | # change first o to b 
    sed 's/o/c/'   # change second o to c abc
#abc

Basic bash tools

cat, sort, uniq, cut, etc.

[back to top]

Number each line in file.txt:

cat -n file.txt

Count the number of unique lines in file.txt

cat file.txt | sort -u | wc -l

Terminal appearances

使用 Xterm 控制序列改变终端大小

https://apple.stackexchange.com/a/47841

Set the window to 100x30 characters:

printf '\e[8;30;100t'

Move the window to the top/left corner

printf '\e[3;0;0t'

System Administration

找出占用空间最大的目录

find . -type d -print0 | xargs -0 du -s | sort -n | tail -20 | cut -f2 | xargs -I{} du -sh {}

Kill all by names

BIN_NAME=lastz ps ax | grep "${BIN_NAME}" | cuf -f 1 | xargs kill -9