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

Edit the Gimp launcher file to disable the splash screen

 $ printf '%s\n' ',s/^Exec=[^ ]*/& -s/' w q | ed /usr/share/applications/gimp.desktop

July 1, 2012, 12:57 a.m.Anon8yhYNaVe

1 score

Faster disk imaging with dd

 $ dd if=/dev/sda bs=$(hdparm -i /dev/sda | grep BuffSize | cut -d ' ' -f 3 | tr [:lower:] [:upper:] | tr -d BUFFSIZE=,) conv=noerror | dd of=image.dd conv=noerror

May 19, 2012, 3:28 a.m.austindcc

1 score

Convert a decimal number to octal, hexadecimal, binary, or anything

 $ bc <<< 'obase=2;1234'

April 12, 2012, 8 p.m.openiduser43

1 score

Remove carriage return '\r' character in many files, without looping and intermediary files

 $ vi +'bufdo set ff=unix' +'bufdo %s/^M$//' +q file1 file2 file3

March 30, 2012, 3:50 p.m.Janos

1 score

Sort and remove duplicate lines in a file in one step without intermediary files

 $ vi +'%!sort | uniq' +wq file.txt

March 22, 2012, 1:09 p.m.Janos

1 score

Show files containing "foo" and "bar" and "baz"

 $ grep -l 'baz' $(grep -l 'bar' $(grep -lr 'foo' *) )

March 16, 2012, 5:37 a.m.Anon5eqErEbE

1 score

Find in files, recursively

 $ grep -rn 'nameserver' /etc 2>/dev/null

Feb. 19, 2012, 8:24 a.m.atpessoa

1 score

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

 $ du -cshx ./a ./b

Feb. 15, 2012, 10:43 p.m.openiduser14

1 score

Create a compressed tar file that rsync will transfer efficiently

 $ GZIP='--rsyncable' tar cvzf bobsbackup.tar.gz /home/bob

Feb. 15, 2012, 12:24 p.m.Anon6y5E4Use

1 score

Cut select pages from a pdf file and create a new file from those pages.

 $ ps2pdf -dFirstPage=3 -dLastPage=10 input.pdf output.pdf

Feb. 15, 2012, 11:08 a.m.Anon6y5E4Use

1 score

View a file with line numbers

 $ cat -n /path/to/file | less

Feb. 13, 2012, 5:14 p.m.openiduser28

1 score

Print the lines of file2 that are missing in file1

 $ comm -23 file2 file1

Feb. 13, 2012, 8:26 a.m.Anon9ge6A4uD

1 score

Uses 'at' to run an arbitrary command at a specified time.

 $ echo 'play alarmclock.wav 2>/dev/null' | at 07:30 tomorrow

Feb. 4, 2012, 11:03 a.m.Anon5MAQumYj

1 score

Calculate an h index from an EndNote export

 $ MAX=$(NUM=1;cat author.xml |perl -p -e 's/(Times Cited)/\n$1/g'|grep "Times Cited" |perl -p -e 's/^Times Cited:([0-9]*).*$/$1/g'|sort -nr | while read LINE; do if [ $LINE -ge $NUM ]; then echo "$NUM"; fi; NUM=$[$NUM+1]; done;); echo "$MAX"|tail -1

Feb. 4, 2012, 1:06 a.m.openiduser14

1 score

Cut select pages from a pdf file and create a new file from those pages.

 $  pdftk input.pdf cat 2-4 7 9-10 output output.pdf

Feb. 3, 2012, 6:50 a.m.mmaki

1 score

Re-compress a gzip (.gz) file to a bzip2 (.bz2) file

 $ time gzip -cd file1.tar.gz 2>~/logfile.txt | pv -t -r -b -W -i 5 -B 8M | bzip2 > file1.tar.bz2 2>>~/logfile .txt

Feb. 1, 2012, 6:02 p.m.DAVEB

1 score

Test your hard drive speed

 $ time (dd if=/dev/zero of=zerofile bs=1M count=500;sync);rm zerofile

Feb. 1, 2012, 5:35 p.m.DAVEB

1 score

Recursively remove all empty sub-directories from a directory tree

 $ find . -depth  -type d  -empty -exec rmdir {} \;

Jan. 31, 2012, 11:15 p.m.openiduser16

1 score

Group count sort a log file

 $ A=$(FILE=/var/log/myfile.log; cat $FILE | perl -p -e 's/.*,([A-Z]+)[\:\+].*/$1/g' | sort -u | while read LINE; do grep "$LINE" $FILE | wc -l | perl -p -e 's/[^0-9]+//g'; echo -e "\t$LINE"; done;);echo "$A"|sort -nr

Jan. 31, 2012, 6:49 p.m.openiduser14

1 score

Use ghostscript to shrink PDF files

 $ gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Jan. 31, 2012, 10:43 a.m.openiduser10

1 score

How to find all hard links to a file

 $ find /home -xdev -samefile file1

Jan. 30, 2012, 8:56 p.m.openiduser7

1 score

Find all the unique 4-letter words in a text

 $ cat ipsum.txt | perl -ne 'print map("$_\n", m/\w+/g);' | tr A-Z a-z | sort | uniq | awk 'length($1) == 4 {print}'

Jan. 29, 2012, 10:28 p.m.Janos

1 score

Concatenate two or more movie files into one using mencoder

 $ mencoder cd1.avi cd2.avi -o movie.avi -ovc copy -oac copy

Dec. 24, 2011, 3:51 p.m.Janos

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

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