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 by glaudiston from sorted by
Terminal - Commands by glaudiston - 41 results
eval echo $(echoprint-codegen "/path/to/file.mp3"| jq ' .[0].metadata | "mp3info -a \"" + .artist + "\" -t \"" + .title + "\" -l \"" + .release + "\" \"" + .filename + "\"" ' ) | bash
2014-08-08 21:14:53
User: glaudiston
Functions: echo eval
0

echoprint identify your song, then return artist, song name and album name(release) in a JSON. jq parse it and mp3info set the data in your mp3 file.

of course it depends on:

mp3info

jq

echoprint

You need to set the environment variable

export CODEGEN_NEST_API_KEY='YOUR_ECHONEST_KEY_HERE'

You can use it with find, but probably will bypass the 120 request/minute of developer account key. So, use a sleep to do it.

Something like:

find -name \*.mp3 | while read $f; do eval echo $(echoprint-codegen "$f" | jq ' .[0].metadata | "mp3info -a \"" + .artist + "\" -t \"" + .title + "\" -l \"" + .release + "\" \"" + .filename + "\"" ' ) | bash; sleep 1; done
wget --no-check-certificate https://www.kernel.org/$(wget -qO- --no-check-certificate https://www.kernel.org | grep tar | head -n1 | cut -d\" -f2)
diff <(cd A; find -type f|xargs md5sum ) <(cd B; find -type f | xargs md5sum )
2013-07-02 18:02:05
User: glaudiston
Functions: cd diff find md5sum xargs
6

This is usefull to diff 2 paths in branches of software, or in different versions of a same zip file. So you can get the real file diff.

ls -s|grep -E "^ *0"|sed "s/^ *0 //g"|xargs -i rm "{}"
2012-04-18 14:50:46
User: glaudiston
Functions: grep ls rm sed xargs
-8

Remove all zero size files from current directory. Its a not recursive option like:

find . -size 0c -exec rm {} \;

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.

getarray(){ a=$1;b="${a[$2]}";eval "c=$b";echo "${c[$3]}";return 0;};a[0]="( a b c )";a[1]="( d e f )";getarray a 1 2
while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &
2011-02-17 11:13:19
User: glaudiston
Functions: sleep tput
91

A nice way to use the console in full screen without forget the current time.

you can too add other infos like cpu and mem use.

$ right(){ l="$(cat -)"; s=$(echo -e "$l"| wc -L); echo "$l" | while read l;do j=$(((s-${#l})));echo "$(while ((j-->0)); do printf " ";done;)$l";done;}; ls --color=none / | right
center(){ l="$(cat -)"; s=$(echo -e "$l"| wc -L); echo "$l" | while read l;do j=$(((s-${#l})/2));echo "$(while ((--j>0)); do printf " ";done;)$l";done;}; ls --color=none / | center
2011-02-14 16:50:35
User: glaudiston
Functions: echo ls read wc
0

Center the output text in max line length of buffered output pipe;

l=10;for((i=0;i<$l;i++));do eval "a$i=($(pv=1;v=1;for((j=0;j<$l;j++));do [ $i -eq 0 -o $j -eq 0 ]&&{ v=1 && pv=1; }||v=$((pv+a$((i-1))[$((j))]));echo -n "$v ";pv=$v;done;));";eval "echo \"\${a$i[@]}\"";done | column -t;
pev winappfile.exe
echo $(( $(echo 1 "* "{2..10}) ))
find -type f | xargs -I{} du -s "{}" | sort -rn | head | cut -f2 | xargs -I{} du -sh "{}"
2011-01-04 11:10:56
User: glaudiston
Functions: cut du find head sort xargs
-1

Show the top file size in human readable form

find -type f | xargs -I{} du -sk "{}" | sort -rn | head
wget --auth-no-challenge --server-response -O- $url 2>&1 | grep "Cookie" | sed "s/^ Set-//g" > cookie.txt; wget --auth-no-challenge --server-response --http-user="user" --http-password="pw" --header="$(cat cookie.txt)" -O- $url
2010-12-01 11:24:35
User: glaudiston
Functions: grep sed wget
1

I have a server with a php requiring basic authentication, like this:

header('WWW-Authenticate: Basic realm="do auth"');

header('HTTP/1.0 401 Unauthorized');

...?>

And the basic authentication in wget do not worked:

wget --auth-no-challenge --http-user="username" --http-password="password" -O- "http://url" wget --keep-session-cookies --save-cookies=cookies.txt --load-cookies=cokies.txt --http-user="username" --http-password="password" -O- "http://url"

I always received the 401 Authorization failed.

The saved cookie is always empty.

With my way, I received the header from the server and save the cookie, then resend the session cookie with authentication data

source_print(){ set | sed -n "/^$1/,/^}$/p"; };
2010-11-24 15:55:34
User: glaudiston
Functions: sed set
0

useful if you want to get the source code of a in memory function

{ var="$( ls / | tee >(cat - >&2) )"; } 2>&1; echo -e "*** var=$var"
2010-11-19 19:36:39
User: glaudiston
Functions: echo
2

Sometimes you want to write the script output to stdout but you need to send it to email.

If you use:

var="$( ls / )"; echo -e "$var";

works but, you need to wait the script terminate to bufferize then print the output var;

With this way, you can use/work/print the output before the variable receive all the output content, then after it you can use the variable for anything else, like send email.

strace -e write=1,2 -p $PID 2>&1 | sed -un "/^ |/p" | sed -ue "s/^.\{9\}\(.\{50\}\).\+/\1/g" -e 's/ //g' | xxd -r -p
2010-10-06 19:37:39
User: glaudiston
Functions: sed strace
3

Useful to recover a output(stdout and stderr) "disown"ed or "nohup"ep process of other instance of ssh.

With the others options the stdout / stderr is intercepted, but only the first n chars.

This way we can recover ALL text of stdout or stderr

perl -MURI::Escape -e 'print uri_escape("String encoded to a url");'
wmic process list IO
2010-08-20 11:05:34
User: glaudiston
0

there's some options, see more details in :

wmic /?

wmic process /?

wmic process list /?

cat | bash
2010-08-18 13:47:46
User: glaudiston
Functions: cat
5

Sometimes you don't want to leave history, because of passwords use or somethink like.

I think it help.

tar -cf - ./file | lzma -c | ssh user@sshserver $(cd /tmp; tar --lzma -xf -)
wget -qO- ifconfig.me/ip
2010-08-05 12:04:43
User: glaudiston
Functions: wget
Tags: ip address
3

alternative to

curl ifconfig.me

for those that don't have curl

ls -l | sed "$(while (( ++i < 5 )); do echo "N;"; done) a -- COMMIT --"
2010-03-17 20:12:05
User: glaudiston
Functions: ls sed
2

specially usefull for sql scripts with insert / update statements, to add a commit command after n statements executed.

vsqlplus "SELECT * FROM TABLE_NAME;"
2010-03-01 17:11:07
User: glaudiston
0

the database client sqlplus generate results one line per row.

This function convert the sqlplus output to show the result vertically, in layout:

Column_name=[Column_Value].

Very usefull for scripts.