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

Group count sort a log file

 $ A=$(FILE=/var/log/myfile.log; cat $FILE | perl -p -e 's/.*,([A-Z]+)[\:\+].*/$1/g' | sort -u | while read LINE; do grep "$LINE" $FILE | wc -l | perl -p -e 's/[^0-9]+//g'; echo -e "\t$LINE"; done;);echo "$A"|sort -nr

Jan. 31, 2012, 6:49 p.m.openiduser14

1 score

Use ghostscript to shrink PDF files

 $ gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Jan. 31, 2012, 10:43 a.m.openiduser10

1 score

How to find all hard links to a file

 $ find /home -xdev -samefile file1

Jan. 30, 2012, 8:56 p.m.openiduser7

1 score

Find all the unique 4-letter words in a text

 $ cat ipsum.txt | perl -ne 'print map("$_\n", m/\w+/g);' | tr A-Z a-z | sort | uniq | awk 'length($1) == 4 {print}'

Jan. 29, 2012, 10:28 p.m.Janos

1 score

Concatenate two or more movie files into one using mencoder

 $ mencoder cd1.avi cd2.avi -o movie.avi -ovc copy -oac copy

Dec. 24, 2011, 3:51 p.m.Janos

1 score

Calculate the average execution time (of short running scripts) with awk

 $ for i in {1..10}; do time some_script.sh; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'

Dec. 21, 2011, 8:50 a.m.Janos

1 score

A convenient way to re-run the previous command with sudo

 $ sudo !!

Dec. 14, 2011, 11:26 p.m.Janos

1 score

Rotate a movie file with mencoder

 $ mencoder video.avi -o rotated-right.avi -oac copy -ovc lavc -vf rotate=1

Dec. 2, 2011, 10:30 p.m.Janos

1 score

View specific column of data from a large file with long lines

 $ cat /tmp/log.data |colrm 1 155|colrm 60 300

Oct. 4, 2011, 9:55 p.m.versorge

1 score

Replace a regexp pattern in many files at once

 $ vi +'bufdo %s/pattern/replacement/g | update' +q $(grep -rl pattern /path/to/dir)

Sept. 15, 2011, 11:50 p.m.Janos

-1 score

Delete all untagged Docker images

 $ docker rmi $(docker images -f "dangling=true" -q)

April 27, 2018, 2:50 p.m.stefanobaghino

-1 score

Get a free shell account on a community server

 $ sh <(curl hashbang.sh | gpg)

March 15, 2015, 9:49 a.m.lrvick

-1 score

Run a local shell script on a remote server without copying it there

 $ ssh user@server bash < /path/to/local/script.sh

June 21, 2012, 12:06 a.m.Janos

0 score

Find files/dirs modified within a given period

 $ find . -type d -newermt "2019-01-01" \! -newermt "2019-02-01" -exec ls -ld {} \;

May 20, 2019, 6:48 p.m.fenchu

0 score

Extract audio only from video files using ffmpeg

 $ ffmpeg -i video.any -vn -acodec libvorbis audio.ogg

April 21, 2019, 10:55 p.m.Janos

0 score

Print the list of your Git commits this month

 $ git log --since='last month' --author="$(git config user.name)" --oneline

Feb. 25, 2019, 6:57 p.m.Janos

0 score

Store the output of find in an array

 $ mapfile -d $'\0' arr < <(find /path/to -print0)

Feb. 18, 2019, 7:48 p.m.Janos

0 score

Check if a text snippet is valid C code

 $ gcc -fsyntax-only -xc - <<< "text snippet"

Feb. 10, 2019, 8:12 a.m.Janos

0 score

While loop to pretty print system load (1, 5 & 15 minutes)

 $ while :; do date; awk '{printf "1 minute load: %.2f\n", $1; printf "5 minute load: %.2f\n", $2; printf "15 minute load: %.2f\n", $3}' /proc/loadavg; sleep 3; done

Sept. 5, 2018, 8:41 p.m.Janos

0 score

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

Aug. 31, 2018, 10:29 p.m.johntellsall

0 score

While loop to pretty print system load (1, 5 & 15 minutes)

 $ while [ 1 == 1 ]; do  cat /proc/loadavg | awk '{printf "1 minute load: %.2f\n", $(NF-5)}' && cat /proc/loadavg |awk '{printf "5 minute load: %.2f\n", $(NF-3)}' && cat /proc/loadavg |awk '{printf "15 minute load: %.2f\n", $(NF-2)}'; sleep 3; date; done

Aug. 30, 2018, 8:54 a.m.peek2much3

0 score

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

Aug. 28, 2018, 12:38 a.m.johntellsall

0 score

Organise image by portrait and landscape

 $ mkdir "portraits"; mkdir "landscapes"; for f in ./*.jpg; do WIDTH=$(identify -format "%w" "$f")> /dev/null; HEIGHT=$(identify -format "%h" "$f")> /dev/null; if [[ "$HEIGHT" > "$WIDTH" ]]; then mv "$f" portraits/ ; else mv "$f" landscapes/ ; fi; done

Aug. 23, 2018, 2:09 p.m.Jab2870

0 score

Create a txt files with 10000 rows

 $ for FILE in *.full ; do split -l 100000 $FILE; mv -f xaa `echo "$FILE" | cut -d'.' -f1`.txt; rm -f x*; done

Aug. 22, 2018, 2:02 p.m.Kifli88

0 score

Remove all container from an specific network (docker)

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

Aug. 17, 2018, 4:38 p.m.gatero