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.
0 score

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

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

Dec. 15, 2014, 3:37 a.m.openiduser171

0 score

Shuffle lines

 $ ... | perl -MList::Util -e 'print List::Util::shuffle <>'

Oct. 25, 2014, 10:40 p.m.Janos

0 score

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

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

Sept. 11, 2014, 10:03 a.m.tsjswimmer

0 score

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

Jan. 9, 2014, 12:34 p.m.Janos

0 score

Replace sequences of the same characters with a single character

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

Dec. 11, 2013, 7:58 p.m.Janos

0 score

Counting the number of commas in CSV format

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

Dec. 1, 2013, 1:03 p.m.Janos

0 score

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

Nov. 9, 2013, 11:49 a.m.Janos

0 score

Add all unknown files in a Subversion checkout

 $ svn add . --force

Sept. 24, 2013, 7:59 a.m.Janos

0 score

Find files that are not executable

 $ find /some/path -type f ! -perm -111 -ls

Sept. 18, 2013, 9:14 p.m.Janos

0 score

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

Sept. 13, 2013, 3:43 p.m.Janos

0 score

Create a heap dump of a Java process

 $ jmap -dump:format=b,file=/var/tmp/dump.hprof 1234

July 8, 2013, 8:18 a.m.Janos

0 score

Insert lines from one text file to another one

 $ awk 'NR % 10 == 1 {getline f2 < "file1"; print f2} 1' file2 | cat -n

June 22, 2013, 9:30 a.m.openiduser102

0 score

Insert lines from one text file to another one

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

June 22, 2013, 9:29 a.m.openiduser102

0 score

Check that a directory is a parent of another

 $ is_parent() { [[ "$2" =~ $1/? ]]; }

June 13, 2013, 11:03 p.m.Couannette

0 score

Create fattal tone mapped images from a directory of raw images

 $ for img in /path/to/rawimages/*.RW2; do pfsin ${img} | pfssize -x 1024 -y 768 | pfstmo_fattal02 -v -s 1 | pfsout /path/to/finished/${img%%}.jpg; done

June 3, 2013, 10:45 p.m.mmaki

0 score

Send a file by email as attachment

 $ uuencode /var/log/messages messages.txt | mailx -s "/var/log/messages on $HOST" me@example.com

May 26, 2013, 9:37 a.m.Janos

0 score

Calculate md5sum from an input string

 $ md5sum <<< YOUR_TEXT | cut -f1 -d' '

May 17, 2013, 8:17 p.m.kowalcj0

0 score

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, 2013, 10:55 p.m.GNA

0 score

Rename all files in a directory to upper case

 $ for i in *; do mv "$i" "${i^^}"; done

April 20, 2013, 9:53 p.m.EvaggelosBalaskas

0 score

Print file owners and permissions of a directory tree

 $ find /path/to/dir1 -printf "%U %G %m %p\n" > /tmp/dir1.txt

March 19, 2013, 10:51 p.m.Janos

0 score

Get only the latest version of a file from across mutiple directories

 $ find . -name custlist\* | perl -ne '$path = $_; s?.*/??; $name = $_; $map{$name} = $path; ++$c; END { print $map{(sort(keys(%map)))[$c-1]} }'

Feb. 23, 2013, 4:23 p.m.Janos

0 score

Recreate or update an existing zip file and remove files that do not exist anymore

 $ zip --filesync -r /path/to/out.zip /path/to/dir

Jan. 26, 2013, 8:48 p.m.Janos

0 score

How to expand a CIDR notation to its IPs

 $ for j in $(seq 0 255); do for i in $(seq 0 255) ; do seq -f "10.$j.$i.%g" 0 255; done; done

Jan. 16, 2013, 11:53 a.m.EvaggelosBalaskas

0 score

Make the output of the `time` builtin easier to parse

 $ TIMEFORMAT=%R

Dec. 4, 2012, 10:43 p.m.Janos

0 score

Remove EXIF data such as orientation from images

 $ mogrify -strip /path/to/image.jpg

Oct. 24, 2012, 12:08 a.m.Janos