razzor1
razzor1

Reputation: 23

Count only visible files in directory

I'm having problem with hidden file in my directory. If I use $(find . -type f | wc -l) it shows 8 files, which counts hidden file too, there should be only 7 files.

Is there anything that could count only visible files?

Upvotes: 2

Views: 2712

Answers (3)

PradyJord
PradyJord

Reputation: 2160

Exclude all files starting with ( . )

find ./  ! -name '\.*' -type f | wc -l

! simply negates the search

If that doesnt work then try this dirty looking solution:

ls -lR | egrep '^(-|l|c|b|p|P|D|C|M|n|s)'  | wc -l

Listed all types of files there excluding directories. You can find the type of files in linux here

without -R of you want to look only in same dir.

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77155

Ignore the names that start with . by saying:

find . ! -name '.*' -type f | wc -l

From the man page:

! expression

 -not expression
         This is the unary NOT operator.  It evaluates to true if the
         expression is false.

If you have filenames with newlines, then you can do using gnu find (as suggested by gniourf gniourf in comments):

find . ! -name '.*' -type f -maxdepth 1 -printf 'x' | wc -c

Upvotes: 3

Manuel Selva
Manuel Selva

Reputation: 19050

find . -type f -not -path '*/\.*' | wc -l

-not -path allows you to ignore files with name starting with . (hidden files)

Upvotes: 0

Related Questions