When I try to execute files in Linux, I'm always annoyed by the need to write ./executablefile instead of just executablefile. I get why it's done, so it won't have an ambigous input, one that is a command and also a file.

But I want it to execute the file if a command is not found (commands have priority). I made a tweak through a guide that if you write a command and it's not found, it searches it in the pacman database and tells me in which package the command is at, so I know it's possible to do something when it says command not found.

Is there a good way to do this?

  • 2
    You can append ./ to your PATH environment variable, but that's not a default for several very good reasons. – Shadur Sep 3 '14 at 11:22

The easiest (but generally contraindicated) method is to simply append ./ to your PATH environment variable at the end of your shell startup script, like so (bash/sh variants):

export PATH=$PATH:./

This will result in the shell attempting all the usual directories first before looking in the local directory for the command.

Note: Use at your own risk and absolutely don't do this as root.

  • 1
    export PATH=PATH:. without trailling / would do the trick – Archemar Sep 3 '14 at 11:52
  • 1
    It is not a good thing to do as PATH will read everything in the current directory wherever any command is executed, may cause ambiguity between sys and local commands as export is reflective. I would recommend modifying command_not_found_handle() method as in the answer by @chaos – Manan Sharma Sep 3 '14 at 11:56
  • 1
    Which is why I immediately mentioned that this is not a terribly good idea. – Shadur Sep 3 '14 at 11:57
  • @shadur I agree – Manan Sharma Sep 3 '14 at 12:00
  • echo "echo rm -rf ~/" > /tmp/sl ; chmod +x /tmp/sl ... And if anyone enters /tmp and has "." in its path, the mistype of "ls" as "sl" could have erased most of that person's home directory... (Well, here it will just say it instead of doing it, but you can see why it's really a bad idea!)... everyone can write somewhere (especially in /tmp, as in this example). There are also numerous other variants (and common mistypes). – Olivier Dulac Sep 3 '14 at 16:55

I'm using command-not-found version: 0.2.44. There is a function called command_not_found_handle() that is executed if a command is not found and it serves a list of commands that are similar.

Write the function as follows (just copy-paste it into you terminal, it's not permantent):

command_not_found_handle ()
    if [ -x $1 ]; then
        echo "executing ./$1"
        return $?;
    if [ -x /usr/lib/command-not-found ]; then
        /usr/bin/python /usr/lib/command-not-found -- "$1";
        return $?;
        if [ -x /usr/share/command-not-found/command-not-found ]; then
            /usr/bin/python /usr/share/command-not-found/command-not-found -- "$1";
            return $?;
            printf "%s: command not found\n" "$1" 1>&2;
            return 127;

I edited the first few lines. When you type, for example, foobar now and the command is not found, it looks in the current folder if there is a file called foobar and if this file is excutable (-x), it will be executed (./$1). You can remove the echo line if you want.

To make it permanent:

That function is normally defined in the file /etc/bash.bashrc. You can append the function into you ~/.bashrc file. That will override the parts in /etc/bash.bashrc.


Put your file in /usr/bin and then you can execute it by just typing the name. Also tab-completion works there

  • +1'd for often used scripts, although I tend to put it in ~/bin. – Sidney Sep 3 '14 at 13:30
  • On many systems the proper location is not /usr/bin but rather /usr/local/bin. If it is only for a single user, then ~/bin is more appropriate. – kasperd Sep 3 '14 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.