Is there any difference between "file" and "./file" paths?
As title says, is there any difference for *NIX filesystems? e.g ls file
and ls ./file
I'm not crazy about the explanation by SmallLoanOf1M. It is technically correct but answers in a way that doesn't match the usage example in the question.
So by way of example, here is one important difference between the two from the question: "file" and "./file"
What if the file is named with a character that is parsed by the shell? Especially regarding characters interpreted by the command one is running.
Specifically, the "dash" character: "-". But other characters are meaningful to the shell.
Example. My file was named "-dingle"
Try to list the file:
ls -dingle
# ls -dingle
ls: invalid option -- 'e'
Even worse, what if the file was named "-rf rmbomb *
"? Now try removing it
rm "-rf rmbomb *"
I'm not even gonna try to run that example, but hopefully you get the idea.
So how do you list a file staring with dash? Use ./
in front.
# ls ./-dingle
./-dingle
Ditto for rm
Yes.
By issuing file
on the command line, BASH will search your $PATH environment variable for a file of that name. Unless the file resides in a directory within your $PATH variable, it won't be found.
.
means the current directory. ./
means within the current directory, in relative terms. It is the equivalent of saying something like /home/sheogorath/shivering/isles.img
when invoking ./isles.img
while working in the /home/sheogorath/shivering/
directory.
As such, it's commonly used to execute files within your working directory "in place".
EDIT:
In your example, ls
is being called upon by the shell and found by using the path variable. Its argument will be processed in your working directory, whatever that may be. Since this is the default for ls
, you won't see any difference between specifying file
and explicitly specifying ./file
as they both point to your current directory.
Not all commands will accept file paths in the working directory, and some expect to have you state files in a directory that they themselves pre-define via configuration. Among commands that accept files as arguments, these commands are less common