I am studying for my A exam and I wanted to get some clarification about how files are stored.
My understanding is that in Windows, storage of all files starts at the root of the drive, whether it be C: or D: etc..., but all files are on one of these drives, either in a subfolder of the lettered drive or directly in C: or D: etc.
In macOS and Linux, there is a root directory: "/." All mass storage devices are mounted by the OS to act as subfolders of /, usually mounted to a single folder called /mount or /media in Linux and /Volumes in macOS.
If this is true, is it possible for a file to be stored outside of a folder in either scenario?
CodePudding user response:
I can't speak about Windows, but on unix-like OSes (macOS, Linux, BSD, etc) and filesystems it's sort-of possible for a file to exist without being in a folder.
In a traditional unix-like filesystem, a directory is essentially a list of filenames and the inodes numbers that hold the actual files. The same inode number can be listed in multiple directories and/or under multiple names in the same directory. These directory entries are called "links" to the file. You can create additional directory entries (links) to a file with the ln
command. Here's a quick demo:
$ echo "This is a file" >afile.txt
$ ln afile.txt anotherlink.txt
$ ls -li
total 16
152165199 -rw-r--r-- 2 gordon staff 15 Feb 17 16:57 afile.txt
152165199 -rw-r--r-- 2 gordon staff 15 Feb 17 16:57 anotherlink.txt
This is not showing two files, this is showing two directory entries that both refer to the same file. Note that the inode number (shown because of the -i
option to ls
) is the same, and the link count (just before my username) is 2.
If you "delete" a file (with rm
or something like that), it doesn't necessarily actually delete the file. For instance:
$ rm afile.txt
$ ls -li
total 8
152165199 -rw-r--r-- 1 gordon staff 15 Feb 17 16:57 anotherlink.txt
$ cat anotherlink.txt
This is a file
Note that one directory entry got removed, and the link count is down to 1, but the file's still there.
When you remove the last directory entry to a file, the link count will drop to 0, and the file will actually be deleted... unless it's open. Files don't actually get deleted until the link count hits zero, and there are no open file handles referring to the file. Here's a demo of opening the file, unlinking it, and then accessing the file through the open file handle:
$ exec 3<> anotherlink.txt # Open the file on FD #3
$ rm anotherlink.txt
$ cat /dev/fd/3
This is a file
...so at this point, a have a file (inode #152165199) that still exists on disk and is accessible, but isn't listed in any directory (and therefore is difficult to access except through that open file handle).
As soon as I close that file handle, the file will actually get deleted.
CodePudding user response:
The / (root) file system contains files critical for the system. I don't believe it's possible to change the root of the file system or create a file outside of the root file system which is a folder/file. You can find more here.