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

Shuffle lines

 $ ... | perl -MList::Util=shuffle -e 'print shuffle <>;'

Jan. 31, 2016, 9:02 p.m.openiduser81

1 score

Preserve your fingers from cd ..; cd ..; cd..; cd..;

 $ upup(){ DEEP=$1; [ -z "${DEEP}" ] && { DEEP=1; }; for i in $(seq 1 ${DEEP}); do cd ../; done; }

June 9, 2015, 3:09 p.m.andreaganduglia

1 score

Get number of all Python Behave scenarios (including all examples from Scenario Outlines)

 $ behave -d | grep "scenarios passed" | cut -d, -f4 | sed -e 's/^[[:space:]]*//' | sed 's/untested/scenarios/g'

April 17, 2015, 2:21 p.m.openiduser188

1 score

Download a file from a webserver with telnet

 $ (echo 'GET /'; echo; sleep 1; ) | telnet www.google.com 80

Dec. 22, 2014, 11:31 p.m.Janos

1 score

Print a flat list of dependencies of a Maven project

 $ mvn dependency:list | sed -ne s/..........// -e /patterntoexclude/d -e s/:compile//p -e s/:runtime//p | sort | uniq

Sept. 22, 2014, 9:02 p.m.Janos

1 score

Open Windows internet shortcut (*.url) files in firefox

 $ grep -i url='*' file.url | cut -b 5- | xargs firefox

Sept. 12, 2014, 12:06 a.m.tsjswimmer

1 score

Remove all at jobs

 $ atq | sed 's_\([0-9]\{1,8\}\).*_\1_g' | xargs atrm

Sept. 10, 2014, 9:56 a.m.laurip

1 score

Deletes orphan vim undo files

 $ find . -type f -iname '*.un~' | while read UNDOFILE ; do FILE=$( echo "$UNDOFILE" | sed -r -e 's/.un~$//' -e 's&/\.([^/]*)&/\1&' ) ; [[ -e "$FILE" ]] || rm "$UNDOFILE" ; done

Sept. 2, 2014, 6:51 p.m.rafaeln

1 score

Extensive "cleanup" operations following "sudo yum upgrade"

 $ sudo yum upgrade && for pkg in $(package-cleanup --orphans -q); do repoquery $(rpm -q $pkg --queryformat="%{NAME}") | grep -q ".*" && echo $pkg; done | xargs sudo yum -y remove && for pkg in $(package-cleanup --leaves --all -q); do repoquery --groupmember $pkg | grep -q "@" || echo $pkg; done

April 16, 2014, 9:58 p.m.openiduser143

1 score

Get average CPU temperature from all cores.

 $ __=`sensors | grep Core` && echo \(`echo $__ | sed 's/.*+\(.*\).C\(\s\)\+(.*/\1/g' | tr "\n" "+" | head -c-1`\)\/`echo $__ | wc -l` | bc && unset __

April 2, 2014, 10:04 p.m.openiduser139

1 score

Concatenate multiple SSL certificate files to make one PEM file

 $ files=("yourcert.crt" "provider.ca.pem") && for i in ${files[@]} ; do $(cat $i >> yourcert.pem && echo "" >> yourcert.pem) ; done

April 2, 2014, 5:41 p.m.renoirb

1 score

List all non Git comited files and make a gzip archive with them

 $ GITFOLDER="/srv/some/folder"   ls-files --others --exclude-standard | tar czf ${GITFOLDER}-archives/uploads-$(date '+%Y%m%d%H%M').tar.gz -T -

April 2, 2014, 5:18 p.m.renoirb

1 score

Converts DD/MM/YYYY date format to ISO-8601 (YYYY-MM-DD)

 $ sed 's_\([0-9]\{1,2\}\)/\([0-9]\{1,2\}\)/\([0-9]\{4\}\)_\3-\2-\1_g'

Dec. 30, 2013, 10:30 a.m.laurip

1 score

Counting the number of commas in CSV format

 $ perl -ne 'print tr/,//, "\n"' < file.csv | sort -u

Dec. 1, 2013, 1:03 p.m.Janos

1 score

Find which log files contain or don't contain a specific error message

 $ for i in *.log; do grep OutOfMemo $i >/dev/null && echo $i oom || echo $i ok; done

Sept. 13, 2013, 3:43 p.m.Janos

1 score

Convert text from decimal to little endian hexadecimal

 $ echo $(printf %08X 256 | grep -o .. | tac | tr -d '\n')

Aug. 21, 2013, 8:44 p.m.openiduser111

1 score

Md5sum the last 5 files in a folder

 $ find /directory1/directory2/ -maxdepth 1 -type f | sort | tail -n 5 | xargs md5sum

Aug. 21, 2013, 3:26 p.m.openiduser113

1 score

Create a transparent image of given dimensions

 $ convert -size 100x100 xc:none transparency.png

July 31, 2013, 11:32 p.m.Janos

1 score

Print a random cat

 $ wget -O - http://placekitten.com/$[500 + RANDOM % 500] | lp

July 26, 2013, 11:43 p.m.openiduser104

1 score

Insert lines from one text file to another one

 $ sed -re ':a;Rfile1' -e 'x;s/^/./;/.{10}/!{x;ba};s/.*//;x' file2

June 22, 2013, 9:29 a.m.openiduser102

1 score

Send a file by email as attachment

 $ uuencode /var/log/messages messages.txt | mailx -s "/var/log/messages on $HOST" me@example.com

May 26, 2013, 9:37 a.m.Janos

1 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

Get only the latest version of a file from across mutiple directories.

 $ find . -name 'filename' | xargs -r ls -tc | head -n1

March 7, 2013, 11:39 p.m.Anntoin

1 score

Get load average in a more parse-able format

 $ python -c 'import os; print os.getloadavg()[0]'

Jan. 5, 2013, 3:32 a.m.FoxWilson