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

Parse nginx statistics output

 $ i=$(curl -s server/nginx_stats); IFS=$'\n'; i=($i); a=${i[0]/Active connections: } && a=${a/ }; r=${i[2]# [0-9]* [0-9]* }; echo "Active: $a, requests: $r"

June 20, 2014, 3:19 p.m.azat

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

Have script run itself in a virtual terminal

 $ tty >/dev/null || { urxvt -hold -e "$0" "$@" & exit; }

March 6, 2014, 3:18 a.m.openiduser111

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

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

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

1 score

Function to extract columns from an input stream

 $ col() { awk '{print $('$(echo $* | sed -e s/-/NF-/g -e 's/ /),$(/g')')}'; }

Dec. 7, 2012, 4:14 p.m.Janos

1 score

Define an own watch(1)-like function

 $ watch () { interrupted=false; trap "interrupted=true" INT; while ! $interrupted; do $*; sleep 1 || interrupted=true; done; }

Nov. 14, 2012, 10 p.m.ulidtko

1 score

Remove offending key from known_hosts file with one swift move

 $ vi +18d +wq ~/.ssh/known_hosts

Oct. 30, 2012, 9:28 p.m.Janos

1 score

Replace the header of all files found.

 $ find . -type f -name '*.html' -exec sed -i -e '1r common_header' -e '1,/STRING/d' {} \;

Oct. 25, 2012, 9:29 a.m.jam

1 score

Redirect stdout to a file you don't have write permission on

 $ echo hello | sudo tee -a /path/to/file

Sept. 11, 2012, 9:24 a.m.Janos

1 score

`tail -f` a file until text is seen

 $ tail -f /path/to/file.log | sed '/^Finished: SUCCESS$/ q'

Aug. 22, 2012, 8:29 a.m.Janos

1 score

Recording SSH sessions

 $ ssh -l USER HOST | tee -a /path/to/file

Aug. 15, 2012, 5:04 p.m.LeandroToledo

1 score

Record audio from microphone or sound input from the console

 $ sox -t ossdsp -w -s -r 44100 -c 2 /dev/dsp -t raw - | lame -x -m s - File.mp3

July 28, 2012, 8:55 p.m.Kleper

1 score

Use vim to pretty-print code with syntax highlighting

 $ vim +'hardcopy > output.ps' +q style.css 

July 21, 2012, 12:13 a.m.Janos

1 score

Log and verify files received via FTP

 $ for i in $(cat /var/log/vsftpd.log | grep $DATE_TIME | grep UPLOAD | grep OK); do ls /FTP/HOME/$i >> /dev/null 2> \&1; if \[ $? = 0 \]; then echo "$i" >> $FILES_OK_UPLOADS.log; else  echo "$DATE ERROR: File $i not found" >> $FTP_FILES_NOTOK_$DATE_TIME.log; fi; done

July 10, 2012, 8:54 p.m.dark_axl