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

Write both stdout and stderr to the same file

 $ do_something.sh &> out.log

June 5, 2012, 11:34 a.m.Janos

0 score

Create or mount an encrypted directory using encfs

 $ encfs -i 5 $PWD/raw $PWD/content

May 22, 2012, 12:21 a.m.Janos

0 score

Run a never-ending process in the background in a way that you can check on its status anytime

 $ screen -d -m -S some_name ping my_router

May 17, 2012, 11:03 p.m.Janos

0 score

Make a hexdump or do the reverse with the xxd command

 $ xxd /path/to/binary/file

May 16, 2012, 10:22 a.m.Janos

0 score

Really lazy way to print the first instance of $foo that occurs after $bar

 $ ifconfig | grep ^en1 -A5 | grep inet | head -n 1

May 12, 2012, 12:30 p.m.Janos

0 score

Print the first instance of $foo that occurs after $bar

 $ sed -n '\@interface Ethernet3/1@,/!/ s/ip address/&/p' file...

May 12, 2012, 6:27 a.m.Anon5DuJaBeh

0 score

Print the first instance of $foo that occurs after $bar

 $ awk '/interface Ethernet3\/1/ {instanza=1} /!/ {instanza=0} instanza && /ip address/ {print}' file...

May 11, 2012, 12:25 p.m.Anon1UHy2ubE

0 score

Sort du output in Human-readable format

 $ du -hsx * | sort -rh

April 26, 2012, 9:08 p.m.Vaevictus

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

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