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

Recording SSH sessions

 $ ssh -l USER HOST | tee -a /path/to/file

Aug. 15, 2012, 5:04 p.m.LeandroToledo

0 score

Clear the swap space forcing everything back to main memory in Linux

 $ sudo swapoff -a; sudo swapon -a

Aug. 14, 2012, 11:21 a.m.Janos

0 score

Redirection operator to override the noclobber option

 $ some_command >| output.txt

Aug. 11, 2012, 9:21 a.m.Janos

0 score

How to set the ip address in Solaris 11

 $ ipadm create-addr -T static -a 192.168.1.10/24 eth0/staticaddr

Aug. 3, 2012, 11:44 a.m.Janos

1 score

Record audio from microphone or sound input from the console

 $ sox -t ossdsp -w -s -r 44100 -c 2 /dev/dsp -t raw - | lame -x -m s - File.mp3

July 28, 2012, 8:55 p.m.Kleper

1 score

Use vim to pretty-print code with syntax highlighting

 $ vim +'hardcopy > output.ps' +q style.css 

July 21, 2012, 12:13 a.m.Janos

1 score

Log and verify files received via FTP

 $ for i in $(cat /var/log/vsftpd.log | grep $DATE_TIME | grep UPLOAD | grep OK); do ls /FTP/HOME/$i >> /dev/null 2> \&1; if \[ $? = 0 \]; then echo "$i" >> $FILES_OK_UPLOADS.log; else  echo "$DATE ERROR: File $i not found" >> $FTP_FILES_NOTOK_$DATE_TIME.log; fi; done

July 10, 2012, 8:54 p.m.dark_axl

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

0 score

Edit the Gimp launcher file to disable the splash screen

 $ sudo sed -i 's/^Exec=[^ ]*/& -s/' /usr/share/applications/gimp.desktop

June 30, 2012, 9:06 p.m.Janos

0 score

`less` is more convenient with the `-F` flag

 $ less -F FILE1

June 25, 2012, 6:47 p.m.Janos

0 score

Append to a file text, a blank line, and the last line of another file

 $ { echo some text; echo; tail -n1 /var/log/apache2/error.log; } >> /path/to/file

June 22, 2012, 5:29 p.m.Janos

0 score

Append to a file text, a blank line, and the last line of another file

 $ echo -e "From: me\n\n$(tail -n1 /var/log/apache2/error.log)" >> file

June 21, 2012, 8:18 p.m.kevin

-1 score

Run a local shell script on a remote server without copying it there

 $ ssh user@server bash < /path/to/local/script.sh

June 21, 2012, 12:06 a.m.Janos

0 score

Convert a list of terms in slug format to capitalized words

 $ sed -e 's/^./\U&/' -e 's/_./\U&/g' -e 's/_/ /g' /path/to/input

June 17, 2012, 7:54 a.m.Janos

0 score

Execute different commands with find depending on file type

 $ find /path/to/dir -type d -exec chmod 0755 '{}' \; -o -type f -exec chmod 0644 '{}' \;

June 17, 2012, 12:01 a.m.Janos

0 score

Convert m4a files to mp3 using faad and lame

 $ faad -o tmp.wav music.m4a && lame -b 192 tmp.wav music.mp3

June 14, 2012, 9:29 a.m.Janos

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

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

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