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

cd up a number of levels

Terminal - cd up a number of levels
function ..(){ for ((j=${1:-1},i=0;i<j;i++));do builtin cd ..;done;}
2010-01-02 08:36:12
User: bhepple
Functions: cd
3
cd up a number of levels

Instead of typing "cd ../../.." you can type ".. 3". For extremely lazy typists, you can add this alias:

alias ...=".. 2" ....=".. 3"

- so now you can write just .... !!!

NB the .. function needs to be "source"d or included in your startup scripts, perhaps .bashrc.

Alternatives

There is 1 alternative - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

What others think

Nice idea!

However, I'm not sure to get the reason why you use 'j=${1:-1}'. Is this parameter expansion trick useful? Is the new variable j useful? Please help me to understand :-)

Comment by Josay 241 weeks and 3 days ago

That's quite fun.

Alternatively (not equivalent but simplistically) :

alias :="cd .." alias :.="cd ../.." alias ::="cd ../../.." alias ::.="cd ../../../.."

# repeat until bored :-)

-so now you can write just :: !!! :-)

Comment by mpb 241 weeks and 3 days ago
{VAR:-VAL} means use $VAR if it's non-null, otherwise use VAL - think of it as a default.
Comment by bhepple 241 weeks and 3 days ago

I _like_ ':.' etc, what a hoot.

Comment by bhepple 241 weeks and 3 days ago

Hmmm php ate my '$' - that should have been: ${VAR:-VAL} means use $VAR if it's non-null, otherwise use VAL - think of it as a default.

Comment by bhepple 241 weeks and 3 days ago

it might help to add 'o=$PWD' and 'OLDPWD=$o' at the beginning and end in order for 'cd -' to work as expected

Comment by patrix 241 weeks and 2 days ago

..() { cd "$(seq ${1:0} | sed '/.*/s,,../,' | tr -d '\n')"; }

This way, you also keep your "cd -" working.

Comment by taliver 241 weeks and 1 day ago

Actually make that:

..() { cd "$(seq ${1:-1} | sed '/.*/s,,../,' | tr -d '\n')"; }

Comment by taliver 241 weeks and 1 day ago

Taliver - good point! I actually use a function for cd which wraps pushd so I never rely on 'cd -'. I'll try and post it here in case it's of interest:

# do ". acd-func.sh" # acd-func 1.0.5, 10-nov-2004 # petar marinov, http:/geocities.com/h2428, this is public domain # http://linuxgazette.net//109/marinov.html ..() { for (( j=${1:-1},i=0; i<j; i++)); do # cd .. always succeeds (on linux, at least) even if we're at / builtin cd .. done pwd ls -CF } alias ...=".. 2" ....=".. 3" cd_func () { local x2 THE_NEW_DIR ADIR INDEX local -i CNT if [[ $1 == "--" ]]; then dirs -v return 0 fi THE_NEW_DIR=$1 [[ -z $1 ]] && THE_NEW_DIR=$HOME if [[ ${THE_NEW_DIR:0:1} == '-' ]]; then # # Extract dir N from dirs INDEX=${THE_NEW_DIR:1} [[ -z $INDEX ]] && INDEX=1 ADIR=$(dirs +$INDEX) [[ -z $ADIR ]] && return 1 THE_NEW_DIR=$ADIR fi # # '~' has to be substituted by ${HOME} [[ ${THE_NEW_DIR:0:1} == '~' ]] && THE_NEW_DIR="${HOME}${THE_NEW_DIR:1}" # # Now change to the new dir and add to the top of the stack pushd "${THE_NEW_DIR}" > /dev/null [[ $? -ne 0 ]] && return 1 THE_NEW_DIR=$(pwd) || builtin cd ${THE_NEW_DIR} || exit $? pwd ls -CF # # Trim down everything beyond 11th entry popd -n +11 2>/dev/null 1>/dev/null # # Remove any other occurence of this dir, skipping the top of the stack CNT=1 while [ "$CNT" -lt 10 ]; do x2=$(dirs +${CNT} 2>/dev/null) [[ $? -ne 0 ]] && return 0 [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}" if [[ "${x2}" == "${THE_NEW_DIR}" ]]; then popd -n +$CNT 2>/dev/null 1>/dev/null CNT=$(($CNT - 1)) fi CNT=$(($CNT + 1)) done return 0 } if [[ $BASH_VERSION > "2.05" ]]; then alias cd=cd_func # ctrl+w shows the menu # but first make sure ^w doesn't get eaten by stty werase stty werase ^- bind -x '"\C-w": cd_func -- ;' fi
Comment by bhepple 241 weeks and 1 day ago

Your point of view

You must be signed in to comment.

Related sites and podcasts