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

Calculate the average execution time (of short running scripts) with awk

 $ for i in {1..10}; do time some_script.sh; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'

Dec. 21, 2011, 8:50 a.m.Janos

0 score

Check the performance of a script by re-running many times while measuring the running time

 $ for i in {1..10}; do time curl http://localhost:8000 >/dev/null; done 2>&1 | grep real

Dec. 17, 2011, 1:49 a.m.Janos

0 score

A convenient way to re-run the previous command with sudo

 $ sudo !!

Dec. 14, 2011, 11:26 p.m.Janos

0 score

Put an ssh session in the background

 $ ~^z

Dec. 9, 2011, 7:44 p.m.Janos

1 score

Rotate a movie file with mencoder

 $ mencoder video.avi -o rotated-right.avi -oac copy -ovc lavc -vf rotate=1

Dec. 2, 2011, 10:30 p.m.Janos

0 score

Recursively remove all empty sub-directories from a directory tree

 $ find . -type d | tac | xargs rmdir 2>/dev/null

Nov. 29, 2011, 8:01 p.m.Janos

0 score

Remove all the versioned-but-empty directories from a Subversion checkout

 $ find . -name .svn -type d | while read ss; do dir=$(dirname "$ss"); test $(ls -a "$dir" | wc -l) == 3 && echo "svn rm \"$dir\""; done

Nov. 27, 2011, 8:38 a.m.Janos

0 score

Create a sequence of integer numbers

 $ echo {4..-9}

Nov. 24, 2011, 10:07 p.m.Janos

0 score

Redirect the output of the time builtin command

 $ { time command; } > out.out 2> time+err.out

Nov. 20, 2011, 8:34 p.m.Janos

0 score

Copy a directory with a large number of files to another server

 $ tar cp -C /path/to/dir . | ssh server2 'tar x -C /path/to/target'

Nov. 17, 2011, 12:19 p.m.Janos

0 score

Redirect the output of multiple commands

 $ { cmd1 ; cmd2 ; cmd3 ; } > out.out 2> err.out

Nov. 14, 2011, 10:08 a.m.Janos

0 score

View a file with line numbers

 $ grep -n ^ /path/to/file | less

Nov. 9, 2011, 11:05 p.m.Janos

0 score

Print the n-th and m-th line of a file

 $ sed -ne '101 p' -e '106 p' /path/to/the/file

Nov. 6, 2011, 11:20 p.m.Janos

0 score

Repeat the previous command but with a string replacement

 $ ^geomtry^geometry

Nov. 4, 2011, 7:10 a.m.Janos

2 score

Rename all files in the current directory by capitalizing the first letter of every word in the filenames

 $ ls | perl -ne 'chomp; $f=$_; tr/A-Z/a-z/; s/(?<![.'"'"'])\b\w/\u$&/g; print qq{mv "$f" "$_"\n}'

Nov. 1, 2011, 12:51 p.m.Janos

0 score

Do not save command history of current bash session

 $ HISTFILE=

Oct. 29, 2011, 2:41 p.m.Janos

0 score

Use rsync instead of cp to get a progress indicator when copying large files

 $ rsync --progress largefile.gz somewhere/else/

Oct. 19, 2011, 12:48 a.m.Janos

0 score

Unpack all of the .tar.bz2 files in current directory

 $ for FILE in *; do tar -jxf $FILE; done

Oct. 17, 2011, 11:23 p.m.versorge

0 score

Create and restore backups using cpio

 $ find . -xdev -print0 | cpio -oa0V | gzip > path_to_save.cpio.gz

Oct. 17, 2011, 11:06 p.m.Janos

0 score

Alert me by email when a disconnected or unreachable server comes back online

 $ while ! ping -c1 the_host_down; do sleep 1; done && date | mail -s 'the host is back!' me@example.com

Oct. 10, 2011, 8:26 p.m.janos

0 score

Add timestamp to the output of ping

 $ ping some_host | while read LINE; do echo $(date): $LINE; done

Oct. 10, 2011, 8:13 p.m.janos

0 score

Test a one-liner with echo commands first, pipe to bash when ready

 $ paste <(ls) <(ls | tr A-Z a-z) | while read OLD NEW; do echo mv -v $OLD $NEW; done | sh

Oct. 8, 2011, 1 p.m.janos

0 score

Find the most recently modified files in a directory and all subdirectories

 $ find /path/to/dir -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f, "\n" }' | tail

Oct. 4, 2011, 10:25 p.m.janos

1 score

View specific column of data from a large file with long lines

 $ cat /tmp/log.data |colrm 1 155|colrm 60 300

Oct. 4, 2011, 9:55 p.m.versorge

0 score

Delete unversioned files in a Subversion checkout directory and all subdirectories

 $ svn st | grep ^? | sed -e 's/^? *//' | xargs -i{} echo rm -fr "{}"

Oct. 3, 2011, 8:22 p.m.janos