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
ls -F |grep -v / | wc -l
ls -Flist all files and append indicator (one of */=>@|) to entriesgrep -v /keep all de strings that do not contain a slashwc -lcount lines
- 7,742
Try this
ls -al | grep ^[-] | wc -l
ls -al-- list all file with long listing formatgrep ^[-]-- search for string which start with "-" that is symbol for denote regular file when list file with ls -alwc -l-- count lines
- 81
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:
echo double-quoted-messagewill print a desirable message.$(any-desirable-valid-command)inside the double quoted message of anechowill print the result of related command execution.pwdwill print the current directory.ls -Fis for listing all files and append indicator (one of */=>@|) to entries. I copied this from thom's answer.grep -v /is a command for searching plain-text, the-v /parameter will keep all the strings that do not contain slash(es).wc -lwill print line counting.
I know this question is 3 years old, I just can't hold my urge to add another answer.
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.
- 354
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.
- 137
To count total number of files with specific extension you may type:
ls|grep jpg |wc -l
- 39,359