462

To count the number of files in a directory, I typically use

ls directory | wc -l

But is there another command that doesn't use wc ?

jww
  • 97,681
  • 90
  • 411
  • 885
Kantura
  • 5,763
  • 5
  • 18
  • 17
  • 27
    What exactly is the problem with `wc` that prevents you from using it? – vanza Jan 03 '14 at 02:04
  • 6
    Not really. Unix commands are generally intended to be used this way, chained in pipes. – Michael Berkowski Jan 03 '14 at 02:04
  • 3
    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 . – Kantura Jan 03 '14 at 02:25
  • 10
    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`. – Elijah Lynn Mar 03 '15 at 21:20
  • 10
    @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. – JamesThomasMoon Jun 26 '15 at 05:57
  • 1
    @vanza **UPDATE** files with `\n` in the name are displayed on one line by `ls` . The `\n` is replaced with `?` . Interesting. – JamesThomasMoon Jun 26 '15 at 06:02
  • 1
    Possible duplicate of https://stackoverflow.com/questions/11307257/is-there-a-bash-command-which-counts-files – tripleee Nov 27 '17 at 18:48
  • 2
    If you want to include subfolders files by counting recursively: https://stackoverflow.com/a/9157162/1176454 – baptx Jan 31 '19 at 11:33
  • 1
    Remember the **-R** for recursively – PYK Sep 14 '20 at 17:14
  • Linux requires a professional command/switch to perform this function. – user2585501 Oct 18 '21 at 03:16
  • 1
    closed for being off-topic, and it has over 400 upvotes, lol. I wonder what the highest voted CLOSED question is. – Joe Jan 29 '22 at 15:35

1 Answers1

720

this is one:

ls -l . | egrep -c '^-'

Note:

ls -1 | wc -l

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

|: pipe output onto...

wc: "wordcount"

-l: count lines.

glennsl
  • 28,186
  • 12
  • 57
  • 75
Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
  • thanks Sajad Lfc , but the egrep command returns a 0 while the wc command returns the correct number of files . Also the wc command works without the -1 included . – Kantura Jan 03 '14 at 02:19
  • 1
    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 . – Kantura Jan 03 '14 at 02:31
  • 1
    @SajadKaruthedath `ls -l . | egrep -c '^-'` does not count hidden files. I suggest adding `-a` flag to `ls`. – JamesThomasMoon Jun 26 '15 at 05:59
  • 1
    @JamesThomasMoon1979: please check my description – Sajad Karuthedath Jun 26 '15 at 06:18
  • 1
    This shows error when there are no files in the folder. Use `ls -1 /dir 2>/dev/null | wc -l`. Here the output from ls is directed to /dev/null and the result is a clean zero – Sathish Dec 16 '16 at 09:18
  • 1
    ls -l | wc -l shows an extra file on Mac. I have 33 files counted manually and also using egrep. wc -l shows 34 instead! – runios Feb 11 '18 at 16:47
  • 13
    @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` – Daniel Biegler Mar 07 '18 at 11:02
  • 1
    A weird favourite of mine: `find . -type f -printf "." | wc -c` – Luke H Aug 30 '19 at 13:52
  • 4
    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]# – Venfah Nazir Jan 09 '20 at 07:28
  • Why is there a hyphen `-` at the end of the patter in `egrep -c '^-'`? – zardosht May 03 '20 at 19:57
  • 8
    Isn't this one against the tradition of not parsing `ls`? – Minh Nghĩa May 14 '20 at 12:55