Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
yeah, i just wanted to be able to count how many there are thats all. it seems like this is actually kind of difficult to do. especially if you have a complex directory structure. because it counts the directories also... hmm. there has to be a simple way, this is such a basic need. thanks for the help guys.
The first character on the line will indicate the file type. A normal file will have a dash "-" character. If you want to count links, then use "grep '^[-l]'" which will include either regular files or links. However, symbolic links will start with an "l" character for both files and directories.
ls -la | grep '^[-l] | grep -v '\/$' | wc -l
This will exclude links to directories because they end with the forward slash character.
Or you could use the "--classify" option of ls so all directories have the "/" character appended to the name. This method will not count the "." and ".." entries.
ls -a --classify | grep -v '\/$' | wc -l
The "-v" option of grep will exclude lines that contain the patter. The pattern in this case is any line ending in a forward slash. Earlier, I used the pattern '^[-l]'. The "^" carat character in a pattern indicates the beginning of a line. The "$" character in a search pattern indicates the end of a line.
i think "ls -laR | grep '^[-l]' | grep -v '\/$' | wc -l" works best for my purposes! thats a big help. im surprised its complex to do get something as simple as a directory file count. thanks!
A problem with the above script is that it will count files in subdirectories also. You need to use the "maxdepth" option.
Code:
find ./ -maxdepth 1 -type f | wc -l
In a similar vein, you might also want to use the "-d" option for the ls command if you are using wildcards, such as counting files starting with an "s"
However, for complicated searches, the "find" command works best.
You can even count the number of regular files, owned by a particular user, larger than a certain size, that was modified in the last 8 hours. OK, this example is a bit contrived, but searching your system for "suid" files is a very common use.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.