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

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

2 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

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