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

Replace symlinks with the actual files they are pointing at

 $ find /path/to/dir -type l -exec sh -c 'cp --remove-destination "$(readlink "{}")" "{}"' \; 

April 24, 2012, 3:29 p.m.Janos

0 score

Expire a user's password immediately

 $ chage -d 0 USERNAME

April 23, 2012, 11:05 p.m.Janos

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

2 score

Create a visual report of the contents of a usb drive

 $ find /path/to/drive -type f -exec file -b '{}' \; -printf '%s\n' | awk -F , 'NR%2 {i=$1} NR%2==0 {a[i]+=$1} END {for (i in a) printf("%12u %s\n",a[i],i)}' | sort -nr

April 15, 2012, 6:04 a.m.Anon1Qa6UsYT

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

1 score

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

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

April 12, 2012, 8 p.m.openiduser43

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

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

0 score
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

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

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