Linux files in directory = 8 when only 1 file

Posted on 2006-05-11
Last Modified: 2013-12-06
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
Question by:JoelRidley
    LVL 15

    Accepted Solution

    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.

    Author Comment

    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
    LVL 27

    Assisted Solution

    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.'
    LVL 27

    Assisted Solution

    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

    LVL 27

    Assisted Solution

    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)

    Author Comment

    Thanks m1tk4 and Nopius this was very helpful  Both answers were great so 50/50 on p[oints
    Thanks Joel

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
    1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now