bashoneliners.com

Welcome to bashoneliners.com, a growing collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We review and improve every contributed one-liner to make sure it is 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.

0

Add all unknown files in a Subversion checkout

svn add . --force

September 24, 2013bashoneliners

0

Find files that are not executable

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

September 18, 2013bashoneliners

1

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

September 13, 2013bashoneliners

1

Convert text from decimal to little endian hexadecimal

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

August 21, 2013openiduser111

1

Md5sum the last 5 files in a folder

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

August 21, 2013openiduser113

2

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

6

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

1

Create a transparent image of given dimensions

convert -size 100x100 xc:none transparency.png

July 31, 2013bashoneliners

1

Print a random cat

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

July 26, 2013openiduser104

0

Create a heap dump of a Java process

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

July 8, 2013bashoneliners

0

Insert lines from one text file to another one

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

June 22, 2013openiduser102

1

Insert lines from one text file to another one

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

June 22, 2013openiduser102

0

Check that a directory is a parent of another

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

June 13, 2013Couannette

0

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, 2013mmaki

1

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, 2013bashoneliners

0

Calculate md5sum from an input string

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

May 17, 2013kowalcj0

3

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, 2013kowalcj0

1

Find distinct file extensions in a folder

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

May 17, 2013kowalcj0

2

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

0

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, 2013GNA

2

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

4

Tree-like output in ls

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

April 26, 2013clitips

2

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, 2013openiduser93

0

Rename all files in a directory to upper case

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

April 20, 2013EvaggelosBalaskas

7

Rename all items in a directory to lowercase names

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

April 20, 2013EvaggelosBalaskas