Why use './' in front of filenames?

23 Apr 2023

In Linux (and MS-DOS I guess) the period signifies the current directory, so if I have a file in the current directory called test.txt, I can refer to it as test.txt or ./test.txt

ian@enrico-rider:~$ cat test.txt
test
ian@enrico-rider:~$ cat ./test.txt
test

I mostly see this in references to files in HTML and have often wondered why. Here it is being used in a Udemy course I’m following.

It’s one of those things that’s difficult to Google, so these days my reflex is to ask ChatGPT such questions.

Okay. That makes sense for executable files. If you just type in the name, Linux will look in the current directory, then if not found, in each of the directories in your $PATH variable. But if you add the ./ to the front, it will only look in your current directory. This claim of ChatGPT’s is easily tested, lets try with cat.

ian@enrico-rider:~$ cat test.txt
test
ian@enrico-rider:~$ ./cat test.txt
-bash: ./cat: No such file or directory
ian@enrico-rider:~$ cp /usr/bin/cat cat
ian@enrico-rider:~$ ./cat test.txt
test

So that checks out, but it doesn’t explain the main place I see it - in HTML.

Again, that makes sense. But it still doesn’t answer why the instructor in my course is using it for:

cp /etc/passwd ./users.txt

Lol - I feel this is a real edge case. I can see it being more a problem with the first file rather than the second one. eg.

So, TL:DR; using ‘./’ in front of a filename can be useful when: