List number of files in a particular folder – Linux, Counting Files and Directories

If you want to find out how many files there are in a folder (and subfolders), below are 5 possible solutions, suitable for any need !

1. list files and folders using du linux command:

[root@server1]# du -a /folders/test | cut -d/ -f2 | sort | uniq -c | sort -nr

17616 data

2. list files and folders using ls and grep linux command

[root@server1]# ls -Ra1 /folders/test|grep -v /|grep -vx ""|grep -vx "\.*"|wc -l

17731

3. count files using find and print linux command:

[root@server1]# find /folders/test/ -name "*.*" -print | wc -l

17681

4. count files, links and folders:

[root@server1]# for t in files links directories; do echo `find /folders/test/ -type ${t:0:1} | wc -l` $t; done 2> /dev/null

17570 files
0 links
24 directories

5. count only files:

[root@server1]# for t in files; do echo `find /folders/test/ -type ${t:0:1} | wc -l` $t; done 2> /dev/null

17556 files

Note:

to count all files, not just files with extension, instead of
*.*
or
\.*
just use *

byrev Written by:

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *