Just how can I get a matter of documents in a directory site making use of the command line?

I have a directory site with a lot of documents. I do not see a ls button to give the matter. Exists some command line magic to get a matter of documents?

0
2019-05-05 19:43:18
Source Share
Answers: 5
ls -1 | wc -l
.

...

$ ls --help | grep -- '  -1'
    -1                         list one file per line
.

...

$ wc --help | grep -- '  -l'
    -l, --lines            print the newline counts
.

PS : Note ls - | wc -

0
2019-05-08 11:55:41
Source

Here's an additional strategy along the lines of the one Gilles posted :

word_count () { local c=("[email protected]"); echo "${#c[@]}"; }
file_count=$(word_count *)

which develops an array with 13,923 components (if that's the amount of documents there are).

0
2019-05-08 08:48:21
Source

Using a wide definition of "documents"

ls | wc -l

(note that it does not count surprise documents and also thinks that documents names do not have newline personalities).

To include surprise documents (other than . and also ..) and also stay clear of troubles with newline personalities, the approved means is :

find . ! -name . -prune -print | grep -c /

Or recursively :

find .//. ! -name . -print | grep -c //
0
2019-05-08 08:38:47
Source

For slim definition of documents :

 find . -maxdepth 1 -type f | wc -l
0
2019-05-08 07:59:47
Source

If you recognize the existing directory site has at the very least one non - surprise documents :

set -- *; echo "$#"
.

This is clearly generalizable to any kind of chunk.

In a manuscript, this has the occasionally unfavorable negative effects of overwriting the positional parameters. You can function around that by utilizing a subshell or with a function (Bourne/POSIX variation) like :

count_words () {
  eval 'shift; '"$1"'=$#'
}
count_words number_of_files *
echo "There are $number_of_files non-dot files in the current directory"
.

A different remedy is $(ls -d -- * | wc -l). If the chunk is *, the command can be reduced to $(ls | wc -l). Analyzing the result of ls constantly makes me worried, yet below it needs to function as long as your documents names do not have newlines, or your ls leaves them. And also $(ls -d -- * 2>/dev/null | wc -l) has the benefit of taking care of the instance of a non - matching chunk beautifully (i.e., it returns 0 because instance, whereas the set * method calls for fiddly examining if the chunk could be vacant).

If documents names might have newline personalities, a choice is to make use of $(ls -d ./* | grep -c /).

Any one of those remedies that rely upon passing the development of a chunk to ls might fall short with a argument checklist also lengthy mistake if there are a great deal of matching documents.

0
2019-05-08 07:41:52
Source