Bash file count




















The filenames are treated as strings, not lines, so embedded newlines are not an issue. It is conceivable that this approach could have a problem if the directory is huge, because the list of filenames must be held in shell memory. Here, we make a small change in that:. This printf command will output only a newline i. And, again, you can include the. The only issue remaining is to count the files.

Yes, grep is an usual solution, and yes counting new lines with wc -l is also an usual solution. Note that grep -c count really counts how many times a snp string is matched, and, if one file name has more than one snp string in the name, the count will be incorrect.

To avoid changing the positional arguments we can transform each argument to one character and print the length of the resulting string for most shells :.

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. Count files in directory with specific string on name? Ask Question. Asked 6 years, 8 months ago. Active 3 years, 4 months ago. Viewed 85k times. I tried using grep -a 'snp' wc -l but then I realized that grep searches within the files.

Improve this question. This command shows the directory structure and then displays the summary at the bottom of the output. As you can see in the output, it shows that there are 7 directories and 20 files in total.

So far, all the solutions we have seen for counting the number of files, also take directories into account. Directories are essentially files but what if you want to count only the number of files, not directories? You can use the wonderful find command. The above command searched for all the files type f in current directory and its subdirectories.

But what if you want to count the number of files in the current directory only excluding the files in the subdirectories? You can use the same command as above but with a slight difference. In Linux, you can have multiple ways to achieve the same goal. I am pretty sure there can be several other methods to count the number of files in Linux. If you use some other command, why not share it with us? Please enter at least 3 characters 0 results found. Abhishek Prakash. Here are several ways to count the number of files in a directory in Linux command line.

Table of Contents. Count number of files and directories including hidden files You probably already know that -a option of ls command shows the hidden files. Count number of files and directories including the subdirectories What you have see so far is the count of files and directories in the current directory only. Count only the files, not directories So far, all the solutions we have seen for counting the number of files, also take directories into account.

Also, the final shopt -u nullglob should be skipped if nullglob wasn't unset then you started. If the files you wish to enumerate have the traditional naming convention of name. An explanation of what the [ ] means would be helpful — Andy Preston.

Note that shopt -s nullglob would cause ls to list the entire working directory instead. Nevertheless, the original answer is extremely helpful. For a recursive search: find. For a non-recursive search, do this: find. Will Vousden Will Vousden Even if you don't have files with spaces, some other user of your script might encounter a maliciously named file, causing the scripts to fail.

Also, other people encountering this on StackOverflow might have files with newlines, and need to know the pitfalls. Also the -maxdepth flag is extremely useful, thanks! This still produces incorrect results if there are file names with newlines in them. The workaround is easy with find ; just print something else than the verbatim file name.

The accepted answer for this question is wrong, but I have low rep so can't add a comment to it. Dan Yard Dan Yard 1 1 silver badge 5 5 bronze badges. But, the shopt nullglob answer is the best answer! You probably should update your original answer instead of answering again.

I think using find vs using ls are two different ways of solving the problem. But then a box of lard which doesn't have find probably doesn't have all those fancy options for ls either. Note also how this extends to a whole directory tree if you take out the -maxdepth 1 — tripleee.

Note this solution will count files inside hidden directories in its count. This can create confusion if one doesn't realize there's a hidden child folder, and may make it advantageous to use ls in some circumstances, which does not report hidden files by default.

Here is my one liner for this. It took me some googling to understand, but this is nice! Small Boy Small Boy 87 1 1 silver badge 7 7 bronze badges. S Moh. S 1, 17 17 silver badges 19 19 bronze badges.

Explanation: github. Shuang Liang Shuang Liang 11 1 1 bronze badge. Here is a generic Bash function you can use in your scripts. Theodore R. Smith Theodore R. Smith But you might want to hide ls error message if no file matches the pattern.

The discussion under Daniel's answer is relevant here too. This works fine when you don't have matching directories or file names with newlines, but a good answer should at least point out these boundary conditions, and a great answer should not have them. Granted, many more bugs happen because they ignored the caveats and then things changed after they thought the code was probably good enough for their purpose. To count everything just pipe ls to word count line: ls wc -l To count with pattern, pipe to grep first: ls grep log wc -l.

Sign up or log in Sign up using Google. Sign up using Facebook.



0コメント

  • 1000 / 1000