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

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

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