Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating file and directory with same name

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
like image 725
atamit81 Avatar asked Oct 08 '15 08:10

atamit81


3 Answers

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.

like image 98
Mureinik Avatar answered Nov 12 '22 20:11

Mureinik


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.

like image 45
myaut Avatar answered Nov 12 '22 19:11

myaut


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
like image 2
asvany Avatar answered Nov 12 '22 19:11

asvany