- 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
- Counting Files and Directories in Linux
- Find number of files in folder and sub folders?
- 9 Answers 9
- How to count number of files in each directory?
- 18 Answers 18
- How can I get a count of files in a directory using the command line?
- 18 Answers 18
Count Number of Files in a Directory in Linux
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:
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:
This is the output:
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.
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.
You can run this command:
The above command searched for all the files (type f) in current directory and its subdirectories.
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.
Here’s the output now:
In the end…
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.
Источник
Counting Files and Directories in Linux
How do you count the number of files or directories in Linux? In his blog we show you how to count files in a directory or the number of subdirectories. So when you are ready we shall begin! Although at first counting files and directories in Linux may not seem the most exciting topic you will be surprised just how much you can learn from these tasks. They are also well suited to those starting out in Linux with one or two elements that may be new even to seasoned Linux users. This all started from a question on my Facebook page. Even the answer can be quite simple there is more to investigate than you would at first think.
Firstly, if we want to be counting files and directories in Linux then the Linux command ls may be a great option Used in conjunction with the command wc we can count the number of items returned. The command ls is used to list directory content and wc is used for word count, used with -l it can count lines. Pipelining commands in fundamentals to UNIX and Linux
Whilst this is good we will not show hidden files or directories. Hidden files start with a dot. To list these we can use the option -a or -A with ls. To show all files we use -a and almost all files with -A. Yes almost all, we exclude the . and .. directories which are system links.
If we want to count the output we are better not count the . and .. directory.
From the output we can see that we have a total of 7 items in the current directory.
If we want to count directories and files separately then we can use the GNU command find. To list files we can use the option -type f. Of course we could count the output as before.
Listing directories is similar but we will see that we will include the current directory which we may not want.
To exclude the current directory from the count we can use the option -mindepth 1 to ensure that we start with the directories content and not the directory.[tux@packstack
So we can see that counting files and directories in Linux is not difficult but it can be even easier. Well at least counting directories. The hard link count for a directory can be used to show how many subdirectories there are in the directory. Each subdirectory has a link back to the parent. A directory start with a hard link count of 2 so just remove 2 from the current hard link count to see how many subdirectories.
The etc directory has 110 subdirectories on my system, 112 – 2 = 110
Источник
Find number of files in folder and sub folders?
I want to find the total count of the number of files under a folder and all its sub folders.
9 Answers 9
May be something like
find . -type f | wc -l
would do the trick. Try the command from the parent folder.
-type f finds all files in . and subfolders. The result (a list of files found) is passed ( | ) to wc -l which counts the number of lines. -name
only looks for certain files.
Use the tree command. You might need to install the tree package.
It will list all the files and folders under the given folder and list a summary at the end.
To count files (even files without an extension) at the root of the current directory, use:
To count files (even files without an extension) recursively from the root of the current directory, use:
The fastest and easiest way, is to use tree . Its speed is limited by your output terminal, so if you pipe the result to tail -1 , you’ll get immediate result. You can also control to what directory level you like the results, using the -L option. For colorized output, use -C . For example:
If it’s not already there, you can get it here.
Don’t count the output lines of find, because filenames, containing 99 newlines, will count as 100 files.
Use this command for each folder in the path
You can use find . | wc -l
find . will list all files and folders and theire contents starting in your current folder.
wc -l counts the results of find
Источник
How to count number of files in each directory?
I am able to list all the directories by
I attempted to list the contents of each directory and count the number of files in each directory by using the following command
But this summed the total number of lines returned by
Is there a way I can count the number of files in each directory?
18 Answers 18
This prints the file count per directory for the current directory level:
Assuming you have GNU find, let it find the directories and let bash do the rest:
- find . -type f to find all items of the type file , in current folder and subfolders
- cut -d/ -f2 to cut out their specific folder
- sort to sort the list of foldernames
- uniq -c to return the number of times each foldername has been counted
You could arrange to find all the files, remove the file names, leaving you a line containing just the directory name for each file, and then count the number of times each directory appears:
The only gotcha in this is if you have any file names or directory names containing a newline character, which is fairly unlikely. If you really have to worry about newlines in file names or directory names, I suggest you find them, and fix them so they don’t contain newlines (and quietly persuade the guilty party of the error of their ways).
If you’re interested in the count of the files in each sub-directory of the current directory, counting any files in any sub-directories along with the files in the immediate sub-directory, then I’d adapt the sed command to print only the top-level directory:
The first pattern captures the start of the name, the dot, the slash, the name up to the next slash and the slash, and replaces the line with just the first part, so:
The second replace captures the files directly in the current directory; they don’t have a slash at the end, and those are replace by ./ . The sort and count then works on just the number of names.
Источник
How can I get a count of files in a directory using the command line?
I have a directory with a large number of files. I don’t see a ls switch to provide the count. Is there some command line magic to get a count of files?
18 Answers 18
Using a broad definition of «file»
(note that it doesn’t count hidden files and assumes that file names don’t contain newline characters).
To include hidden files (except . and .. ) and avoid problems with newline characters, the canonical way is:
For narrow definition of file:
PS: Note ls — | wc —
I have found du —inodes useful, but I’m not sure which version of du it requires. It should be substantially faster than alternative approaches using find and wc .
On Ubuntu 17.10, the following works:
Combine with | sort -nr to sort descending by number of containing inodes.
Probably the most complete answer using ls / wc pair is
if you want to count dot files, and
- -A is to count dot files, but omit . and .. .
- -q make ls replace nongraphic characters, specifically newline character, with ? , making output 1 line for each file
To get one-line output from ls in terminal (i.e. without piping it into wc ), -1 option has to be added.
(behaviour of ls tested with coreutils 8.23)
If you know the current directory contains at least one non-hidden file:
This is obviously generalizable to any glob.
In a script, this has the sometimes unfortunate side effect of overwriting the positional parameters. You can work around that by using a subshell or with a function (Bourne/POSIX version) like:
An alternative solution is $(ls -d — * | wc -l) . If the glob is * , the command can be shortened to $(ls | wc -l) . Parsing the output of ls always makes me uneasy, but here it should work as long as your file names don’t contain newlines, or your ls escapes them. And $(ls -d — * 2>/dev/null | wc -l) has the advantage of handling the case of a non-matching glob gracefully (i.e., it returns 0 in that case, whereas the set * method requires fiddly testing if the glob might be empty).
If file names may contain newline characters, an alternative is to use $(ls -d ./* | grep -c /) .
Any of those solutions that rely on passing the expansion of a glob to ls may fail with a argument list too long error if there are a lot of matching files.
Источник