I've always wondered why it is not possible to have two files of the same data type under the same name in the same directory. I'm mainly talking about Windows but i think this applies to other OS's. What is going on in the OS when you try to do that and it forbids you? Thanks in advance.
-
4Why this happens is pretty obvious. Suppose I have two files, `/var/apples (500k)` and `/var/apples (10gb)`. How do you delete only the 10gb one? How do you read the contents of one or the other? A world where filenames aren't unique identifiers is a world I don't want to live in. – CollinD Sep 22 '15 at 16:34
-
1Why can't you have two variables with the same name and different values? Or two people who use the same physical passport? Or why can't I be at two different physical locations at once? – Felix Kling Sep 22 '15 at 16:36
-
Also, with the same name, will take you more time to look for anything you are looking for. Because, you wouldn't which file you want. However, I found this online.[http://answers.microsoft.com/en-us/windows/forum/windows_7-files/2-files-exactly-same-name-in-same-folder/9c7628de-bc1a-48c8-a369-6a74da1ed4c4?auth=1] – Maduro Sep 22 '15 at 16:36
-
Is this really what is happening though? As far as i'm concerned (i don't have much experience though) the only way where it should forbit you to do that would be if the files are going to be written inside the same memory space. Is this what's happening? – KeyC0de Sep 22 '15 at 16:37
-
1@RestlessC0bra: It's all about identification. If two things have the same ID how can you tell which want to get? That's why we have passports and do not only use our names as IDs (our names are not unique). Of course every file may also have a unique numeric ID, but that is not really memorable for us ("what exactly was file 35723 again?"). – Felix Kling Sep 22 '15 at 16:37
-
Personally, I think files should be identified to the OS by UUIDs so that two different files can have the exact same file name. It seems crazy to me that a file's name is what is used to literally identify it in the file system. – Robert Talada Nov 29 '17 at 14:44
4 Answers
Because every file have to be unique, when they are in the same directory. In this way, when you look for it, you will know which file do you need.
Plus, all the comments under your question are totally right.
EDIT
There are different way to hide files. I hope this link can help you.
how to hide files and folders on every operating system
NOTE:
I am providing this link because the example you provide me in the comments under this answer

- 713
- 5
- 23
- 44
-
Thank you for answering. But why does it have to be unique? Let's say the file is a .txt. I wouldn't mind to search both .txt files to see which is the one i want. Of course this is hypothetical. I'm never going to do that. But i was wondering whether there was a deeper reason. – KeyC0de Sep 22 '15 at 16:46
-
uhm, the problem is, what if you have 100 files .txt? now, you would mind to open those 100 files =/. – Maduro Sep 22 '15 at 16:48
-
Maybe i have written some very important password in that .txt file and i have the other .txt file "for cover". In case someone wants to search my computer for it, the OS would point him to the other .txt. This way it would be impossible for him to find it without knowing exactly where it is? You know for curiosity's sake.. – KeyC0de Sep 22 '15 at 16:49
-
If you have some "very important file" and one with the same name "for cover", how would the OS know which one you wanted to open? Files are identified by name. – Andrew Henle Sep 22 '15 at 16:53
-
is more about to identify the file. For example, when you download something twice, the system add a number to it. So, if you download 50 files, each file will have a number or letter (not sure) at the end, but it will help you to identify the file. you can also `hide` files, if you have very important things to hide ;). At least I know you can do that on mac – Maduro Sep 22 '15 at 16:55
-
@RestlessC0bra I didn't see that comment. Maybe it was post when I was writing my answer. I am very careful when I type, because I don't want -1 on my answer =/. i will research what that comment says. – Maduro Sep 22 '15 at 16:57
-
Also, if someone search your computer, and find one file, then it will see the other one because they are in the same directory. And, this person wouldn't mind to search both files either. So, there are better solution to 'cover' things/file/folder – Maduro Sep 22 '15 at 17:05
To assuredly perform some action, the instructions to do that must be unambiguous. A filename (or file name) is used to identify a storage location in the file system.
OS wants unique file names because when you will tell OS to do something, and you have two files with exactly the same name, OS can't know which file to perform the action on. For example, when appending to a text file, OS may append to wrong one of the two with same name making the entire system unreliable. So what do we do? We can add some other unique information to each file. But that will essentially come down to same idea... why not just keep unique names?
Now that we know keeping unique names is vital, all we need is a function to check a file's existence for answering your question's part 2. Since OS's are mostly in C, What's the best way to check if a file exists in C? question on SO should answer that for you.

- 1
- 1

- 13,888
- 8
- 60
- 75
Actually, it is possible to create multiple files with the same name on Windose. That screws things up but you can write code that will do it.
Many operating systems support multiple files with the same name in the same directory and do so rationally. In fact, that was the norm before Eunuchs and PeeCees sent operating system work backwards.

- 20,574
- 3
- 26
- 62
In Windows, the default settings is to ignore extensions, so you could have a package directory and a package.txt file in the same folder. From Windows Explorer, it will appear that you have to entries called "package", when in fact, it is just hiding part of the filename of one.
-
This is not the point of the question, he was asking about two files with same "full names" (same extension). – srdjan.veljkovic Sep 22 '15 at 18:00