Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Recursively counting files in a Linux directory Ask Question. Asked 9 years, 11 months ago. Active 8 months ago. Viewed k times.
How can I recursively count files in a Linux directory? Improve this question. Robert Buckley Robert Buckley 9, 6 6 gold badges 23 23 silver badges 24 24 bronze badges.
SkippyleGrandGourou Isn't it called a pipe? DaveStephens Yes, it's also called that. It's also called a Sheffer stroke, verti-bar, vbar, stick, vertical line, vertical slash, bar, obelisk, glidus. In RFC20 it's called "vertical line". Show 1 more comment. Active Oldest Votes.
You can also remove the -type f to include directories and symlinks in the count. It's possible this command will overcount if filenames can contain newline characters. Improve this answer. Felix 3, 2 2 gold badges 26 26 silver badges 41 41 bronze badges. Remove the -type f to include directories in the count — phatblat. Is there a faster method?
If there is any possibility that file names contain the newline character you might want to use the -print0 flag. See my answer for an alternative. If your files have newlines in them, you can still use find to do it by using an -exec instead of a print: find.
In this way, you are not actually outputting the filenames, but you are outputting a single blank line per file encountered, regardless of the name, so the line count will work in any case.
In this case, tr deletes all non-null characters and wc counts the characters fed into it. Show 10 more comments. For the current directory: find -type f wc -l. Abhishek Maurya Abhishek Maurya 1, 1 1 gold badge 13 13 silver badges 12 12 bronze badges. This solution does not take filename that contain newlines into account. For the current directory, you don't even need the. Actually, on some platforms, you do need to spell out find.
Kusalanandra Your comment applies to almost every answer here. Add a comment. Greg Bell Greg Bell 1, 14 14 silver badges 16 16 bronze badges. It could get stuck on directories with spaces in their names. Changing the first line to find. Using find for the outer loop is just a needless complication.
Finally this is what I needed. My folders have thousands of files so printing them with tree or anything else is not an option — lesolorzanov. This includes.. Remarks: the first line is a count of files, directories, symlinks, etc all together, that's why it is bigger than the second line. Slava Fomin II I like your idea of using rsync here. I'd never have thought about it!
Thanks Qeole, the idea is not mine though. I read it several years ago somewhere that rsync is the fastest to delete a folder with lots of files and subfolders, so I thought it might be quickly to count files as well. Tried this. That's for about , files on SSD. Dunno what version of rsync you used, but in 3.
I used the default rsync on macOS: rsync version 2. Hidden files can be included too by supplying the -a option. This is actually the simplest way to see number of directories and files. From the man page: By default tree does not print hidden files. You have to supply the -a option to include them. To install this on macOS, use brew and run brew install tree , preferable after running brew update. It's also printing all the filenames, so it will be slow if you have many files.
Wow, very nice tool, it can print folders colorized, list only folders, output as JSON. It can list 34k folders and 51k files in very few seconds. Show 2 more comments. Looks like this is the only solution that handles files with newlines in their names. That makes them just more readable. I mean, newlines in the file names not the content!
I was just joking Yeah, newlines in filenames always have to be taken into account. They could come from malicious content or less spectacular, from a typo. This will not work for every find. Combining several of the answers here together, the most useful solution seems to be: find. Martin 2, 21 21 silver badges 22 22 bronze badges.
Sebastian Meine Sebastian Meine What's with the echo -e? I guess you put it in to fold any newlines, but it will also mangle any other irregular whitespace, and attempt to expand any wildcard characters present verbatim in the file names. I'd go simply with something like find. If you want to know how many files and sub-directories exist from the present working directory you can use this one-liner find.
Santrix Santrix 6 6 silver badges 10 10 bronze badges. Excellent solution! The only issue I found was directories with spaces or special characters. Add quotes where the dir name is used: find. I've modified it a bit and it works quite well for me: find. My comments on Sebastian's answer apply here too. Franck Dernoncourt Franck Dernoncourt Any file manager supports all the basic functions like searching, copying, moving, creating, and deleting files. Some file managers even support advanced features like SSH connections.
Here are some of the best file managers available for Linux. Most of them should be available for all the popular Linux distros.
It has a very simplistic UI, easy navigation, and management. Check out Nautilus File Manager. Check out Konqueror. It allows users smooth browsing, locating, copying, and moving files experience around the Linux system. It incorporates other interesting features like file previewing, tabbed navigation, file sorting, and grouping, etc. It features bash integration, built-in VFS, and menu customization, etc. It is a full-fledged file manager but on the console screen.
It supports all the classic functions like searching, copying, moving, and deleting files, etc. All it requires is having the right tool and knowledge to figure out. Hopefully, this guide was successful in demonstrating how to count files in directories recursively in Linux. Student of CSE. I love Linux and playing with tech and gadgets.
0コメント