I'm not the only one who uses some commands in some directories, different in other directories. When I enter a specific directory from which I run a script with multiple arguments, I'd like to have it in history easily available. Instead, history is spammed by hundreds of other commands from different tabs and windows and it's hard to reach what exactly was the thing I'd like to rerun.

Imagine you have a server that you use for very specific script, that script is launched with tons of arguments -x this -y that/thing/there.conf -d -g -t also -t more - whenever you ssh onto that server, to use the script, you'll press up few times and it's there.

So, is there a way to do this on one system, under one user?

  • Is the usage of the bash shell a requirement? Because there is a plugin for ZSH available and you might also like its additional features compared to bash.
    – MaPePeR
    Dec 15, 2016 at 15:07
  • That plugin might make me switch, but I'd like to keep using bash, I already have it pimped up
    – iScrE4m
    Dec 15, 2016 at 15:14
  • You should read up on all the other awesome zsh features and this plugin will probably be the least reason to switch ;)
    – MaPePeR
    Dec 15, 2016 at 15:35

3 Answers 3


First of all, it might be simpler to just map your Up and Down buttons to history-search-backward and history-search-forward respectively. From man bash:

          Search  forward through the history for the string of characters
          between the start of the current line and the point.  This is  a
          non-incremental search.
          Search backward through the history for the string of characters
          between the start of the current line and the point.  This is  a
          non-incremental search.

With this enabled, if start typing the name of your command and then hit Up, only those commands from your history starting with whatever you've typed will be shown. That way, you can very quickly find the command you're interested in and don't need to fiddle around with directory-specific history files. Just type s, then Up and only commands starting with s will be found. Use fooba and only those starting with fooba will be shown.

To enable this, add the following lines to your ~/.inputrc file on the server (depending on your terminal emulator, you might need a slightly different format. Have a look at my answer here if this one doesn't work):

"\e[A": history-search-backward
"\e[B": history-search-forward

That said, yes it is possible to set a history file per directory. Add this function to your ~/.profile (not to your ~/.bashrc since this file isn't read by default when using ssh to log into a remote machine):

    targetDirs=("/home/terdon/foo" "/home/terdon/bar")
    for dir in "${targetDirs[@]}"; do
        if [[ "$dir" = "$PWD" ]]; then
            ## Set the history file name
            export HISTFILE="./.bash_history"
            ## clear current history
            history -c
            ## read history from the $HISTFILE
            history -r
            ## Exit the function
    ## This will be run if the PWD is not in
    ## the targetDirs array
    export HISTFILE="$HOME/.bash_history"
    ## Read the history (in case we are leaving
    ## one of the targetDirs)
    history -r

And then set your PROMPT_COMMAND variable (this is a command that is executed each time a shell prompt is shown) to it:

export PROMPT_COMMAND='setHistFile'

Change the targetDirs array to the list of the directories you want to have their own history file.


There is a shell history extension that allows you to search history and shows you relevant results first based on the current directory, git repository, exit status, and host.

You can install the project here: https://github.com/curusarn/resh
Launch it using CTRL+R (replaces reverse search).
This is how the search application looks like:

screenshot of the search application

Disclaimer: I'm the creator of the project and this is shameless self-promotion


.mycd — a script I have written — might be what you are after. The idea is to store one bash history file per directory that only contains commands executed in that directory. Once the history files are built this way, you can use ctrl+r to search through them.

You must log in to answer this question.

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