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

Convert any 16:9 video to play on a QHD widescreen Android phone

 $ ffmpeg -i $1 -y -threads 0 -subq 6 -deinterlace -level 30 -f mp4 -acodec libfaac -ab 160k -ar 24000 -ac 2 -vcodec libx264 -b 1000k -maxrate 1000k -bufsize 2000k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -coder 0 -refs 2 -flags +loop -vol 256 -trellis 1 -me_method umh -async 1 $2

April 18, 2012, 8:18 p.m.openiduser44

0 score

Sort du output in Human-readable format

 $ for i in G M K; do du -hsx * | grep "[0-9]$i\b" | sort -nr; done 2>/dev/null

April 14, 2012, 11:06 a.m.Janos

0 score

Sort du output in Human-readable format

 $ for i in $(echo -e 'G\nM\nK'); do du -hsx /* 2>/dev/null | grep '[0-9]'$i | sort -rn; done

April 14, 2012, 6:02 a.m.jasembo

0 score

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

 $ echo 'obase=2;1234' | bc

April 11, 2012, 11:20 p.m.Janos

0 score

Convert from avi format to mp4 encoding

 $ ffmpeg -i file.avi file.mp4

April 11, 2012, 11:10 p.m.Janos

0 score

Format input into multiple columns, like a table, useful or pretty-printing

 $ mount | column -t

April 8, 2012, 4:08 p.m.Janos

0 score

Function to extract columns from an input stream

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

April 5, 2012, 11:36 p.m.Janos

0 score

Resize an image proportionally to some specified width or height

 $ mogrify -geometry x31 path/to/image.gif

April 3, 2012, 9:48 p.m.Janos

0 score

Do something in another directory without going there

 $ (cd /path/to/somewhere; tar c .) > somewhere.tar

April 2, 2012, 10:24 p.m.Janos

0 score

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

 $ recode pc..l1 file1 file2 file3

March 31, 2012, 5:23 p.m.Anon8MaLEqEp

0 score

Find the target path a symlink is pointing to

 $ readlink a_symbolic_link_to_somewhere

March 31, 2012, 3:23 p.m.Janos

0 score
0 score

List or edit and re-execute commands from the history list

 $ fc -l

March 15, 2012, 12:10 p.m.Janos

0 score

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

 $ find /path/to/dir -type f -mtime -7 -print0 | xargs -0 ls -lt | head

March 8, 2012, 5:10 p.m.Janos

0 score

List open files

 $ lsof -n

March 2, 2012, 10:01 a.m.Janos

0 score

Set a colorful bash prompt per dev test prod environments

 $ PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\] '

Feb. 25, 2012, 2:46 p.m.Janos

0 score

Print the lines of file2 that are missing in file1

 $ grep -vxFf file1 file2

Feb. 8, 2012, 2:42 p.m.Janos

0 score

Find in files, recursively

 $ find /etc -type f -print0 2>/dev/null | xargs -0 grep --color=AUTO -Hn 'nameserver' 2>/dev/null

Feb. 2, 2012, 7:32 p.m.openiduser21

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