Timeline for How can I get a count of files in a directory using the command line?
Current License: CC BY-SA 3.0
15 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 1, 2024 at 0:00 | comment | added | calestyo | Fails as soon as a file contains a newline. | |
| Jul 15, 2020 at 18:18 | comment | added | user62612 | "ls -1 | wc" counts files. dash-one (-1) flag for 'ls' is an easy way to get a single file per line. | |
| May 17, 2020 at 2:26 | comment | added | DARKGuy | Wow I can't believe that something so easily done in Windows with "dir" can be so cumbersome with Linux :| | |
| Jan 16, 2020 at 23:30 | comment | added | Brian Peterson | How does this work? I need to know how to modify it slightly / combine recursion with including hidden files. Even a link would be nice. | |
| May 14, 2015 at 14:22 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | hijacking the accepted answer to include the canonical way, sorry |
| May 14, 2015 at 14:01 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | deleted 112 characters in body |
| May 14, 2015 at 13:48 | history | edited | Stéphane Chazelas | CC BY-SA 3.0 | added 219 characters in body |
| Mar 3, 2015 at 22:30 | comment | added | godlygeek | 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. | |
| Mar 3, 2015 at 22:20 | comment | added | godlygeek | If you have a file whose name contains a newline, this approach will incorrectly count it twice. | |
| Sep 24, 2013 at 2:16 | comment | added | James | An empty directory returns 0 for me | |
| Aug 25, 2010 at 15:14 | comment | added | warren | that doesn't get everything in a directory - you've missed dot files, and collect a couple extra lines, 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 . and .. entries. You'll still be off-by-one, however. | |
| Aug 24, 2010 at 19:09 | vote | accept | Blake | ||
| Aug 24, 2010 at 16:47 | comment | added | Lesmana | please add note that ls does ls -1 if the output is a pipe. | |
| Aug 24, 2010 at 6:07 | comment | added | Sandy | wc is a "word count" program. 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. | |
| Aug 23, 2010 at 21:56 | history | answered | James | CC BY-SA 2.5 |