Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Why do we use ./filename to execute a file in linux?

Why not just enter it like other commands gcc, ls etc...

share|improve this question
Wouldn't the first line be better written as "Why do we use ./command_name to execute a command in linux?" –  user15760 Jul 26 '13 at 6:18

4 Answers 4

up vote 23 down vote accepted

In Linux, UNIX and related operating systems, . denotes the current directory. Since you want to run a file in your current directory and that directory is not in your $PATH, you need the ./ bit to tell the shell where the executable is. So, ./foo means run the executable called foo that is in this directory.

You can use type or which to get the full path of any commands found in your $PATH.

share|improve this answer
It is very common to execute programs in the current directory. Why doesn't the shell search in there as well? It first searches in ., then in $PATH. –  Michael Jul 9 '13 at 22:14
there are also aliases that can get in the way, not just $PATH . –  user2485710 Oct 14 '14 at 21:35
@Michael security and sanity: If it searched in . first then it would be a security issue, you or someone else could replace ls for example (a simple virus/trojen: make a zip file with an executable named ls in it, as someone is searching through, they run this executable, that …) . If it searched in . last then you can spend a long time going crazy not knowing why your program is not working (e.g. you make a program called test, instead of running your program it runs the systems test program. Which produces no output). –  richard Mar 14 at 18:24

You can try to add :. to your $PATH variable.

Try ALT+F2 and type: gksudo gedit /etc/environment if running Linux/GTK (this is what you have if using Ubuntu).

HOWEVER, I strongly advise you NOT to do that. It's bad bad bad and bad.

You know, that kind of things work like this since 1970. There is a reason why the current directory isn't included in the $PATH.

. is the current directory

.something would be a hidden file (Type "ALT+" to make them appear in Nautilus, or try "ls -la".

./someProgram.sh is what you type to RUN an executable someProgram.sh in the current directory.

.somethingElse would mean that you have a hidden executable in the current directory, which is a bad idea.

share|improve this answer

The literal answer is as others have given: because the current directory isn't in your $PATH.

But why? In short, it's for security. If you're looking in someone else's home directory (or /tmp), and type just gcc or ls, you want to know you're running the real one, not a malicious version your prankster friend has written which erases all your files. Having . as the last entry in your path is a bit safer, but there are other attacks which make use of that.

share|improve this answer
+1 for why! Good explanation there. –  Josh Dec 1 '10 at 20:24
A practical example of someone getting bit by this:unix.stackexchange.com/questions/7767 –  mattdm Feb 23 '11 at 20:13
what could go wrong with having . at the end? –  balki May 3 '11 at 12:57
Yeah, what "other attacks" are there? Apart from typos like "ls-l". –  aib Jul 4 '11 at 3:03
Typos, or else using a common command which sysadmins type often but which may not be installed — vim, for example. –  mattdm Oct 8 '12 at 13:11

If you mean, why do you need ./ at the start - that's because (unlike in Windows), the current directory isn't part of your path by default. If you run:

$ ls

your shell looks for ls in the directories in your PATH environment variable (echo $PATH to see it), and runs the first executable called ls that it finds. If you type:

$ a.out

the shell will do likewise - but it probably won't find an executable called a.out. You need to tell the shell where a.out is - it it's in the current directory (.) then the path is ./a.out.

If you're asking why it's called "a.out", that's just the default output file name for gcc. You can change it with the -o command line arg. For example:

$ gcc test.c -o test
$ ./test
share|improve this answer
Thanks.My doubt is why do you need ./ at the start.... I got the use of "." (to poit the current directory) but why "/" after that? –  Renjith G Nov 30 '10 at 8:35
/ is the path separator in Linux, so you use it to separate the directory (.) from the filename (a.out). Without it you have .a.out which is a valid filename in its own right. (Try touch .a.out; ls -lA to see this.) –  Simon Whitaker Nov 30 '10 at 8:37
that is how you specify the path in Unix, <dir>/<file> so you are basically saying execute a file in the current directory, which is indicated by ./test –  bronzebeard Nov 30 '10 at 8:37
Red Hat Linux release 9 (Shrike) Kernel 2.4.20-8 on an i686 [renjithg@cvsserver renjithg]$ touch .a.out;ls -lA total 3 -rw-rw-r-- 1 renjithg renjithg 0 Nov 30 13:46 .a.out -rwxrwxr-x 1 renjithg renjithg 11669 Nov 30 13:46 a.out -rw-rw-r-- 1 renjithg renjithg 218 Aug 24 2009 aray.c [renjithg@cvsserver renjithg]$ –  Renjith G Nov 30 '10 at 8:48
Red Hat Linux 9? Time to upgrade! –  mattdm Feb 17 '11 at 1:53

Your Answer


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.