Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Simple question, but I'm not sure where to look and google doesn't respond to periods and slashes.

I'm just trying to count the # of files & directories in the current directory (not including subfolders/files) and was trying to differentiate ls -1 | wc -l and ls | wc -l since they seem identical. A site I was looking at said "Keep in mind that this is also counting the ./ and ../ directories." regarding the one with ls -1, and I'm not sure if that means it includes the directories before or something (which I don't want), but it didn't seem to do that from testing.

Could someone confirm which one of those would be most adequate for counting # of files & directories in the current directory only (not sub) and what they mean by ./ and ../ directories?

share|improve this question
    
The "Keep in mind that this is also counting the ./ and ../ directories" is wrong (unless it is a typo for "." and "..". –  vonbrand Jan 30 '13 at 20:55
    
@vonbrand Can you expound on that, please? –  Christopher Jan 31 '13 at 1:45

3 Answers 3

up vote 9 down vote accepted

Every directory on a Unix system (and probably every other system too) contains at least two directory entries. These are . (current directory) and .. (parent directory). In the case of the root directory, these point to the same place, but with any other directory, they are different. You can see this for yourself using the stat, pwd and cd commands (on Linux):

$ cd /
$ stat . .. bin sbin | grep Inode
Device: 802h/2050d      Inode: 2           Links: 27
Device: 802h/2050d      Inode: 2           Links: 27
Device: 802h/2050d      Inode: 548865      Links: 2
Device: 802h/2050d      Inode: 2670593     Links: 2
$ pwd
/
$ cd ..
$ pwd
/
$

Notice that bin and sbin each has two links to it. One is the directory entry in the root directory, and the other is the . entry inside that directory.

Using ls with a pipe to wc -l is a simple trick to count the number of lines in ls' output. The assumption is that each file or directory will occupy exactly one line in the output. GNU ls will, when the output is a non-terminal, do this automatically; others might need the -1 option to turn on the behavior explicitly. wc -l simply counts and outputs the number of lines (-l) in its input.

The problem with that approach is that in Linux and on the file systems traditionally used on Linux, file and directory names (they are really one and the same in this regard) are allowed to contain newline characters. In the presence of those, either method falls apart -- those entries will be counted as two or more entries when in reality they are one.

As long as you are using GNU ls, have no directory entries with names containing newline characters, and have no odd aliases for ls (for example, ls -a), both will output the count of files and directories in the current (or specified) directory. For most people, this is good enough, but it is not valid in the general case.

If you need to handle unusual characters (primarily newlines) in directory entry names properly, I suggest using ls's -b option to escape them. ls -1bA will print each directory entry name on its own line, escape unusual characters (so each directory entry will be seen as one), including any dotfiles and -directories. Tack on wc -l for a complete command line of ls -1bA | wc -l which will report the number of files and directories in the current directory (but ignore . and ..; that's the difference between -a and -A), but not descend into any subdirectories. If you don't want any dotfiles to be counted towards the total, simply omit the -A parameter to ls.

share|improve this answer
    
Both -a and -A include hidden files as well which I've realized I want to avoid, so at most I'd do ls -b | wc -l, but this does give me the right answer. Is the line I copy and pasted regarding this command incorrect? If it was correct, shouldn't I be getting an output of 8 when there are 6 files or folders in a directory? –  Adam Jan 30 '13 at 17:46
    
@Adam yes, that will probably do the trick ("ls -b | wc -l"); you can also use the "-1" (dash + number one) which forces a listing in a single column instead of all on a single line, but that happens anyway when piping to another command. So (imho) I'd leave it as "ls -1b | wc -l" ; and to "test", run "ls -1b" and count the number of lines, then run "ls -1b | wc -l" and verify the results. (That's how to debug/test pipes.) –  michael_n Jan 30 '13 at 19:44
    
@Sardathrion, that FAQ is quite misleading. The Unix filesystem conventions aren't "strange"; where they are common it is the other system picking them up. But OK. Then it says that the "file command uses magic numbers to identify ...", which is wrong: It uses a lot of heuristics too. Not very reliable in my book. –  vonbrand Jan 30 '13 at 21:00
    
@vonbrand: fair enough, link deleted. I just skimmed it and it seemed fine. I know I found one that was good but now cannot remember where it was. –  Sardathrion Jan 31 '13 at 7:37

To answer the question in the subject:

When a directory B is created in Unix, it is added as a new entry to another directory A (its parent directory), and in B, two entries are added: one called . as a hard link to itself, and one called .. as a hard link to A.

Those are the only hard links to directories allowed (though some older versions of some Unices did also allow arbitrary links).

that's why with most file systems (btrfs being a notable exception), the number of links of a directory is an indication of how many subdirectories it has (accounting for their .. entries).

When you're renaming/moving a directory, if it's to the same directory (under a different name), only the name entry in A is changed. B's . and .. is not affected. But if you move it do a different directory, then .. in B will change. That explains why you can rename a directory on which you don't have write access (assuming you have write access to the parent directory) only as long as you don't move it to another directory (otherwise the need to change the .. entry prevents you from moving it).

Beware though: /a/b/../c might not be the same as /a/c because /a/b might be a symbolic link to some other directory.

An exception to that is when that path is given to the cd command to some shells. Those cds treat .. logically ignoring the .. entries in directories. A reason why you often see cd -P in properly written scripts, to disable that feature that could otherwise cause confusion and inconsistencies.

To count the number of entries in the current directory excluding . and .. with bash, you can do:

shopt -s nullglob dotglob
set -- *
echo "$#"

With zsh:

f=(*(ND))
echo $#f

Portably:

find . ! -name . -prune -print | grep -c /
share|improve this answer
    
I'm a little confused; all three of those you listed add one to the actual count. e.g. if I have 6 files or folders in a directory, they output 7, not 6. To me, it seems like ls | wc -l is the only one excluding . and .. –  Adam Jan 30 '13 at 17:35
1  
@Adam, ls doesn't list hidden files. You need ls -A to get the same as those. IF you don't want to count the hidden files, take off the dotglob (for bash) or D (for zsh) or add a ! -name '.*' before -print for find. –  Stéphane Chazelas Jan 30 '13 at 17:36
    
Yea, I just noticed this after actually listing the files. Do you know then why ls | wc -l still gives me the right answer and excludes hidden files, . & ..? –  Adam Jan 30 '13 at 17:44
1  
Adam, in your directory, you have ., .. and another file whose name starts with . which is not listed by ls but would be by ls -A. So ls | wc -l doesn't give you the correct answer because it is not counting that hidden file. –  Stéphane Chazelas Jan 30 '13 at 17:49
1  
Yes, . and .. are hidden files since they start with a .. There's even a legend that explains that dotfiles are hidden by accident as an early implementation of ls was meant to exclude only . and .., but there was a bug that caused it to exclude any file starting with .. ls | wc -l doesn't work if filenames contain newline characters. –  Stéphane Chazelas Jan 30 '13 at 18:07

I'm not a great Linux expert, but I know Linux (used to be administrator 16 years ago, on Slackware :) good old time

the ./ and ../ directories it's simple: . is current directory, .. is previous directory (in the tree of pwd -local directory command-

If it counts them, I reckon they add 2 to the total of listing, not really go recursively and count the directory below the current one, and also count again the current directory (.) :))

So basically I think it adds the value 2 to already count(files) in the current directory.

Anyone correct me if I'm wrong.

I'm just posting to help out and seen that noone answered this question here, they might be busy. But just experiment and see if you have 10 files and get count of 12 then that's it.

share|improve this answer
    
Yes, Adrian is correct : . is the current dir and .. is the directory immediately above in the hierarchy. –  schaiba Jan 30 '13 at 13:24
    
.. does not always refer to the previous directory in the tree: in "/" .. refers to the current directory as well. –  Bonsi Scott Jan 30 '13 at 15:28
    
because you might be in / root of the filesystem ? –  Adrian Tanase Jan 30 '13 at 17:09
    
If it adds two to the # of files in a directory why am I getting the correct answer when testing it in terminal on osx? e.g. a directory with 6 files or folders will output 6, not 8 when I do ls | wc -1 –  Adam Jan 30 '13 at 17:31
    
I realized that it's because they're including hidden files (namely .ds_store), but I still don't see how ls | wc -l includes . and .. when I get the right answer, as opposed to the right answer + 2. –  Adam Jan 30 '13 at 17:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.