57

Simple question I'm sure. I've seen an answer that show how to do it including subdirectories, but I want to know how many files (not folders) are in the current directory only. Thanks.

7 Answers 7

97
ls -F |grep -v / | wc -l
  1. ls -F list all files and append indicator (one of */=>@|) to entries
  2. grep -v / keep all de strings that do not contain a slash
  3. wc -l count lines
3
  • List of everything except directories.
    – thom
    Nov 3, 2013 at 23:56
  • 1
    I really appreciate you breaking it out and explaining the sections, thank you for a working and well explained answer!
    – bcsteeve
    Nov 4, 2013 at 1:10
  • 1
    While all answers solve my problem, I'm choosing this one as its documented and easiest for me to understand. But thanks to everyone!
    – bcsteeve
    Nov 4, 2013 at 1:12
26

Try this oneliner:

find -maxdepth 1 -type f | wc -l
3
  • This is good, only files and nothing else :-)
    – thom
    Nov 3, 2013 at 23:55
  • This skips links and special character devices.
    – ojblass
    Nov 4, 2013 at 0:48
  • All answers here work great! So first, THANK YOU. Second... how do I choose?
    – bcsteeve
    Nov 4, 2013 at 1:09
7

Try this

ls -al | grep ^[-] | wc -l
  1. ls -al -- list all file with long listing format
  2. grep ^[-] -- search for string which start with "-" that is symbol for denote regular file when list file with ls -al
  3. wc -l -- count lines
4
  • What about soft links? What about hard linked files?
    – user85164
    Nov 3, 2013 at 23:23
  • Nice one, only files and links, nothing else.
    – thom
    Nov 3, 2013 at 23:58
  • For soft/hard link jut replace - by l
    – andr3w
    Nov 4, 2013 at 0:05
  • I really appreciate you breaking it out and explaining the sections, thank you for a working and well explained answer!
    – bcsteeve
    Nov 4, 2013 at 1:11
3

I just want to add thom's answer because I like to play with Bash. Here it goes:

echo "Directory $(pwd) has $(ls -F |grep -v / | wc -l) files"

Bellow is an example result of my /data directory:

Directory /data has 580569 file(s).

And bellow are my explanations:

  1. echo double-quoted-message will print a desirable message.
  2. $(any-desirable-valid-command) inside the double quoted message of an echo will print the result of related command execution.
  3. pwd will print the current directory.
  4. ls -F is for listing all files and append indicator (one of */=>@|) to entries. I copied this from thom's answer.
  5. grep -v / is a command for searching plain-text, the -v / parameter will keep all the strings that do not contain slash(es).
  6. wc -l will print line counting.

I know this question is 3 years old, I just can't hold my urge to add another answer.

3

If you have tree installed on your system you can use this command:

tree -L 1 /path/to/your/directory | tail -n 1

It shows you the number of files and directories in that directory.

-L n shows the depth of search.

You can install tree with sudo apt-get install tree.

0

Pure bash, no pipes, no subshells, no external executables:

_c() { printf '%d\n' $#; }
_c *
_c *.sh

Will fail with error if there are no (matching) files, which bash can avoid with shopt -s nullglob.

-3

To count total number of files with specific extension you may type:

ls|grep jpg |wc -l

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.