Is there a way to have an alias set for a particular directory and its subdirectories?

I know I can put it in my .bashrc file, but I would like it only for a certain directory.

Edit: what i mean is i'd like to run commands that are aliased inside of the directory

~$ foo
-bash: foo: command not found
~/project$ foo
  • Maybe you mean an alias shortcut for an specific file or command?. If so, look at this example: alias ls='df --human-readable', which make the df output as readable as possible.
    – slackmart
    Commented Feb 18, 2014 at 0:40
  • yea that is what i want, but just to have that only exist where i want it, not globally Commented Feb 18, 2014 at 0:41
  • i could have a script, but this is a repository Commented Feb 18, 2014 at 0:42
  • 1
    Sort of related: unix.stackexchange.com/questions/25487/…
    – slm
    Commented Feb 18, 2014 at 1:43

6 Answers 6


Edit: I have deleted an earlier answer that relied on autoloaded functions as local commands, but that also more-or-less re-invented the problem of having '.' in $PATH - one might run unexpected code.

Instead, here is a minimal version of a wrapper for 'cd' as several respondents have suggested which sources files only in specified directories. It does not handle pushd or popd, nor does it unset or unalias any defined commands after one moves to another directory - one could define a separate callable cleanup function for that.

cd ()
    command cd "$@" || return;
    local FRC=.functions.rc;
    case ${PWD:- $(pwd)} in
            [ -O "$FRC" ] && source "$FRC"
  • 2
    That's quite dangerous as someone could plant a .alias.foo in /tmp or other world-writable directories with back-door commands. Commented Feb 18, 2014 at 7:17
  • Note that [ -O file ] returns true if file is a symlink (by anybody) to a file you own, so one can still do harm by symlinking /tmp/.alias.foo to a harmful script your own. Note that you can edit your comments for 5 minutes after you added it, and you can also remove it. Commented Feb 18, 2014 at 8:19

I can suggest to use GNU Make utility with declaration of commands in Makefile in directory of interest (~/project). Then it is possible to get such functionality:

~$ foo
-bash: foo: command not found
~$ make foo
make: *** No rule to make target 'foo'.  Stop.
~/project$ make foo

Makefile for this example:

.PHONY : foo
    @echo "Bar!"

This will work only in directory of interest, but not in subdirectories.

  • Actually that's a very elegant solution to this problem. Really good thinking! For instance, I want rsync commands for projects I've to directly start transferring files. I was looking for directory specific aliases but Makefiles fit in perfectly: make sync will call an rsync I defined. Commented May 15, 2018 at 10:56

You could make cd, pushd and popd functions which call the builtins and then look at the CWD and use that to determine which aliases to add or remove:

function cd() {
    command cd "$@"
    blah blah blah...
  • builtin would a more correct syntax. (command also searches PATH), but both will ignore the alias/function
    – Ricky
    Commented Feb 18, 2014 at 2:13
  • @RickyBeam. command is just as fine and doesn't search in PATH (as cd is builtin) and is standard. Commented Feb 18, 2014 at 7:18

The only way I can think of to do this is by hooking the call to cd (pushd, popd, etc) to run a custom function that inspects the (real) current working directory and adds or removes aliases (or functions) as necessary.


I'm working on a project that accomplishes just that. Check it out: localalias.

Here's a demo:

enter image description here


I believe what you're asking for is called bookmarking. The 2 approaches I've seen used for doing this are to either maintain a set of aliases or use a tool to "bookmark" some long directory path, and then use this tool to "jump" to a bookmarked directory.

Method #1 - Aliases

You can make aliases to these directories, and store them in your ~/.bashrc file. You can then just type things like this:

alias dir1="cd ~/path/to/some/deep/deep/dir1"
alias dir2="cd ~/path/to/some/deep/deep/dir2"

Now when you login, aliases such as these will just work from any shell that makes use of the ~/.bashrc file.

Method #2 - Directory Bookmarking Tools

Take a look at this Q & A titled: Quick directory navigation in the terminal. Tools such as autojump or xd - eXtra fast Directory changer, can be used as well to "bookmark" frequently used directories so that you can easily change to them without having to type long paths.

Context "sensitive" aliases

This would be something that's only available when you're in a specific directory. I'm not aware of any way to do something like this. You'd have to develop your own homegrown solution to facilitate something like this. A script would be the logical method for implementing something like this.

One approach would be to develop a script that checks to see what directory you're in prior to running. If it's not the designated directory, then it would simply exit out. Otherwise it would proceed. This could be implemented as a function in Bash as well.

  • 3
    bookmarking is not what i want. Commented Feb 18, 2014 at 1:32

Not the answer you're looking for? Browse other questions tagged .