- Count number of files within a directory in Linux? [closed]
- 1 Answer 1
- Find the number of files in a directory
- 8 Answers 8
- Count Number of Files in a Directory in Linux
- Count number of files in directory in Linux
- Count number of files and directories (without hidden files)
- Count number of files and directories including hidden files
- Count number of files and directories including the subdirectories
- Count only the files, not directories
- Count only the files, not directories and only in current directory, not subdirectories
Count number of files within a directory in Linux? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
I am connecting via ssh to another host to access some data . Unfortunately a bunch of basic commands don’t seem to work on this host . If I use wc it returns «unrecognized command» . So I am looking for other options .
Use the tree command. It will give you the tree and at the bottom tell you how many files and directories there are. If you want hidden files also use tree -a .
@vanza «What exactly is the problem with wc» , what if a file has a \n in the file name? Yes, extremely unlikely! But still technically valid and possible.
1 Answer 1
Which means: ls : list files in dir
-1 : (that’s a ONE) only one entry per line. Change it to -1a if you want hidden files too
No wait . I made a booboo . You are absolutely right Sajad Lfc . ls -1 dir | egrep -c » This returns the number of files in dir . Thanks .
@SajadKaruthedath ls -l . | egrep -c ‘^-‘ does not count hidden files. I suggest adding -a flag to ls .
@runios that’s because ls -l returns an additional line at the top adding up the file sizes for a total amount. You should use ls -1 and not the ls -l . Also if one wants hidden files but without the directories . and .. you should use ls -1A | wc -l
An effective native way without using pipe: du —inodes [root@cs-1-server-01 million]# du —inodes 1000001 ./vdb.1_1.dir 1000003 . [root@cs-1-server-01 million]#
Find the number of files in a directory
Is there any method in Linux to calculate the number of files in a directory (that is, immediate children) in O(1) (independently of the number of files) without having to list the directory first? If not O(1), is there a reasonably efficient way? I’m searching for an alternative to ls | wc -l .
ls | wc -l will cause ls to do an opendir(), readdir() and probably a stat() on all the files. This will generally be at least O(n).
Yeah correct, my fault. I was thinking of O(1) and O(n) to be same, although I should know it better.
8 Answers 8
readdir is not as expensive as you may think. The knack is avoid stat’ing each file, and (optionally) sorting the output of ls.
avoids aliases in your shell, doesn’t sort the output, and lists 1 file-per-line (not strictly necessary when piping the output into wc).
The original question can be rephrased as «does the data structure of a directory store a count of the number of entries?», to which the answer is no. There isn’t a more efficient way of counting files than readdir(2)/getdents(2).
One can get the number of subdirectories of a given directory without traversing the whole list by stat’ing (stat(1) or stat(2)) the given directory and observing the number of links to that directory. A given directory with N child directories will have a link count of N+2, one link for the «..» entry of each subdirectory, plus two for the «.» and «..» entries of the given directory.
However one cannot get the number of all files (whether regular files or subdirectories) without traversing the whole list — that is correct.
The «/bin/ls -1U» command will not get all entries however. It will get only those directory entries that do not start with the dot (.) character. For example, it would not count the «.profile» file found in many login $HOME directories.
One can use either the «/bin/ls -f» command or the «/bin/ls -Ua» command to avoid the sort and get all entries.
Perhaps unfortunately for your purposes, either the «/bin/ls -f» command or the «/bin/ls -Ua» command will also count the «.» and «..» entries that are in each directory. You will have to subtract 2 from the count to avoid counting these two entries, such as in the following:
expr `/bin/ls -f | wc -l` - 2 # Those are back ticks, not single quotes.
The —format=single-column (-1) option is not necessary on the «/bin/ls -Ua» command when piping the «ls» output, as in to «wc» in this case. The «ls» command will automatically write its output in a single column if the output is not a terminal.
Count Number of Files in a Directory in Linux
Here are several ways to count the number of files in a directory in Linux command line.
I presume you are aware of the wc command for counting number of lines. We can use the same wc command with ls command to count the number of files in a directory.
This task seems simple but could soon turn slightly complex based on your need and definition of counting files. Before I confuse you further, let’s see about various use cases of counting the number of files in Linux.
Count number of files in directory in Linux
Let me first show you the content of the test directory I am going to use in this tutorial:
[email protected]:~/tutorials$ ls -la total 64 drwxr-xr-x 4 abhishek abhishek 4096 Apr 29 17:53 . drwxr-xr-x 55 abhishek abhishek 4096 Apr 29 15:50 .. -rwxr–r– 1 abhishek abhishek 456 Mar 6 16:21 agatha.txt -rw-r–r– 1 abhishek abhishek 0 Apr 16 19:53 .a.t -rwxr–r– 1 abhishek abhishek 140 Mar 22 16:41 bash_script.sh -rw-rw-r– 1 abhishek abhishek 95 Feb 11 13:12 cpluplus.cpp -rw-r–r– 1 abhishek abhishek 1778 Apr 29 16:16 my_zip_folder.zip drwxr-xr-x 4 abhishek abhishek 4096 Apr 19 19:07 newdir -rw-r–r– 1 abhishek abhishek 163 Apr 13 15:07 prog.py -rw-r–r– 1 abhishek abhishek 19183 Mar 18 18:46 services -rw-r–r– 1 abhishek abhishek 356 Dec 11 21:35 sherlock.txt -rwxrw-r– 1 abhishek abhishek 72 Jan 21 15:44 sleep.sh drwxr-xr-x 3 abhishek abhishek 4096 Jan 4 20:10 target
You can see that it has 9 files (including one hidden file) and 2 sub-directories in that directory. But you don’t have to do it manually. Let’s count the number of files using Linux commands.
Count number of files and directories (without hidden files)
You can simply run the combination of the ls and wc command and it will display the number of files:
There is a problem with this command. It counts all the files and directories in the current directories. But it doesn’t see the hidden files (the files that have name starting with a dot).
This is the reason why the above command showed me a count of 10 files instead of 11 (9 files and 2 directories).
Count number of files and directories including hidden files
You probably already know that -a option of ls command shows the hidden files. But if you use the ls -a command, it also displays the . (present directory) and .. (parent directory). This is why you need to use -A option that displays the hidden files excluding . and .. directories.
This will give you the correct count of files and directories in the current directory. Have a look at the output that shows a count of 11 (9 files and 2 directories):
You can also use this command to achieve the same result:
Note that it the option used is 1 (one) not l (L). Using the l (L) option displays an additional line at the beginning of the output (see ‘total 64’ in the directory output at the beginning of the article). Using 1 (one) lists one content per line excluding the additional line. This gives a more accurate result.
Count number of files and directories including the subdirectories
What you have see so far is the count of files and directories in the current directory only. It doesn’t take into account the files in the subdirectories.
If you want to count the number of files and directories in all the subdirectories, you can use the tree command.
This command shows the directory structure and then displays the summary at the bottom of the output.
[email protected]:~/tutorials$ tree -a . ├── agatha.txt ├── .a.t ├── bash_script.sh ├── cpluplus.cpp ├── my_zip_folder.zip ├── newdir │ ├── new_dir │ │ ├── c.xyz │ │ ├── myzip1.zip │ │ └── myzip2.zip │ └── test_dir │ ├── c.xyz │ ├── myzip1.zip │ └── myzip2.zip ├── prog.py ├── services ├── sherlock.txt ├── sleep.sh └── target ├── agatha.txt ├── file1.txt └── past ├── file1.txt ├── file2.txt └── source1 └── source2 └── file1.txt 7 directories, 19 files
As you can see in the output, it shows that there are 7 directories and 20 files in total. The good thing about this result is that it doesn’t count directories in the count of files.
Count only the files, not directories
So far, all the solutions we have seen for counting the number of files, also take directories into account. Directories are essentially files but what if you want to count only the number of files, not directories? You can use the wonderful find command.
The above command searched for all the files (type f) in current directory and its subdirectories.
[email protected]:~/tutorials$ find . -type f | wc -l 20
Count only the files, not directories and only in current directory, not subdirectories
That’s cool! But what if you want to count the number of files in the current directory only excluding the files in the subdirectories? You can use the same command as above but with a slight difference.
All you have to do is to add the ‘depth’ of your find. If you set it at 1, it won’t enter the subdirectories.
find . -maxdepth 1 -type f | wc -l
[email protected]:~/tutorials$ find . -maxdepth 1 -type f | wc -l 9
In Linux, you can have multiple ways to achieve the same goal. I am pretty sure there can be several other methods to count the number of files in Linux. If you use some other command, why not share it with us?
I hope this Linux tutorial helped you learn a few things. Stay in touch for more Linux tips.