user7437554
user7437554

Reputation:

Count the number of a particular kind of files from console

I want to count the number of some xyz files from console.

I have an arrange like this:

Folder-A

Folder-B

Folder-C

All of them have xyz files inside.

I need the command to return the number of xyz files and the location of it.

For example: /home/path/to/A 25; then for B and so on.

Could it be possible? I thought in using find -name *.xyz, but do not know what to use next.

Any help?

Upvotes: 1

Views: 72

Answers (3)

user1934428
user1934428

Reputation: 22366

Adapting the solution by @tso:

for d in Folder-A Folder-B Folder-C
do
  find $d -type f -name '*.xyz'
done | wc -l

Getting separate counts for each folder:

for d in Folder-A Folder-B Folder-C
do
  echo $d $(find $d -type f -name '*.xyz' | wc -l)
done

Upvotes: 0

tso
tso

Reputation: 4934

find FolderA FolderB FolderC -type f -name '*.xyz' | wc -l
  • . where start search (. - from current directory)
  • Use -type f to include only files (not links or directories).
  • wc -l means "word count, lines only." Since find will list one file per line, this returns the number of files it found.

Upvotes: 1

blue112
blue112

Reputation: 56582

for i in FolderA FolderB FolderC
do
    echo -n $i :
    find $i -type -f -name '*.yzx' | wc -l
done

That should do the trick.

echo -n prints a line without the line return, so it should print something like:

Folder A: 17
Folder B: 0
Folder B: 4

Upvotes: 1

Related Questions