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.
0 score

Generate a sequence of numbers

 $ seq 1 10

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

0 score

Recursively compute factorial of positive integer using only built-ins

 $ factorial() {  [ "$1" = "0" ] && echo ${2:-1} || factorial $(( $1 - 1 )) $(( ${2:-1} * $1 )); }

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

0 score

Compute factorial of positive integer using only built-ins

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

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

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

1 score

Generate a sequence of numbers

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

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

0 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

1 score

Using tcpdump with port ranges and file count/size

 $ sudo /usr/sbin/tcpdump -i any -s 0 -n -Z <user_name> -C 500 -W 100 -w /home/<user_name>/$(hostname).pcap -f '(port (# or # or # or # or # or # or ...) or portrange <start>-<end>)' &>/dev/null

March 15, 2019, 1:02 a.m.peek2much3

1 score

Compare (diff) vim-generated backup of a file with that file's current version.

 $ diff~() { diff "$1"~ "$1"; }

March 12, 2019, 2:52 a.m.juanolopezgerena

1 score

Count the total number of hours of your music collection

 $ find . -print0 | xargs -0 -P 40 -n 1 sh -c 'ffmpeg -i "$1" 2>&1 | grep "Duration:" | cut -d " " -f 4 | sed "s/.$//" | tr "." ":"' - | awk -F ':' '{ sum1+=$1; sum2+=$2; sum3+=$3; sum4+=$4; if (sum4 > 100) { sum3+=1; sum4=0 }; if (sum3 > 60) { sum2+=1; sum3=0 }; if (sum2 > 60) { sum1+=1; sum2=0 } if (NR % 100 == 0) { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 } } END { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 }'

March 1, 2019, 9:09 p.m.pingiun

0 score

Print the list of your Git commits this month

 $ git log --since='last month' --author="$(git config user.name)" --oneline

Feb. 25, 2019, 6:57 p.m.Janos

0 score

Store the output of find in an array

 $ mapfile -d $'\0' arr < <(find /path/to -print0)

Feb. 18, 2019, 7:48 p.m.Janos

1 score

Take values from a list (file) and search them on another file

 $ for ITEM in $(cat values_to_search.txt); do  (egrep $ITEM full_values_list.txt && echo $ITEM found) | grep "found" >> exit_FOUND.txt; done

May 16, 2018, 3:20 p.m.ManuViorel

1 score

Clear the Exim Queue (remove all messages)

 $ exim -bp | exiqgrep -i | xargs exim -Mrm

Feb. 11, 2019, 8:58 a.m.Robert

0 score

Check if a text snippet is valid C code

 $ gcc -fsyntax-only -xc - <<< "text snippet"

Feb. 10, 2019, 8:12 a.m.Janos

1 score

Inspect the HTTP headers of a website

 $ curl -I amazon.com

Feb. 8, 2019, 10:56 p.m.Janos

2 score

Send HTTP POST to a website with a file input field

 $ curl -L -v -F "value=@myfile" "http://domain.tld/whatever.php"

Feb. 15, 2012, 11:26 p.m.openiduser14

4 score

Find all log files modified 24 hours ago, and zip them

 $ find . -type f -mtime +1 -name "*.log" -exec zip -m {}.zip {} \; >/dev/null

Nov. 9, 2018, 10:04 a.m.TrongTan124