In the book "Learning the UNIX operating system", there is a section: "3.1.8 Listing Files", that describes the ls
command.
In the paragraph on ls -l
it describes the columns of the output of this command.
The second column of the ls -l
command contains a single number. This number is in the book described as "The number of files and directories linked to this one." ( linked to the file or directory named in the last column of the same row as the concerned number. )
I tried this command and compared the output with the actual amount of files and directories in the current directory.
ls -l
drwxr-xr-x 6 azbc staff 192 Sep 7 16:09 test
In the directory test
, I have 2 subdirectories and 1 file, and 1 hidden file and a listing of the current directory, plus a listing of the parent directory, thus together 6 files and directories.
ls -a -F
./ .hidden_file.txt dir_2/
../ dir_1/ file_1.sh
It seems logical to me to identify all files and directories (including hidden files and directories) as linked to the current directory. It also seems logical to identify the parent directory as linked to the current directory.
But why is the current directory identified as linked to itself ?
The ls -la
command for the test directory gives the following output. ( the -F option shows a /
in case of a directory behind the directory name, and a * in case of an executable)
ls -la -F
total 0
drwxr-xr-x 6 azbc staff 192 Sep 7 16:09 ./
drwxr-xr-x+ ?? azbc staff ?? Sep 7 16:06 ../
-rw-r--r-- 1 azbc staff 0 Sep 7 16:09 .hidden_file.txt
drwxr-xr-x 2 azbc staff 64 Sep 7 16:06 dir_1/
drwxr-xr-x 2 azbc staff 64 Sep 7 16:06 dir_2/
-rwx--x--x 1 azbc staff 0 Sep 7 16:06 file_1.sh*
A file itself is identified with only one link. Is the file linked to itself ? Or is it linked to the directory it is in ?
Since in the listing of a directory the directory itself is represented in the listing and therefore logical to be counted as a link.
However in the listing of a file itself there is only the file itself represented in the listing.
ls -la -F file_1.sh
-rwx--x--x 1 azbc staff 0 Sep 7 16:06 file_1.sh
That makes it logical to say that the file is linked to itself.
However it seems more logical to me to say that the file is linked to the directory it is in.
This seems not consequent.
Or is the listing of the linked files merely a counting of of the files and directories present in the listing output of the command, and not an identification of the real links to the file or directory in the the file system ?
Edit: as reply to @George Udosen, on:
"Now to try and answer your query in the comment:
'What is here being listed as a link ? Is the file itself listed ? Or is the directory that contains the file being listed ?'"
If I list the directory test
:
ls -la -F test
...
drwxr-xr-x 2 azbc staff 64 Sep 7 16:06 dir_1/
...
-rwx--x--x 1 azbc staff 0 Sep 7 16:06 file_1.sh*
it identifies the directory dir_1
with 2
links !
If I then list that directory test/dir_1
ls -la -F test/dir_1
total 0
drwxr-xr-x 2 azbc staff 64 Sep 7 16:06 ./
drwxr-xr-x 9 azbc staff 288 Sep 7 21:37 ../
Hey, indeed !! it lists 2
entries !
The file file_1.sh*
was identified with 1
link.
If I list the file file_1.sh
ls -la -F test/file_1.sh
-rwx--x--x 1 azbc staff 0 Sep 7 16:06 test/file_1.sh*
Ho !! it lists indeed 1
entry !! , namely file_1.sh
itself ! and again identifies that file with 1
entry.
By the way from this can I conclude that every entry listed having 1
link
is a file and not a directory ? Ho, this seems not to be the case as symbolic links are also listed as having 1
link / 1
entry.
Each file has an inode containing metadata about the file
unlinking
dirent
?