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 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

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

3 score

Remove offending key from known_hosts file with one swift move

 $ sed -i 18d .ssh/known_hosts

Jan. 16, 2013, 2:29 p.m.EvaggelosBalaskas

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

1 score

Get load average in a more parse-able format

 $ python -c 'import os; print os.getloadavg()[0]'

Jan. 5, 2013, 3:32 a.m.FoxWilson

1 score

Function to extract columns from an input stream

 $ col() { awk '{print $('$(echo $* | sed -e s/-/NF-/g -e 's/ /),$(/g')')}'; }

Dec. 7, 2012, 4:14 p.m.Janos

4 score

Show dd status every so often

 $ watch --interval 5 killall -USR1 dd

Dec. 6, 2012, 6:16 p.m.FoxWilson

0 score

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

 $ TIMEFORMAT=%R

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

1 score

Define an own watch(1)-like function

 $ watch () { interrupted=false; trap "interrupted=true" INT; while ! $interrupted; do $*; sleep 1 || interrupted=true; done; }

Nov. 14, 2012, 10 p.m.ulidtko

1 score

Remove offending key from known_hosts file with one swift move

 $ vi +18d +wq ~/.ssh/known_hosts

Oct. 30, 2012, 9:28 p.m.Janos

1 score

Replace the header of all files found.

 $ find . -type f -name '*.html' -exec sed -i -e '1r common_header' -e '1,/STRING/d' {} \;

Oct. 25, 2012, 9:29 a.m.jam

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

1 score

Redirect stdout to a file you don't have write permission on

 $ echo hello | sudo tee -a /path/to/file

Sept. 11, 2012, 9:24 a.m.Janos

1 score

`tail -f` a file until text is seen

 $ tail -f /path/to/file.log | sed '/^Finished: SUCCESS$/ q'

Aug. 22, 2012, 8:29 a.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

1 score

Recording SSH sessions

 $ ssh -l USER HOST | tee -a /path/to/file

Aug. 15, 2012, 5:04 p.m.LeandroToledo

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