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

Get mac address from default interface OS X

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

Aug. 21, 2013, 10:28 a.m.spotmac

5 score

Convert directory of videos to MP4 in parallel

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

Aug. 13, 2013, 5:10 a.m.shavenwarthog

1 score

Create a transparent image of given dimensions

 $ convert -size 100x100 xc:none transparency.png

July 31, 2013, 11:32 p.m.Janos

1 score

Print a random cat

 $ wget -O - http://placekitten.com/$[500 + RANDOM % 500] | lp

July 26, 2013, 11:43 p.m.openiduser104

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

2 score

Convert a music file (mp3) to a mp4 video with a static image

 $ ffmpeg -loop_input -i cover.jpg -i soundtrack.mp3 -shortest -acodec copy output_video.mp4

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

1 score

Find all of the distinct file extensions in a folder

 $ find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u

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

2 score

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, 2013, 8:03 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

2 score

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, 2013, 8:54 a.m.openiduser95

4 score

Tree-like output in ls

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

April 26, 2013, 1:37 p.m.clitips

1 score

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, 2013, 7:46 a.m.openiduser93

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

5 score

Rename all items in a directory to lower case

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

April 20, 2013, 9:41 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

1 score

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

 $ find . -name 'filename' | xargs -r ls -tc | head -n1

March 7, 2013, 11:39 p.m.Anntoin

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

2 score

Create a thumbnail from the first page of a PDF file

 $ convert -thumbnail x80 file.pdf[0] thumb.png

Feb. 6, 2013, 9:44 p.m.Janos