bashoneliners.com

Welcome to bashoneliners.com, a curated collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We aim to make each published one-liner to be 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.

Replace a pattern in a file in a portable way

f=/path/to/file; sed -e "s/pattern/replacement/" "$f" > "$f".bak && mv "$f".bak "$f"

November 22, 2023bashoneliners

Count the total number of hours of your music collection

find . -print0 | xargs -0 -P 40 -n 1 sh -c 'ffmpeg -i "$1" 2>&1 | grep "Duration:" | cut -d " " -f 4 | sed "s/.$//" | tr "." ":"' - | awk -F ':' '{ sum1+=$1; sum2+=$2; sum3+=$3; sum4+=$4; if (sum4 > 100) { sum3+=1; sum4=0 }; if (sum3 > 60) { sum2+=1; sum3=0 }; if (sum2 > 60) { sum1+=1; sum2=0 } if (NR % 100 == 0) { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 } } END { printf "%.0f:%.0f:%.0f.%.0f\n", sum1, sum2, sum3, sum4 }'

March 1, 2019pingiun

Find and replace string inside specific files

grep -ril '$SEARCH_PATTERN' src | sed -i 's/$FIND_PATTERN/$REPLACE_PATTERN/g'

August 17, 2018gatero

Print wifi access points sorted by signal

iw dev IFACE scan | egrep "SSID|signal" | awk -F ":" '{print $2}' | sed 'N;s/\n/:/' | sort

June 16, 2018kazatca

Retrieve dropped connections from firewalld journaling

sudo journalctl -b | grep -o "PROTO=.*" | sed -r 's/(PROTO|SPT|DPT|LEN)=//g' | awk '{print $1, $3}' | sort | uniq -c

September 14, 2017FoxBuru

Get the latest Arch Linux news

w3m https://www.archlinux.org/ | sed -n "/Latest News/,/Older News/p" | head -n -1

August 15, 2017Jab2870

List status of all Git repositories

find ~ -type d -name .git | sed 's?/\.git$??' | awk '{ print "-------------------------"; print "\033[1;32mGit Repo:\033[0m " $0; system("git --git-dir=\""$0"\"/.git --work-tree=\""$0"\" status")}'

October 16, 2016uMt

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, 2015openiduser188

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

September 22, 2014bashoneliners

Remove all at jobs

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

September 10, 2014laurip

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

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

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

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

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

Replace sequences of the same characters with a single character

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

December 11, 2013bashoneliners

Insert lines from one text file to another one

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

June 22, 2013openiduser102

Get streamed FLV from Chrome with lsof

export psid=$(pgrep -f libflashplayer.so); cp /proc/$psid/fd/$(lsof -p $psid | grep eleted | awk {' print $4 '} | sed -e "s/[a-z]//g") saved.flv

May 11, 2013GNA

Tree-like output in ls

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

April 26, 2013clitips

Unhide all hidden files in the current directory.

find . -maxdepth 1 -type f -name '\.*' | sed -e 's,^\./\.,,' | sort | xargs -iname mv .name name

April 25, 2013openiduser93

Function to extract columns from an input stream

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

December 7, 2012bashoneliners

Replace the header of all files found.

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

October 25, 2012jam

Tail a file with "tail -f" until text is seen

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

August 22, 2012bashoneliners

Edit the Gimp launcher file to disable the splash screen

sudo sed -i 's/^Exec=[^ ]*/& -s/' /usr/share/applications/gimp.desktop

June 30, 2012bashoneliners