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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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 /
Method 3
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0