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

-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

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

0 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

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

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

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

0 score

While loop to pretty print system load (1, 5 & 15 minutes)

 $ while :; do date; awk '{printf "1 minute load: %.2f\n", $1; printf "5 minute load: %.2f\n", $2; printf "15 minute load: %.2f\n", $3}' /proc/loadavg; sleep 3; done

Sept. 5, 2018, 8:41 p.m.Janos

0 score

Scan entire Git repos for dangerous Amazon Web Service IDs

 $ git ls-tree --full-tree -r --name-only HEAD | xargs egrep -w '[A-Z0-9]{20}'

Aug. 31, 2018, 10:29 p.m.johntellsall