bashoneliners.com

Welcome to bashoneliners.com, a growing collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We review and improve every contributed one-liner to make sure it is of high quality: useful, easy to read, follows best practices, with clear, detailed, accurate explanation. These one-liners should help you automate tasks, troubleshoot problems, whether it be in system administration, file management, networking or programming.

0

Redirection operator to override the noclobber option

some_command >| output.txt

August 11, 2012bashoneliners

0

How to set the ip address in Solaris 11

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

August 3, 2012bashoneliners

1

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, 2012Kleper

1

Use vim to pretty-print code with syntax highlighting

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

July 21, 2012bashoneliners

1

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, 2012dark_axl

1

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, 2012Anon8yhYNaVe

0

Edit the Gimp launcher file to disable the splash screen

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

June 30, 2012bashoneliners

0

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, 2012bashoneliners

0

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, 2012kevin

0

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

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

June 21, 2012bashoneliners

0

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, 2012bashoneliners

0

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, 2012bashoneliners

0

Convert m4a files to mp3 using faad and lame

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

June 14, 2012bashoneliners

0

Write both stdout and stderr to the same file

do_something.sh &> out.log

June 5, 2012bashoneliners

0

Create or mount an encrypted directory using encfs

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

May 22, 2012bashoneliners

1

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, 2012austindcc

0

Make a hexdump or do the reverse with the xxd command

xxd /path/to/binary/file

May 16, 2012bashoneliners

0

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, 2012bashoneliners

0

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

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

May 12, 2012Anon5DuJaBeh

0

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, 2012Anon1UHy2ubE

0

Sort du output in Human-readable format

du -hsx * | sort -rh

April 26, 2012Vaevictus

0

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, 2012bashoneliners

0

Expire a user's password immediately

chage -d 0 USERNAME

April 23, 2012bashoneliners