Parsing the output of ls always makes me uneasy, but here it should work as long as your file names don't contain newlines, or your ls escapes them. Any of those solutions that rely on passing the expansion of a glob to ls may fail with a argument list too long error if there are a lot of matching files.
The option -L is used for specifying the maximum display level of the directory tree. The tree command does not only count the number of files, but also the number of directories, considering as many levels of the directory tree as you like.
Here's another technique along the lines of the one Gilles posted :. On Linux, to make the command very robust and handle files that might have newlines in their name, use this:. This saves us from the ordeal of parsing ls output. It's worthy to notice the use of some loved commands like tail and cut. Also, note that tree is not available by default. The command above first capture information about the directory at level 1, then get the last line tail -n 1 where our goal is, and end up with cut to take the third word.
If you have rights to install packages, there is a very simple tool to do this and more. It is called ncdu and it can be installed using apt or yum. A basic usage of ncdu would be:. This will display an ncurses-based screen which you can navigate using cursor keys. At the bottom, initially you will see the total number of files in that directory and subdirectories.
A bonus of ncdu is that it gives a progress bar while scanning. You can also redirect the output to a file for later use. In the man page, there is an interesting section on how hard links are handled across various versions of ncdu. Sign up to join this community. The best answers are voted up and rise to the top.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. How can I get a count of files in a directory using the command line? Ask Question. Asked 11 years, 2 months ago. Active 2 months ago. Viewed k times. Improve this question. Blake Blake 3, 2 2 gold badges 14 14 silver badges 8 8 bronze badges. Add a comment. Active Oldest Votes. Using a broad definition of "file" ls wc -l note that it doesn't count hidden files and assumes that file names don't contain newline characters.
Improve this answer. James James 3, 1 1 gold badge 14 14 silver badges 4 4 bronze badges. The -l switch causes it to count lines. In this case, it's counting the lines in the output from ls. This is the always the way I was taught to get a file count for a given directory, too. An empty directory will still return 1 line. Mureinik Mureinik k 49 49 gold badges silver badges bronze badges.
The comment itself worked good, thanks. But Im a bit confused. I tested it with a NodeJs modules folder with a bunch of subdirectories. How can this be possible? Open the terminal and switch to the location of the directory.
Type in: find. Max Brandt Max Brandt 4 4 bronze badges. Waxhaw Waxhaw 1 1 silver badge 6 6 bronze badges. But OP is not located in the images directory, they need a way to find the directory in addition to counting the images.
That's why they use find and not ls. Otherwise, sure, you have a point. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. Save my name, email, and website in this browser for the next time I comment. This site uses Akismet to reduce spam. Learn how your comment data is processed. All rights reserved.
Any material cannot be used without our explicit consent for online and offline purposes. Monitoring Linux Processes using Prometheus and Grafana. How To List Disks on Linux. How To Add Route on Linux. How To Encrypt Partition on Linux.
0コメント