Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What is the difference between filename and ./filename? Under what circumstances is one preferred to the other?

share|improve this question

5 Answers 5

up vote 18 down vote accepted

For datafiles, it does not make a difference - both statements will reference the data file in your current directory. (For example, cat filename and cat ./filename are semantically identical.)

It's a different story if the filename in question refers to an executable, i.e. is the first (or even only) thing you type on the command line. ./filename will look for the executable in your current directory, and nowhere else. filename, on the other hand, will evaluate the environment variable PATH, and look for filename in every directory stored therein (which might or might not include your current directory).

./filename (or any other /path/to/filename) is thus preferred when you want to execute a specific executable, not the first one found in your PATH.

share|improve this answer

If you're running a program, filename says to run the first one found in your $PATH and the second one says to run the one in the current directory.

If you're using that filename as an argument to a program as in do_something filename or do_something ./filename then they mean the same thing.

share|improve this answer

It depends what circumstances you're using it as.

As an argument, eg "program filename", then it's up to the program, but generally they're identical.

As a program name, eg "filename arguments", then "filename arguments" will search PATH to find the binary, while "./filename arguments" will use the program in the current directory. This is obviously useful if . isn't in the PATH, but it's also useful to use this one even if . is in the path, perhaps because it matches an earlier entry.

share|improve this answer

You are referring to command execution, right?

There is an enviroment variable PATH that contains some system directories, delimited by semicolon. When you type command the system looks in these directories in the order they are specified to find an executable file named <command> If it finds it, it tries to execute it. You can see your PATH via echo $PATH in bash. For example:

$ echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/home/ivanatora/bin:/usr/local/kde4/bin:/usr/local/kde4/bin

When you type ./filename you are specifying exact path: the current directory. You can see that the current directory seldom is in your $PATH (for security reasons). So if you want to execute a file from the current directory, you use ./filename.

To execute a file by its path (especialy if it is not in $PATH) you can also type <path>/file, like:

/sbin/ifconfig

share|improve this answer

Executable security is the main reason for this difference.

You do not want . (present working directory or PWD) in your path because someone could trojan http://en.wikipedia.org/wiki/Trojan_horse_%28computing%29 a crucial executable like ps or ls, in your PWD and fool you by presenting bad data.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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