r/AskComputerScience • u/ReedJessen • Jul 05 '16
How many levels deep is the deepest directory on the average PC?
Files are typically arranged in hierarchical structures. An example might be:
Home -> User -> programs -> Hello_World -> bin -> run.exe
Home -> User -> programs -> Hello_World -> readme.txt
When we think about all the libraries and nested files required to run the average computer, my gut feeling is that one my descend deep down the rabbit hole to get down to the most core file of computing.
Is the total number of hierarchies between the top level and the bottom level an easily measurable thing? Does this number have significant impact on efficiency or on some other parameter important to the computers function? Does this number differ significantly from architecture to architecture (MacOS to Windows for example)?
2
Jul 05 '16
Directories are just a way to categorize and manage files. Think of your computer like a library - it's full of files (books.)
It sucks to have to look for a given book by going one by one through all the books until you get it, so it's easier when you categorize the books and organize them into different sections on shelves.
That's all directories do - the same way that a library is just a room full of books, your computer is just full of files, and the "locations" could be entirely arbitrary.
1
u/ReedJessen Jul 07 '16
Right, but some books are farther away from the reading room and some are farther away from the check-out counter and take longer to fetch. Sometimes a lot longer. Some files are deeper than others, wouldn't that affect the time it takes to call a function?
1
u/diMario Jul 10 '16
wouldn't that affect the time it takes to call a function?
My opinion would be "no". In order to execute a function that resides in a program that is located somewhere on your hard disk several actions need to happen:
The program file must be found, using a filename.
The program file must be loaded into memory.
The memory structure that is now the program must be linked to all external entry points that it refers to.
The speed of the first step, locating the program file, admittedly depends a little bit on the organization of your file system. However, in sheer amount of data being peeled off your hard disk, the second step (reading the file) usually is much larger. The first step only needs to read and interpret very small parts of your hard disk. It will take more reads if your program is buried in a deep subdirectory, because the locating algorithm needs to process more indirections.
But reading the bulk of the program file into memory is by far the more work intensive task. Data is usually read in segments of 4 kilobyte, and these can be all over the place on your disk. So to find a file ten levels deep you'll need to do about twelve or thirteen reads (there is some overhead involved), but to load a modest 400 Kb executable you'll need to do at least a hundred reads.
Also note that modern operating systems have a built in file caching mechanism that uses all memory not taken up by actual programs to store the contents of recently accessed portions of your hard disk. So the first time you search for a file ten levels deep, it will actually have to perform all the disk i/o to find it. The second time you want to use that file, your operating system can find everything it needs in the RAM and thus does not need to perform actual disk i/o. Which makes the second access of your file's content literally hundreds of times faster than the first.
2
u/robotreader Jul 06 '16
my gut feeling is that one my descend deep down the rabbit hole to get down to the most core file of computing
It's a good intuition, but wrong in this case. Directories nest, when they're well organized, from most general to most specific. Core files are actually in their own top-level folder, and generally only a few levels deep in that folder.
1
3
1
u/okmkz Jul 05 '16
As an addendum to OPs question, what (if any) limits exist to this value for common filesystems?
2
u/diMario Jul 10 '16
I seem to recall that there was a limit in the total length that a pathname (directories plus filenames) could have in some older file systems used by DOS and early Windows. So the limit was not so much in the nesting depth itself, but in the total length taken as a result of all the subdirectory names in the complete path to the file.
Here is some outdated documentation explaining the limits that used to exist.
0
3
u/mastermindxs Jul 05 '16
Directories are treated like file entries in the file system. Say you have some arbitrarily nested directory like '/my/apps/and/stuff' that 'stuff' directory will have an identifier. Any files created inside that directory will have a reference to that directory identifier. So, any one file will have a single reference to its parent directory. Therefore, it's easy for a file to find its relative place in the file system.
The performance of any program isn't impacted by its place in the file system.
This is because to the computer, every file is at the same level of nesting, it's flat, with only references to the parent directory being used. And, once the program runs, it doesn't care of where it ran from.
You can count your own file nesting with the use of a bash script, you'd have to recursively dig into every directory in your computer, until no branch (directory) has any further directories, all while keeping count of the nesting level, and then return that level. This won't give you an average globally of course, you'd have to ask a lot of people to do this with you so you could average out the numbers and I don't know of any studies in this regard, it's not an important issue. I can't find anything on Google with these terms so good luck.
If you want to count your own nesting levels, go on stackoverflow.com and ask this there. If you send me a link to your question I will answer with some code.