What is the difference between
./filename? Under what circumstances is one preferred to the other?
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.
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
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
To execute a file by its path (especialy if it is not in $PATH) you can also type
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.
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.