bashoneliners.com

Welcome to bashoneliners.com, a growing collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We review and improve every contributed one-liner to make sure it is of high quality: useful, easy to read, follows best practices, with clear, detailed, accurate explanation. These one-liners should help you automate tasks, troubleshoot problems, whether it be in system administration, file management, networking or programming.

1

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

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

September 12, 2014tsjswimmer

0

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

firefox $(grep -i ^url='*' file.url | cut -b 5-)

September 11, 2014tsjswimmer

1

Remove all at jobs

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

September 10, 2014laurip

1

Delete orphan vi and 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

September 2, 2014rafaeln

2

Generate random texts

tr -dc a-z1-4 </dev/urandom | tr 1-2 ' \n' | awk 'length==0 || length>50' | tr 3-4 ' ' | sed 's/^ *//' | cat -s | fmt

July 31, 2014bkmeneguello

2

Find recent logs that contain the string "Exception"

find . -name '*.log' -mtime -2 -exec grep -Hc Exception {} \; | grep -v :0$

July 19, 2014bashoneliners

2

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, 2014azat

2

Install profiling versions of all libghc dpkg packages

sudo dpkg -l | grep libghc | grep "\-dev" | cut -d " " -f 3 | tr '\n' ' ' | sed -e 's/\-dev/\-prof/g' | xargs sudo apt-get install --yes

May 26, 2014openiduser146

3

Compute factorial of positive integer

fac() { { echo 1; seq $1; } | paste -s -d\* | bc; }

May 21, 2014jeroenjanssens

1

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, 2014openiduser143

3

Find all files recursively with specified string in the filename and output any lines found containing a different string

find . -name '*conf*' -exec grep -Hni 'text to match' {} \; > matches.txt

April 14, 2014n00tz

7

Display the number of connections per IP to port 80

while true; do clear; date; echo; echo "[Count] | [IP ADDR]"; echo "-------------------"; netstat -n | grep ':80\>' | awk '! /LISTEN/ {print $5}' | cut -d: -f1 | uniq -c; sleep 5; done

April 9, 2014cesp

1

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, 2014openiduser139

1

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, 2014renoirb

1

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, 2014renoirb

2

Have script run itself in a virtual terminal

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

March 6, 2014openiduser111

11

Show the 10 largest open files

lsof / | awk '$7 > 1048576 { print $7 / 1048576 "MB", $9, $1 }' | sort -nu | tail

February 28, 2014cellojoe

3

Extract your external IP address using dig

dig +short myip.opendns.com @resolver1.opendns.com

February 25, 2014bashoneliners

2

Remove .DS_Store from the repository you happen to staging by mistake

find . -name .DS_Store -exec git rm --ignore-unmatch --cached {} +

February 22, 2014Kuwana

3
0

Check if a file exists and has a size greater than X

[[ $(find /path/to/file -type f -size +51200c 2>/dev/null) ]] && echo true || echo false

January 9, 2014bashoneliners

1

Convert data format from DD/MM/YYYY to ISO-8601 (YYYY-MM-DD)

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

December 30, 2013laurip

0

Replace sequences of the same characters with a single character

echo heeeeeeelllo | sed 's/\(.\)\1\+/\1/g'

December 11, 2013bashoneliners

1

Counting the number of commas in CSV format

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

December 1, 2013bashoneliners

0

Count the lines of each file extension in a list of files

git ls-files | xargs wc -l | awk -F ' +|\\.|/' '{ sumlines[$NF] += $2 } END { for (ext in sumlines) print ext, sumlines[ext] }'

November 9, 2013bashoneliners