Here are several ways to count the number of files in a given directory in Linux. Table of Contents. How do you know how many files are there is a directory? Method 1: Use ls and wc command for counting number of lines in directory The simplest and the most obvious option is to use the wc command for counting number of files.
Listing the contents of a directory is easy. But what if you want to list only the directories, not files and links? Linux Handbook Abhishek Prakash. Send login link. Check your inbox and click the link. Sorry, something went wrong.
July 7, July 7, May 20, January 11, Your email address will not be published. Save my name, email, and website in this browser for the next time I comment. Skip to content. Improve this question. Blake Blake 3, 2 2 gold badges 14 14 silver badges 8 8 bronze badges.
Add a comment. Active Oldest Votes. Using a broad definition of "file" ls wc -l note that it doesn't count hidden files and assumes that file names don't contain newline characters. Improve this answer. James James 3, 1 1 gold badge 14 14 silver badges 4 4 bronze badges. The -l switch causes it to count lines. In this case, it's counting the lines in the output from ls.
This is the always the way I was taught to get a file count for a given directory, too. An empty directory will still return 1 line. And if you call ls -la , you will get three lines in the directory. You want ls -lA wc -l to skip the. You'll still be off-by-one, however.
An empty directory returns 0 for me — James. A corrected approach, that would not double count files with newlines in the name, would be this: ls -q wc -l - though note that hidden files will still not be counted by this approach, and that directories will be counted.
Show 4 more comments. For narrow definition of file: find. Maciej Piechotka Maciej Piechotka 16k 10 10 gold badges 49 49 silver badges 90 90 bronze badges. And you can of course omit the -maxdepth 1 for counting files recursively or adjust it for desired max search depth. If you have a file whose name contains a newline, this approach will incorrectly count it twice. Most versions of ls do -1 automatically when the output is to a pipe.
Dennis that's interesting I didn't know that an application could tell its output was going to a pipe. Though, yes ls does use -1 if it's piped try it: ls cat , I find the -1 syntax more explicit. In my tests it was significantly faster to also provide the -f option to avoid ls sorting the filenames. Unfortunately you still get the wrong answer if your filenames contain newlines.
Show 1 more comment. On Ubuntu Frax Frax 2 2 silver badges 4 4 bronze badges. This will change the behavior of the program if you have symlinked directories in the directory you are scanning. The previous behavior was that the linked subdirectory would have its file count added to the overall count; the new behavior is that the linked directory will count as a single file, and its contents will not be counted.
If the path of a file is too long, an error message will be emitted and the program will halt. The source is available under Apache License 2.
Christopher Schultz Christopher Schultz Just great! And for those unaware: you can complile the above code in the terminal: gcc -o dircnt dircnt. Is there an easy way to make this recursive? Do you need help with the solution, or do you want me to write the whole thing? ChristopherSchultz, the benchmarks you posted above - how big was the directory in question? I really wanted to use this in Python so I packaged it up as the ffcount package.
Thanks for making the code available ChristopherSchultz! Show 11 more comments. Use find. For example: find. This will recursively find files under the current directory. If he want only current directory, not the whole tree recursively, he can add -maxdepth 1 option to find. It seems the reason find is faster than ls is because of how you are using ls. If you stop sorting, ls and find have similar performance. You should be using -printf as ives shows anyway, so the count is correct when some joker writes filenames with newlines in them.
Show 1 more comment. Thomas Thomas 2, 2 2 gold badges 24 24 silver badges 28 28 bronze badges. You should definitely clear the cache between tests. The first time I run ls -l wc -l on a folder on an external 2.
The second time it takes 12 seconds IIRC. Also this could potentially depend on your file system too. I was using Btrfs. Thank you, perl snippet is solution for me. Use -f to disable the sorting, which will save some time: ls -f wc -l Or you can use find : find. Mohammad Anini Mohammad Anini 4, 3 3 gold badges 36 36 silver badges 44 44 bronze badges. I found the example a little bit confusing. I was wondering why there were numbers on the left, instead of directory names.
Thank you for this though, I ended up using it with a few minor tweaks. The numbers on the left are my directory names from my example data. Sorry that was confusing. Also, there's no guarantee that the name returned by ls can be passed to find , as ls escapes non-printable characters for human consumption. See Why you shouldn't parse the output of ls 1 — Charles Duffy. From man locate -c, --count Instead of writing file names on standard output, write the number of matching entries only.
Additional , you should know that it also counts the directories as files! BTW: If you want an overview of your files and directories on your system type locate -S It outputs the number of directories, files, etc. LOL if you have all the counts in a database already, then you can certainly count quickly. Benubird Benubird Brew install tail for OS X. We can find the C code to list the files using getdents from here : There are two modifications you will need to do in order quickly list all the files in a directory.
First, increase the buffer size from X to something like 5 megabytes.
0コメント