Creating file and directory with same name

10,101

Solution 1

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.

Solution 2

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.

Solution 3

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
Share:
10,101
atamit81
Author by

atamit81

Updated on June 09, 2022

Comments

  • atamit81
    atamit81 about 2 years

    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