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.

Print the lines of file2 that exist in file1

grep -xFf file1 file2

December 11, 2023bashoneliners

Get the Proportional Set Size (PSS) memory use of a Linux process

sudo cat /proc/1/smaps | grep '^Pss:' | tr -s ' ' | cut -d' ' -f2 | paste -sd+

June 16, 2023harisokanovic

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

Scan entire Git repo for dangerous Amazon Web Service IDs

git grep -Ew '[A-Z0-9]{20}'

September 5, 2018bashoneliners

Find and replace string inside specific files

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

August 17, 2018gatero

Take values from a list (file) and search them on another file

for ITEM in $(cat values_to_search.txt); do  (egrep $ITEM full_values_list.txt && echo $ITEM found) | grep "found" >> exit_FOUND.txt; done

May 16, 2018ManuViorel

List all packages with at least a class defined in a JAR file

jar tf "$1" | grep '/.*\.class$' | xargs dirname | sort -u | tr / .

February 19, 2018stefanobaghino

Output an arbitrary number of open TCP or UDP ports in an arbitrary range

comm -23 <(seq "$FROM" "$TO") <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | shuf | head -n "$HOWMANY"

February 9, 2018stefanobaghino

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 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

Ban all IPs that attempted to access phpmyadmin on your site

grep "phpmyadmin" $path_to_access.log | grep -Po "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" | sort | uniq | xargs -I% sudo iptables -A INPUT -s % -j DROP

April 2, 2015openiduser187

Print the window title of current mpv session to display what is playing

wmctrl -pl | grep $(pidof mpv) | cut -d- -f2-

December 15, 2014openiduser171

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

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

September 12, 2014tsjswimmer

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

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

September 11, 2014tsjswimmer

Find recent logs that contain the string "Exception"

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

July 19, 2014bashoneliners

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

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

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

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

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

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

September 13, 2013bashoneliners

Convert text from decimal to little endian hexadecimal

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

August 21, 2013openiduser111

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

Remove files and directories whose name is a timestamp older than a certain time

ls | grep '....-..-..-......' | xargs -I {} bash -c "[[ x{} < x$(date -d '3 days ago' +%Y-%m-%d-%H%M%S) ]] && rm -rfv {}"

May 7, 2013openiduser95

Tree-like output in ls

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

April 26, 2013clitips