Linux has powerful set of shell commands using which you can almost achieve anything. Specially combining commands through pipes and redirection can make them more stronger.

If you need to find the total file count in a directory in Linux ( CentOS / RHEL etc ) then you can do this using following command :

find . -type f | wc -l

or :
find ./ -type f | wc -l

Both commands will have same affect and will count all the files in all sub directories in the current directory. The parameter ‘-type f’ makes sure that ‘find’ command is only searching for files and not including directories in the count. The output is then piped ( passed ) to next command ‘wc’ called ‘word count’ and using -l or –lines parameters we can get the count for any listing. wc command has other parameters using which you can find byte count, char count etc. Check command help or man pages for detail syntax and usage.

If you just want to know the count of files in the current directory without including any subdirectories then you can achieve this using below command :

ls -l | wc -l

These commands are also helpful in general scripting and can help achieve desired logic in the scripts.

Tags: , , , , ,

Leave a Reply