Linux (commonly pronounced LIH-N?ks in English; variants exist) is a Unix-like computer operating system. Linux is one of the most prominent examples of free software and open source development: typically all underlying source code can be freely modified, used, and redistributed by anyone.

Tags | |

Count files in a directory

So the problem is that you have a script that needs to count how many files are in a directory in order to perform a function, but how do you do it"




ls -1|wc -l


That will give you the number, but it's also counting any folders, we just want files.




ls -R1 /path|grep -v /|grep -vx ""|grep -vx "\.*"|wc -l



If we wanted to include hidden files we would use:




ls -Ra1 /disk_array/backups/|grep -v /|grep -vx ""|grep -vx "\.*"|wc -l


If we want to exclude directory in that path from being counted:




ls -Ra1 /disk_array/backups/|grep -v /path to exclude|grep -v /|grep -vx ""|grep -vx "\.*"|wc -l



Comments

Feel free to leave a comment or question

Name: (Optional but appreciated):

Comment:

Use [code] [/code] for code block style



Security Code: