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

Re-compress a gzip (.gz) file to a bzip2 (.bz2) file

 $ time gzip -cd file1.tar.gz 2>~/logfile.txt | pv -t -r -b -W -i 5 -B 8M | bzip2 > file1.tar.bz2 2>>~/logfile .txt

Feb. 1, 2012, 6:02 p.m.DAVEB

1 score

Test your hard drive speed

 $ time (dd if=/dev/zero of=zerofile bs=1M count=500;sync);rm zerofile

Feb. 1, 2012, 5:35 p.m.DAVEB

1 score

Recursively remove all empty sub-directories from a directory tree

 $ find . -depth  -type d  -empty -exec rmdir {} \;

Jan. 31, 2012, 11:15 p.m.openiduser16

1 score

Group count sort a log file

 $ A=$(FILE=/var/log/myfile.log; cat $FILE | perl -p -e 's/.*,([A-Z]+)[\:\+].*/$1/g' | sort -u | while read LINE; do grep "$LINE" $FILE | wc -l | perl -p -e 's/[^0-9]+//g'; echo -e "\t$LINE"; done;);echo "$A"|sort -nr

Jan. 31, 2012, 6:49 p.m.openiduser14

1 score

Use ghostscript to shrink PDF files

 $ gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Jan. 31, 2012, 10:43 a.m.openiduser10

1 score

How to find all hard links to a file

 $ find /home -xdev -samefile file1

Jan. 30, 2012, 8:56 p.m.openiduser7

1 score

Find all the unique 4-letter words in a text

 $ cat ipsum.txt | perl -ne 'print map("$_\n", m/\w+/g);' | tr A-Z a-z | sort | uniq | awk 'length($1) == 4 {print}'

Jan. 29, 2012, 10:28 p.m.Janos

1 score

Concatenate two or more movie files into one using mencoder

 $ mencoder cd1.avi cd2.avi -o movie.avi -ovc copy -oac copy

Dec. 24, 2011, 3:51 p.m.Janos

1 score

Calculate the average execution time (of short running scripts) with awk

 $ for i in {1..10}; do time some_script.sh; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'

Dec. 21, 2011, 8:50 a.m.Janos

1 score

A convenient way to re-run the previous command with sudo

 $ sudo !!

Dec. 14, 2011, 11:26 p.m.Janos

1 score

Rotate a movie file with mencoder

 $ mencoder video.avi -o rotated-right.avi -oac copy -ovc lavc -vf rotate=1

Dec. 2, 2011, 10:30 p.m.Janos

1 score

View specific column of data from a large file with long lines

 $ cat /tmp/log.data |colrm 1 155|colrm 60 300

Oct. 4, 2011, 9:55 p.m.versorge

1 score

Replace a regexp pattern in many files at once

 $ vi +'bufdo %s/pattern/replacement/g | update' +q $(grep -rl pattern /path/to/dir)

Sept. 15, 2011, 11:50 p.m.Janos

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

Generate a sequence of numbers

 $ perl -e 'print "$_\n" for (1..10);'

May 30, 2017, 2:47 p.m.abhinickz6

0 score

Find all of the distinct file extensions in a folder

 $ find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u

May 17, 2013, 8:05 p.m.kowalcj0

-1 score

Generate a sequence of numbers

 $ seq 1 10

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

-1 score

Delete all untagged Docker images

 $ docker rmi $(docker images -f "dangling=true" -q)

April 27, 2018, 2:50 p.m.stefanobaghino

-1 score

Get a free shell account on a community server

 $ sh <(curl hashbang.sh | gpg)

March 15, 2015, 9:49 a.m.lrvick

-1 score

Run a local shell script on a remote server without copying it there

 $ ssh user@server bash < /path/to/local/script.sh

June 21, 2012, 12:06 a.m.Janos

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

0 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

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

(Pseudo)random 6-digit number

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

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