• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Linux files in directory = 8 when only 1 file

Hello expert,
Can anyone tell me why some Fedora Core 45 directories will list 8 files in the direcrtory when ls -la shows only one? Is the directory symlinked or hardlinked? or is this a bug? thanks Joel
  • 3
  • 2
4 Solutions
The number you see there is not the number of files contained in the directory. If you refer to the one immediately after -rwxrwxr-x column in ls -al output, this number shows the total number of hard links to this object in the file system. Here is how it's calculated.

Let's imagine we have the following structure - directory /a with 2 subdirectories /a/b and /a/c. If you do a ls -al in your root directory, you'll see something like:

drwxr-xr-x   24 root   root      4096 May 12 03:10 ./
drwxr-xr-x   24 root   root      4096 May 12 03:10 ../
drwxr-xr-x    4 root   root      4096 May 12 03:10 a/

The number 4 is the total number of references to the space on the disk (inode) that lists files in /a, namely:

a from /
. from /a (self)
.. from /a/b
.. from /a/c

total = 4. If you add /a/d as a subdirectory and do a ls -al at / again, you'll see:

drwxr-xr-x   24 root   root      4096 May 12 03:10 ./
drwxr-xr-x   24 root   root      4096 May 12 03:10 ../
drwxr-xr-x    5 root   root      4096 May 12 03:12 a/

Long story short, this number is always the number of subdirectories in the directory + 2. It does not represent the number of files in the directory.
JoelRidleyAuthor Commented:
Hello m1tk4
ls returns this

total 16
drwxr-xr-x 2 root root 4096 Mar 1  07:47   .
drwxr-xr-x 4 root root 4096 Apr 5  08:47   . .

and ther are no files in the directory just . and . .
I know the . . is one level up and . is the top level
what I don't understand is the total 16
Is 16 the number of files and directories from
/etc/acpi/actions to /etc because /etc has
more than 16 files in it
thanks Joel
16 is a total number of total blocks used by that directory

info coreutils ls

go to 'what information is listed' then search for '-l'

'For each directory that is listed, preface the files with a line `total BLOCKS', where BLOCKS is the total disk allocation for all files in that directory. The block size currently defaults to 1024 bytes, but this can be overridden.'
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

to get number of entries in a directory use:

ls -a | wc -l

to get recursive number of all files and directories in subdirectory:

ls -Ra | awk 'NR==1{next};/^$/{getline; next};{print}' | wc -l

to get total number of used inodes (total number of files and directories) in a filesystem:

df -i

df -i is good with two  exceptions:
1) directory entry may have 1 inode with many directory entries for that inode (hard links)
2) directory may have directory entry without any inode (symbolic link)
JoelRidleyAuthor Commented:
Thanks m1tk4 and Nopius this was very helpful  Both answers were great so 50/50 on p[oints
Thanks Joel

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now