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.

List docker log sizes

docker ps -qa | xargs docker inspect --format='{{.LogPath}}' | xargs sudo du -hl

June 18, 2019lockntross

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

Clear the Exim Queue (remove all messages)

exim -bp | exiqgrep -i | xargs exim -Mrm

February 11, 2019Robert

Search man pages and present a PDF

man -k . | awk '{ print $1, $2 }' | dmenu -i -p man | awk '{ print $2, $1 }' | tr -d '()' | xargs man -t | ps2pdf - - | zathura -

December 18, 2018Jab2870

Scan entire Git repos for dangerous Amazon Web Service IDs

git ls-tree --full-tree -r --name-only HEAD | xargs egrep -w '[A-Z0-9]{20}'

August 31, 2018johntellsall

Dump all AWS IAM users/roles to a Terraform file for editing / reusing in another environment

echo iamg iamgm iamgp iamip iamp iampa iamr iamrp iamu iamup | AWS_PROFILE=myprofile xargs -n1  terraforming

August 28, 2018johntellsall

Remove all container from an specific network (docker)

docker ps -a -f network=$NETWORK --format='{{.ID}}' | xargs docker rm -f

August 17, 2018gatero

Kill a process running on port 8080

lsof -i :8080 | awk '{l=$2} END {print l}' | xargs kill

June 15, 2018jamestomasino

Delete all untagged Docker images

docker images -f dangling=true -q | xargs --no-run-if-empty docker rmi

June 15, 2018penguincoder

Big CSV > batches > JSON array > CURL POST data with sleep

cat post-list.csv | split -l 30 - --filter='jq -R . | jq --slurp -c .' | xargs -d "\n" -I % sh -c 'curl -H "Content-Type: application/json" -X POST -d '"'"'{"type":1,"entries":%}'"'"' http://127.0.0.1:8080/purge-something && sleep 30'

March 7, 2018pratham2003

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

Kill a process running on port 8080

lsof -i :8080 | awk 'NR > 1 {print $2}' | xargs --no-run-if-empty kill

September 1, 2017bashoneliners

Kill a process running on port 8080

lsof -i :8080 | awk '{print $2}' | tail -n 1 | xargs kill

August 18, 2017kimbethwel

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

Nmap scan every interface that is assigned an IP

ifconfig -a | perl -lne 'print $_ for /\b(?!255)(?:\d{1,3}\.){3}(?!255)\d{1,3}\b/g' | xargs nmap -A -p0-

February 8, 2015ratchode

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

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

September 12, 2014tsjswimmer

Remove all at jobs

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

September 10, 2014laurip

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

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

Md5sum the last 5 files in a folder

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

August 21, 2013openiduser113

Get mac address from default interface OS X

netstat -rn | awk '/default/ { print $NF }' | head -1 | xargs -I {}  ifconfig {} | awk '/ether/ {print $2}'

August 21, 2013spotmac

Convert directory of videos to MP4 in parallel

for path in *.avi; do echo "${path%.avi}"; done | xargs -I{} -P9 HandBrakeCLI -i {}.avi -o {}.mp4

August 13, 2013shavenwarthog

Dump network traffic with tcpdump to file with time-stamp in its filename

date +"%Y-%m-%d_%H-%M-%Z" | xargs -I {} bash -c "sudo tcpdump -nq -s 0 -i eth0 -w ./dump-{}.pcap"

May 17, 2013kowalcj0

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