Welcome to bashoneliners.com, a growing collection of practical and well-explained Bash tips and tricks. We are committed to review every contributed one-liner to ensure its high quality, practical usefulness, and a solid explanation of how it works. We want to document one-liners for frequent (non-trivial) tasks executed in the shell, written in the best possible way, following good practices known in the industry. To get the latest Bash one-liners, follow @bashoneliners on Twitter. If you find any problems, please report on GitHub Issues.
1 score

Get a wide info of all pods on all namespaces with Kubectl

 $ kubectl get pods -A -o wide

Sept. 15, 2022, 6:42 p.m.adawolfs

0 score

Oneliner checksum validation

 $ sha512sum filename.iso | grep -oE ".*\ " | (read checksum; [ "$Checksum" ==  checksumCheck ] && printf "\033[0;32mValid" || printf "\033[0;31mInvalid")

Aug. 24, 2022, 10:43 a.m.Hussain-Aqeel

0 score

Move file and cd with one command

 $ mv file dir && cd "$_"  && pwd

April 16, 2022, 9:55 a.m.Bryan-netizen

1 score

Report disk usage by file type

 $ find . -type f -empty -prune -o -type f -printf "%s\t" -exec file --brief --mime-type '{}' \; | awk 'BEGIN {printf("%12s\t%12s\n","bytes","type")} {type=$2; a[type]+=$1} END {for (i in a) printf("%12u\t%12s\n", a[i], i)|"sort -nr"}'

Dec. 28, 2020, 10:09 p.m.joeashcraft

0 score

Find with invert match - e.g. find every file that is not mp3

 $ find . -name '*' -type f -not -path '*.mp3'

Nov. 23, 2020, 10:21 p.m.sandreas

-1 score

Convert pip list --outdated for reuse in pip install -U

 $ pip install -U $(pip list --outdated 2> /dev/null | grep -v 'Version' | grep -v '------' | awk '{printf $1 " " }' && echo)

April 16, 2020, 1:04 p.m.docdyhr

0 score

Convert pip list --outdated for reuse in pip install -U

 $ python3 -m pip install -U $(python3 -m pip list outdated 2> /dev/null | grep -v 'Version' | grep -v '\-\-\-\-\-\-' | awk '{printf $1 " " }' && echo)

April 16, 2020, 10:55 a.m.docdyhr

0 score

Recursively remove all "node_modules" folders

 $ find . -name "node_modules" -exec rm -rf '{}' +

March 29, 2020, 8:50 a.m.tg-z

0 score

Read other user's shell history, and convert epoch to human-readable date

 $ cat /home/john/.bash_history | awk '/#[0-9]*$/ {split($0, arr, "#"); print "#", strftime("%c",arr[2]);getline; print; }'

March 10, 2020, 6 p.m.joeashcraft

1 score

Outputs list of $PATH dirs sorted by line length

 $ echo -e ${PATH//:/\\n} | awk '{print length, $0}' | sort -n | cut -f2- -d' '

March 7, 2020, 7:39 a.m.tg-z

0 score

Generate a sequence of numbers

 $ seq 1 10

Jan. 17, 2020, 10:09 p.m.penkoad

0 score

Prints "Good" in green if the sha256 sum of a file matches the sum listed in a dist/checksum file and "Bad" in red if it does not match

 $ [[ $(sha256sum _downloaded_file_ | cut -d' ' -f 1) == $(grep -Po '\b[a-zA-Z0-9]{64}\b' _checksum_fle_) ]] && printf "\033[0;32mGood" || printf "\033[0;31mBad"

Dec. 13, 2019, 8:10 p.m.ratfink417

0 score

Listen to a song from youtube

 $ listen-to-yt() { if [[ -z "$1" ]]; then echo "Enter a search string!"; else mpv "$(youtube-dl --default-search 'ytsearch1:' \"$1\" --get-url | tail -1)"; fi }

Dec. 8, 2019, 6:46 p.m.MKamnikar

0 score

(Pseudo)random 6-digit number

 $ let N=0 N=N+10**{0..5}*${RANDOM:0:1}

Dec. 7, 2019, 3:26 p.m.CK

0 score

Compute factorial of positive integer using only built-ins

 $ bang() ( eval let N=1 N*={1..$1} ; echo $N; )

Nov. 29, 2019, 9:06 p.m.CK

2 score

Generate a sequence of numbers

 $ printf '%s\n' {1..10}

Nov. 25, 2019, 8:11 a.m.CK

0 score

Recursively compute factorial of positive integer using only built-ins

 $ bang() ( IFS=\*; let N=$1-1 k="$*" && bang $N $k || echo ${2-1} )

Nov. 25, 2019, 8:06 a.m.CK

2 score

Using a single sudo to run multiple && arguments

 $ sudo -s <<< 'apt update -y && apt upgrade -y'

Oct. 27, 2019, 7:02 p.m.secure-debian

1 score

Generate a sequence of numbers

 $ for i in {1..10};do echo $i;done

Oct. 25, 2019, 12:48 a.m.diego

0 score

List docker log sizes and remind how to empty them

 $ docker ps -qa | xargs docker inspect --format='{{.LogPath}}' | xargs sudo du -hl; sleep 1; printf "\r\n  echo '' > \$(docker inspect --format={{.LogPath}} container_name_or_id) \r\n*****COPY****ABOVE****TO******CLEAR*****LOG*****CHANGE***CONTAINER_ID******* \r\n \n"

June 18, 2019, 8:06 p.m.lockntross

2 score

Find files/dirs modified within a given period

 $ find . -type d -newermt "2019-01-01" \! -newermt "2019-02-01" -exec ls -ld {} \;

May 20, 2019, 6:48 p.m.fenchu

1 score

Scan all open ports without any required program

 $ for i in {1..65535}; do (echo < /dev/tcp/127.0.0.1/$i) &>/dev/null && printf "\n[+] Open Port at\n: \t%d\n" "$i" || printf "."; done

May 8, 2019, 4:56 p.m.Goeks1

0 score

Extract audio only from video files using ffmpeg

 $ ffmpeg -i video.any -vn -acodec libvorbis audio.ogg

April 21, 2019, 10:55 p.m.Janos

2 score

Generate a random 32 characters password

 $ tr -dc 'a-zA-Z0-9~!@#$%^&*_()+}{?></";.,[]=-' < /dev/urandom | fold -w 32 | head -n 1

March 24, 2019, 6:39 p.m.cheuv25