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

Converts DD/MM/YYYY date format to ISO-8601 (YYYY-MM-DD)

 $ sed 's_\([0-9]\{1,2\}\)/\([0-9]\{1,2\}\)/\([0-9]\{4\}\)_\3-\2-\1_g'

Dec. 30, 2013, 10:30 a.m.laurip

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

1 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

1 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

1 score

Convert text from decimal to little endian hexadecimal

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

Aug. 21, 2013, 8:44 p.m.openiduser111

1 score

Md5sum the last 5 files in a folder

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

Aug. 21, 2013, 3:26 p.m.openiduser113

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

6 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

1 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

1 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

3 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