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

Concatenate PDF files using GhostScript

 $ gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=output.pdf -dBATCH file1.pdf file2.pdf file3.pdf

Jan. 26, 2012, 8:51 a.m.Janos

0 score

Format text with long lines to text with fixed width

 $ fmt -s -w80 file.txt

Jan. 22, 2012, 10:08 a.m.Janos

0 score

Come back quickly to the current directory after doing some temporary work somewhere else

 $ pushd /some/where/else; work; cd /somewhere; work; cd /another/place; popd

Jan. 15, 2012, 11:12 p.m.Janos

0 score

Export a git project to a directory

 $ git archive master | tar x -C /path/to/dir/to/export

Jan. 12, 2012, 11:04 a.m.Janos

0 score

Delete all tables of a mysql database

 $ mysql --defaults-file=my.cnf -e 'show tables' | while read t; do mysql --defaults-file=my.cnt  -e 'drop table '$t; done

Jan. 8, 2012, 7:53 a.m.Janos

0 score

Run remote X11 applications with ssh

 $ ssh -X servername

Jan. 5, 2012, 7:50 a.m.versorge

0 score

Calculate the total disk space used by a list of files or directories

 $ du -s file1 dir1 | awk '{sum += $1} END {print sum}'

Dec. 28, 2011, 8:42 p.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

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

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