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 printf from sorted by
Terminal - Commands using printf - 149 results
whichpath() { local -A path; local c p; for c; do p=$(type -P "$c"); p=${p%/*}; path[${p:-/}]=1; done; local IFS=:; printf '%s\n' "${!path[*]}"; }
2011-09-16 15:55:15
User: RanyAlbeg
Functions: printf type
0

I find it useful when I want to add another crontab entry and I need to specify the appropriate PATH.

I give ''whichpath'' a list of programs that I use inside my script and it gives me the PATH I need to use for this script.

''whichpath'' uses associative array, therefore you should have Bash v4 in order to run it.

See sample output.

i=60;while [ $i -gt 0 ];do if [ $i -gt 9 ];then printf "\b\b$i";else printf "\b\b $i";fi;sleep 1;i=`expr $i - 1`;done
SCALE=3; WIDTHL=10; WIDTHR=60; BAR="12345678"; BAR="${BAR//?/==========}"; while read LEFT RIGHT rest ; do RIGHT=$((RIGHT/SCALE)); printf "%${WIDTHL}s: %-${WIDTHR}s\n" "${LEFT:0:$WIDTHL}" "|${BAR:0:$RIGHT}*"; done < dataset.dat
2011-08-22 19:35:21
User: andreasS
Functions: printf read
0

WIDTHL=10 and WIDTHR=60 are setting the widths of the left and the right column/bar. BAR="12345678" etc. is used to create a 80 char long string of "="s. I didn't know any shorter way.

If you want to pipe results into it, wrap the whole thing in ( ... )

I know that printing bar graphs can be done rather easily by other means. Here, I was looking for a Bash only variant.

isgd () { curl 'http://is.gd/create.php?format=simple&url='"$1" ; printf "\n" }
2011-08-14 23:31:39
User: dbbolton
Functions: printf
Tags: curl shorturl url
1

Check the API. You shouldn't need sed. The print-newline at the end is to prevent zsh from inserting a % after the end-of-output.

Also works with http://v.gd

arp-scan -I eth0 -l | perl -ne '/((\d{1,3}\.){3}\d{1,3})/ and $ip=$1 and $_=`nmblookup -A $ip` and /([[:alnum:]-]+)\s+<00>[^<]+<ACTIVE>/m and printf "%15s %s\n",$ip,$1'
printf "%02d:%02d\n" $(curl -s "http://search.twitter.com/search?from=zurvollenstunde&rpp=1" | grep -E '(Es ist jetzt|ago)' | sed 's/<[^>]*>//g;s/[^[:digit:]]//g' | xargs )
mem(){ { case $1 in st*|[vgid]*) printf "%s " "$@";; *) dd if=$3 2>&1|sed '$!d;/^0/d;s/ .*//;s/^/'"$1"' '"$2"' 1 0 /; r '"$3"'' 2>/dev/null;;esac;printf "\r\nquit\r\n";}|nc -n 127.0.0.1 11211; }
2011-06-17 06:39:07
User: argv
Functions: dd printf sed
5

usage: mem memcache-command [arguments]

where memcache-command might be:

set

add

get[s]

append

prepend

replace

delete

incr

decr

cas

stats

verbosity

version

notes:

exptime argument is set to 0 (no expire)

flags argument is set to 1 (arbitrary)

printf "g/^/m0\nw\nq"|ed $FILE
gawk 'BEGIN {RS="\n\n"; if (ARGV[1]=="-i"){IGNORECASE=1; ARGC=1}};{Text[NR]=$0};END {asort(Text);for (i=1;i<=NR;i++) printf "%s\n\n",Text[i] }' -i<Zip.txt
2011-05-10 19:08:27
User: IF_Rock
Functions: gawk printf
0

Among other things, this allows the sorting of comment descriptions and command lines retrieved as text from CommandLineFu.com.

ls *.jpg | awk -F'.' '{ printf "%s %04d.%s\n", $0, $1, $2; }' | xargs -n2 mv
2011-05-01 13:32:58
User: hute37
Functions: awk ls printf xargs
Tags: awk mv xargs
3

rename file name with fixed length nomeric format pattern

services() { printf "$(service --status-all 2>&1|sed -e 's/\[ + \]/\\E\[42m\[ + \]\\E\[0m/g' -e 's/\[ - \]/\\E\[41m\[ - \]\\E\[0m/g' -e 's/\[ ? \]/\\E\[43m\[ ? \]\\E\[0m/g')\n";}
2011-04-23 12:38:09
User: stanix
Functions: printf
1

Replace service --status-all 2>&1 by service --status-all 2>/dev/null to hide all services with the status [ ? ]

while :; do sensors|grep ^Core|while read x; do printf '% .23s\n' "$x"; done; sleep 1 && clear; done;
2011-04-20 06:41:57
Functions: grep printf read sleep
1

Watch the temperatures of your CPU cores in real time at the command line. Press CONTROL+C to end.

GORY DETAILS: Your computer needs to support sensors (many laptops, for example, do not). You'll need to install the lm-sensors package if it isn't already installed. And it helps to run the `sensors-detect` command to set up your sensor kernel modules first. At the very end of the sensors-detect interactive shell prompt, answer YES to add the new lines to the list of kernel modules loaded at boot.

git branch -D `git branch | awk '{ if ($0 !~ /next|master/) printf "%s", $0 }'`
2011-04-14 17:43:21
User: denheck
Functions: awk printf
-1

This command will delete all branches in your git repository other than next and master. I use this to cleanup my git repos after making multiple branches and merging them back into next. It's much faster than individually deleting each expired branch using:

git branch -D <branch_name>
cls(){ printf "\33[2J";} or, if no printf, cat >cls;<ctrl-v><ctrl+[>[2J<enter><ctrl+d> cls(){ cat cls;}
2011-04-06 01:51:45
User: argv
Functions: cat printf
13

this leaves the cursor at the bottom of the terminal screen, where your eyes are.

ctrl-l moves it to the top, forcing you to look up.

while cat /proc/net/dev; do sleep 1; done | awk '/eth0/ {o1=n1; o2=n2; n1=$2; n2=$10; printf "in: %9.2f\t\tout: %9.2f\r", (n1-o1)/1024, (n2-o2)/1024}'
shebang() { if i=$(which $1); then printf '#!%s\n\n' $i > $2 && vim + $2 && chmod 755 $2; else echo "'which' could not find $1, is it in your \$PATH?"; fi; }
2011-03-09 14:47:32
User: bartonski
Functions: chmod echo printf vim which
3

The first argument is the interpreter for your script, the second argument is the name of the script to create.

awk -F $'\t' '{printf $1 LS $2 LS $3 LS $4 LS $5; for (i = 7; i < NF; i++) printf $i "\t"; printf "\n--\n";}' LS=$'\n' 'Ad report.tsv' | column -t -s $'\t'
2011-02-28 10:52:16
User: zhangweiwu
Functions: awk column printf
-1

The exported TSV file of Google Adwords' first five columns are text, they usually should collapse into one cell, a multi-line text cell, but there is no guaranteed way to represent line-break within cells for .tsv file format, thus Google split it to 5 columns.

The problem is, with 5 columns of text, there are hardly space to put additional fields while maintain printable output.

This script collapses the first five columns of each row into one single multi-line text cell, for console output or direct send to printer.

awk -F $'\t' '{printf $1 LS $2 LS $3 LS $4 LS $5; for (i = 7; i < NF; i++) printf $i "\t"; printf "\n";}' LS=`env printf '\u2028'` 'Ad report.tsv'
2011-02-28 10:48:46
User: zhangweiwu
Functions: awk printf
0

The exported TSV file of Google Adwords' first five columns are text, they usually should collapse into one cell, a multi-line text cell, but there is no guaranteed way to represent line-break within cells for .tsv file format, thus Google split it to 5 columns.

The problem is, with 5 columns of text, there are hardly space to put additional fields while maintain printable output.

This script collapses the first five columns of each row into one single multi-line text cell. new line character we use Line-Separator character (unicode U+2028), which is respected by gnumeric. It outputs a new .tsv file that opens in gnumeric.

MIN=10 && for i in $(seq $(($MIN*60)) -1 1); do printf "\r%02d:%02d:%02d" $((i/3600)) $(( (i/60)%60)) $((i%60)); sleep 1; done
p(){ c=$(($(tput cols)-3));j=$(($1*c/100)); tput sc;printf "[$(for((k=0;k<j;k++));do printf "=";done;)>";tput cuf $((c-j));printf "]";tput rc; };for((i=0; i<=100; i++));do p i;done;echo
2011-02-20 02:17:05
User: glaudiston
Functions: printf tput
2

A simple way yo do a progress bar like wget.

yes 'clear;printf "`date`\n" | figlet -f starwars | boxes;sleep 1' | sh
2011-02-16 03:58:19
User: lkjoel
Functions: printf sleep yes
0

This is a different version from my original command: Console clock -- Beautiful (http://www.commandlinefu.com/commands/view/7902/console-clock-beautiful )

This one uses Boxes and Figlet.

To install the dependencies on Ubuntu, type in:

sudo apt-get install boxes figlet

To install the dependencies on Debian, type in:

aptitude install boxes figlet
yes 'clear;printf "\n\n`date`\n" | figlet -f starwars;sleep 1' | sh
2011-02-16 03:05:52
User: lkjoel
Functions: printf sleep yes
1

This will show a console clock with Figlet.

This is with the Star Wars font.

Change the -f option to anything else.

Notice: You need Figlet installed for this to work.

To install Figlet on Ubuntu, type in:

sudo apt-get install figlet

To install Figlet on Debian, type in:

aptitude install figlet
yes 'clear;printf "\n\n\n\n\t\t\t`date`\n";sleep 1' | sh
2011-02-16 02:57:16
User: lkjoel
Functions: printf sleep yes
2

This command will automatically clear the old clock time, and show the new clock time.

It will also slightly format it.

awk ' { printf ("%s ", $0)} END {printf ("\n") } ' FILE
2011-02-02 11:51:41
User: bouktin
Functions: awk printf
Tags: awk
-1

remove all carriage return of a given file (or input, if used with | ) and replace them with a space (or whatever character is after %s)

println() {echo -n -e "\e[038;05;${2:-255}m";printf "%$(tput cols)s"|sed "s/ /${1:-=}/g"}
2011-01-09 18:08:18
User: joedhon
Functions: printf sed
Tags: sed tput printf
0

function for .bash_aliases that prints a line of the character of your choice in the color of your choice across the terminal.

Default character is "=", default color is white.