Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using expr from sorted by
Terminal - Commands using expr - 14 results
port=32768; while netstat -atn | grep -q :$port; do port=$(expr $port + 1); done; echo $port
expr `echo "123671" | sed -e 's/[0-9]/ + &/g' -e 's/^ +//g'` 20
dd of=output.txt if=input.txt ibs=1 skip=$(expr `stat -c%s input.txt` / 2)
i=0;while :; do i=$(expr "$i" + 1); scrot "$i".png; sleep 2; done;
2011-05-27 00:25:28
User: scripteles
Functions: expr sleep
1

Take a screenshot every 2 seconds and save it as a png file

expr `find . -type f -printf "%s + "0`
fname=$1;f=$(ls -la $fname);fsz=$(echo $f|awk '{ print $5 }');nrrec=$(wc -l $fname|awk '{ print $1 }');recsz=$(expr $fsz / $nrrec);echo "$recsz"
2010-09-14 08:40:22
User: vuurst
Functions: awk echo expr ls wc
0

command was too long...

this is the complete command:

fname=$1; f=$( ls -la $fname ); if [ -n "$f" ]; then fsz=$( echo $f | awk '{ print $5 }' ); if [ "$fsz" -ne "0" ]; then nrrec=$( wc -l $fname | awk '{ print $1 }' ); recsz=$( expr $fsz / $nrrec ); echo "$recsz"; else echo "0"; fi else echo "file $fname does not exist" >&2; fi

First the input is stored in var $fname

The file is checked for existance using "ls -lart".

If the output of "ls -lart" is empty, the error message is given on stderr

Otherwise the filelength is taken from the output of "ls -lart" (5th field)

With "wc -l" the number of records (or lines) is taken.

The record size is filelength devided by the number of records.

please note: this method does not take into account any headers, variable length records and only works on ascii files where the records are sperated by 0x0A (or 0x0A/0x0D on MS-DOS/Windows).

jb() { if [ -z $1 ];then printf 'usage:\njb <"date and/or time"> <"commandline"> &\nsee parsedate(3) strftime(3)\n';else t1=$(date +%s); t2=$(date -d "$1" +%s) ;sleep $(expr $t2 - $t1);$2 ;fi ;}
2010-08-26 23:50:42
User: argv
Functions: date expr printf
2

EXAMPLES

jb "next sun 12pm" "/bin/sh ~you/1.sh" &

jb "2010-08-29 12:00:00" "~you/1.sh" &

jb "29aug2010 gmt" ". ~you/1.sh" &

jb 12:00p.m. "nohup ./1.sh" &

jb 1min "echo stop!" &

SEE ALSO

parsedate(3) strftime(3)

watch -tn1 'date -u +%T -d @$(expr $(date -d HH:MM +%s) - $(date +%s)) | toilet -f bigmono12'
2010-06-26 11:56:11
User: prayer
Functions: date expr watch
Tags: date time
-2

Change HH:MM with your target time.

This is for a Debian/Ubuntu GNU system. You need bash (package bash), date (package coreutils) and toilet (package toilet). Install with:

# apt-get install bash coreutils toilet toilet-fonts

while [ 1 -lt 2 ]; do i=0; COL=$((RANDOM%$(tput cols)));ROW=$((RANDOM%$(tput cols)));while [ $i -lt $COL ]; do tput cup $i $ROW;echo -e "\033[1;34m" $(cat /dev/urandom | head -1 | cut -c1-1) 2>/dev/null ; i=$(expr $i + 1); done; done
2010-05-28 16:07:56
User: dave1010
Functions: cat cut expr head tput
1

Same as original, but works in bash

while :; do integer i=0; COL=$((RANDOM%$(tput cols))); ROW=$((RANDOM%$(tput cols))); while (( i <= COL)) do tput cup $i $ROW; echo "\033[1;34m" $(cat /dev/urandom | head -1 | cut -c1-1) 2>/dev/null; i=$(expr $i + 1); done done
a=($(ls *html)) && a=${a[$(expr ${#a[@]} - 1)]} && rm $a
2009-10-12 16:40:06
Functions: expr ls rm
-3

plays with bash arrays. instead of storing the list of files in a temp file, this stores the list in ram, retrieves the last element in the array (the last html file), then removes it.

expr 512 \* 7
2009-09-23 19:11:38
User: chuckr
Functions: expr
Tags: math expr
4

expr will give you a quick way to do basic math from the CLI. Make sure you escape things like * and leave a space between operators and digits.

expr $(fdisk -s ` grep ' / ' /etc/mtab |cut -d " " -f1`) / 1024
while [ 0 ]; do expr 2365200000 \- `date +%s` \- `date --date "YYYY-mm-dd HH:MM:ss" +%s`; sleep 1; clear; done
2009-02-13 20:02:37
User: wwest4
Functions: expr sleep
1

watch the seconds of your life tick away - replace YYYY-mm-dd HH:MM:ss w/ your birthtime.