In Linux, why can't I create a file and a directory with same name. Seeing following error when test file exists.
$mkdir test
mkdir: cannot create directory ‘test’: File exists
$cd test
bash: cd: test: Not a directory
A directory is a special kind of a file - one that doesn't have any data of its own, but a list of other files it contains.
Like with any other file, you can't have two files with the same name in the same location, regardless of whether they are regular files, directories, symbolic links, named pipes, or whatever.
You misused the term file, which can actually be a directory, a socket or a pipe. The name test
is kept in a directory entry which is linked to an inode corresponding to a file.
The file in the traditional meaning is called regular file in Unix, check S_ISREG
macro in stat(2) call man page.
If you want, you can create a file and a directory with the same name when you use different capitalization of the letters.
$mkdir Test ; touch test
$ls -l
-rw-r--r-- 1 user 1002 0 Oct 8 10:52 test
drwxr-xr-x 2 user 1002 40 Oct 8 10:52 Test
$find -iname test
./test
./Test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With