1

I've sub folders named 1 to 1000 under some path, lets say /path/to/mydir. So I want to create custom alias so some approach such that when I enter 105 then it should take me to /path/to/mydir/105. behind the screen i need to execute cd /path/to/mydir/105 command.

what changes should i need to do on .bashrc to do this?

Can someone help me on this?

3 Answers 3

3
for ((i=1;i<=1000;i+=1)); do alias "$i=cd \"/path/to/mydir/$i\""; done

EDIT following comment on other anwser, find not needed to retrieve directories at first level: using glob expansion

for d in /path/to/mydir/*/; do
    dn=${d%/}                      # remove trailing /
    alias "${dn##*/}=cd \"$d\""    # remove path prefix from directory for alias name
done
Sign up to request clarification or add additional context in comments.

2 Comments

LOL... that should be the solution
Just don't type alias alone on a slow terminal.
3

I think if you don't use alias you could just make a simple script to do that

#!/bin/bash
cd /path/to/yourdir/${1}

and name that with go and put in your $PATH I recommend you to put it in the /usr/local/bin/ path cause that's a path that user should have write privilege and put their executable binaries/scripts in.

so you could just

$go 105

to change your dir to the dir you want.

EDIT

in fact you could also do that with alias according to tldp

#!/bin/bash

shopt -s expand_aliases
alias go='cd  /path/to/your/dir/${1}'

and with this

source ./testalias.sh
go 1234

But you still needed to type a command go for this

6 Comments

and... adding the script's location to the $PATH or placing it/a symlink in /usr/bin
Ok got it. I thought other option instead of creating a separate command i thought of adding logic in .bashrc file. is there any way to identify if there is a number entered go to this dir? something like, if ( $_ eq numeric) cd /path/to/yourdir/$_ Could you please let me know is there any such logic I can write in .bashrc?
As far as I know, alias didn't have a backquote or replacement placeholder thing to achieve this... here is the tldp of alias
didn't think to make something generic, otherwise a function could be used instead go() { cd "/path/to/mydir/$1";}
@armnotstrong I got it, Thank you very much for everyone. I'll try this
|
0

Use find and parse the output through awk, constructing an alias command that can be stored in a file srcfile

find /path/to/mydir -type d | awk -F\/ '{ print "alias "$NF"=\"cd \\\""$0"\\\"\"" }' > srcfile

Then in the bash.rc file:

source srcfile

This will load the aliases into your bash shell.

4 Comments

the alias will be defined for sh process only not calling shell
If I try the option which is suggested by @nahuel-fouilleul, all the folders from 1 to 1000 will be aliased whether the folders exists or not.\n But using this method, I can able to create alias only for the folders exists in the given directory. But only problem I face here is how can I add (') in the output. Ex: instead of alias 11939=cd home/username/path/to/dir/11939 I need the below line as output. alias 11939='cd home/username/path/to/dir/11939' Any suggestions?
I'm sorry, I need " before cd something like, find /path/to/mydir -type d | awk -F\/ '{ print "alias "$NF" = \""cd "$0"\"" }' | sh But it is not working. any suggestions?
I have amended the solution for it to work better with the local shell and bash.rc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.