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

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

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

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

0 score

Get the last modification date of a file in any format you want

 $ date -r /etc/motd +%Y%m%d_%H%M%S

Oct. 17, 2012, 4:42 p.m.Janos

0 score

Forget all remembered path locations

 $ hash -r

Oct. 14, 2012, 9:46 a.m.Janos

0 score

Rename files with numeric padding

 $ perl -e 'for (@ARGV) { $o = $_; s/\d+/sprintf("%04d", $&)/e; print qq{mv "$o" "$_"\n}}'

Oct. 6, 2012, 1:38 p.m.Janos

0 score

Copy or create files with specific permissions and ownership

 $ install -b -m 600 /dev/null NEWFILE

Sept. 25, 2012, 2:20 p.m.Janos

0 score

Run command multiple times with a for loop and a sequence expression

 $ for i in {1..10}; do date; sleep 1; done

Aug. 19, 2012, 9:27 a.m.Janos

0 score

Clear the swap space forcing everything back to main memory in Linux

 $ sudo swapoff -a; sudo swapon -a

Aug. 14, 2012, 11:21 a.m.Janos

0 score

Redirection operator to override the noclobber option

 $ some_command >| output.txt

Aug. 11, 2012, 9:21 a.m.Janos

0 score

How to set the ip address in Solaris 11

 $ ipadm create-addr -T static -a 192.168.1.10/24 eth0/staticaddr

Aug. 3, 2012, 11:44 a.m.Janos

0 score

Edit the Gimp launcher file to disable the splash screen

 $ sudo sed -i 's/^Exec=[^ ]*/& -s/' /usr/share/applications/gimp.desktop

June 30, 2012, 9:06 p.m.Janos

0 score

`less` is more convenient with the `-F` flag

 $ less -F FILE1

June 25, 2012, 6:47 p.m.Janos

0 score

Append to a file text, a blank line, and the last line of another file

 $ { echo some text; echo; tail -n1 /var/log/apache2/error.log; } >> /path/to/file

June 22, 2012, 5:29 p.m.Janos

0 score

Append to a file text, a blank line, and the last line of another file

 $ echo -e "From: me\n\n$(tail -n1 /var/log/apache2/error.log)" >> file

June 21, 2012, 8:18 p.m.kevin

0 score

Convert a list of terms in slug format to capitalized words

 $ sed -e 's/^./\U&/' -e 's/_./\U&/g' -e 's/_/ /g' /path/to/input

June 17, 2012, 7:54 a.m.Janos