cd up a number of levels

function ..(){ for ((j=${1:-1},i=0;i<j;i++));do builtin cd ..;done;}
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.

By: bhepple
2010-01-02 08:36:12
cd cd

These Might Interest You

  • This is a kind of wrapper around the shell builtin cd that allows a person to quickly go up several directories. Instead of typing: cd ../.. A user can type: cd ... Instead of: cd ../../.. Type: cd .... Add another period and it goes up four levels. Adding more periods will take you up more levels.

    cd() { if [[ "$1" =~ ^\.\.+$ ]];then local a dir;a=${#1};while [ $a -ne 1 ];do dir=${dir}"../";((a--));done;builtin cd $dir;else builtin cd "$@";fi ;}
    eightmillion · 2009-10-29 21:43:51 4
  • Ever wanted to find the most recently modified files, but couldn't remember exactly where they were in a project directory with many subdirectories? The "find" command, using a combination of "-mtime -N" and "-depth -D" can be used to find those files. If your directory structure isn't very deep, just omit the "-depth -D", but if your directory structure is very deep, then you can limit the depth of the traversal using "-depth -D", where "D" is the maximum number of directory levels to descend. Show Sample Output

    find . -type f -depth -3 -mtime -5
    totoro · 2009-03-25 19:54:06 0
  • Change run control links from start "S" to stop "K" (kill) for whatever run levels in curly braces for a service called "myservice". NEWFN variable is for the new filename stored in the in-line shell. Use different list of run levels (rc*.d, rc{1,3,5}.d, etc.) and/or swap S with K in the command to change function of run control links. Show Sample Output

    sudo find /etc/rc{1..5}.d -name S99myservice -type l -exec sh -c 'NEWFN=`echo {} | sed 's/S99/K99/'` ; mv -v {} $NEWFN' \;
    zoomgarden · 2010-01-03 00:56:57 0
  • Okay, Jimmy, the command 'optical-monitor' is needed within the global config. for this command to work (and if supported by your licsense: yada yada, blah, blah). But, like its mad PHAT Kid, you dig? Basically, a Foundry/Brocade switch is reading the Laser light levels just like those Fiber test kits from Owl that cost 4k. I know, I also find it hard to contain myeself ;-) No really..... Show Sample Output

    show optic <slot #>
    rootgeek · 2010-03-26 03:07:22 0

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 :-)
Josay · 441 weeks and 2 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 :: !!! :-)
mpb · 441 weeks and 2 days ago
{VAR:-VAL} means use $VAR if it's non-null, otherwise use VAL - think of it as a default.
bhepple · 441 weeks and 2 days ago
I _like_ ':.' etc, what a hoot.
bhepple · 441 weeks and 2 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.
bhepple · 441 weeks and 1 day ago
it might help to add 'o=$PWD' and 'OLDPWD=$o' at the beginning and end in order for 'cd -' to work as expected
patrix · 441 weeks and 1 day ago
..() { cd "$(seq ${1:0} | sed '/.*/s,,../,' | tr -d '\n')"; } This way, you also keep your "cd -" working.
taliver · 441 weeks ago
Actually make that: ..() { cd "$(seq ${1:-1} | sed '/.*/s,,../,' | tr -d '\n')"; }
taliver · 441 weeks 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 1.0.5, 10-nov-2004 # petar marinov, http:/, this is public domain # ..() { 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
bhepple · 441 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. 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.

Share Your Commands

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.


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: