 Today I'm just going to go over a command that was shared with me. I have a viewer who's a Patreon supporter of mine. His name is Mr. Newby. And the other day in a chat room that we hang out in, he decided he just posted a command that he came up with. I guess he was trying to count the number of files in folders and subfolders in a directory, and he was just sharing what he came up with. So I'm going to show you his command, show you the output, then we're going to walk through it step by step and look at one or two alternate changes to it. So let me go ahead and start in here. First I'm just going to grab the whole command and run it. So I'm in my music directory. And if I list out the files in here, you'll see I've got blue things. The darker blue is directories and then the lighter blue, the teal color is individual files. So I want this command. What's going to do is going to look at each, every single file in this directory and subdirectories and give me a count of the number of files in each of the directories listed here. So I'll run that and you can see here, we got miscellaneous nine inch nails, D music, which is my playlist for when I'm playing doom, which will have some nine inch nails and some other stuff in there. I got synth wave here and it's telling you how many files are in each of those directories and in the subdirectories of those directories. So it's nicely output. So let's go ahead, clear the screen here and we will look at this command step by step. First we're going to look in the current directory. So we have find and then dot will say current directory and really you don't need the dot. If you remove that, it's going to automatically go for the current directory. And then we're going to say type file. So it's going to ignore directories. I mean, it will look in directories, but it's only going to return a list of files. I guess if you wanted to count the number of directories in the subdirectories included with this, you could remove that or change it to I think D for directories. So anyway, that's going to list everything in all these directories. And for an example here, we're going to go one, two, three, four, five, six, seven, eight, nine files inside my back folder. So I have nine songs in that directory. Let's go ahead and move on to the next part of this command. So the next thing he did. So when we did this, we want the name of each directory, which would be in between the first and second slash. So what he did was he used Ock to print and what it did. Let me go ahead and paste it here. He says Ock and he's going to use the forward slash as a delimiter and then print everything in the second column. That's great. That works great. I tend to use the cut command more. So you could change this and I'm not saying one's better than the other. Just in my mind, I use the cut command a lot. And I can say dash delimiter and I'm going to say the forward slash and then I can say dash F2. It outputs the same thing. And again, if we count the names, how many times back that folder shows up here, it will be nine. So what we want to do now is sort to make sure everything's in order and then we're going to count how many unique things there are for each one. So what do we mean by that? Let's go ahead and now pipe this into sort, which will sort it alphabetically and then we will take that and we'll pipe that into unique. Now sort has a built-in unique, but we're not just uniting it. We are counting the unique. There might be a way to do this straight with sort, but we're saying unique dash C and that's going to give us a count for each thing. For example, in my They Might Be Giants folder, there were 11 unique things. So now we have the number. We're pretty much done. We've gotten what we want, but we want to sort it so that when there's more files in a directory, it's going to be at the bottom and less files are at the top. So we can now take this and pipe that again into sort. And when I do that, it doesn't quite work right because look, so it's sorting us goes, oh, three is before four. So here's four, but then it's saying 53. And because it starts with a five, it's putting it here. We don't want that. I did a whole video on this a while ago, but if you just add to the sort command dash N, that's going to look at those as if they're numbers. So it's going to look at that 53 as a 53 and not just that it starts with a five. There we go. We're pretty much done except for he had one more command in there which removed this leading white space. And the command he used, he used awk again. And so we could pipe this into awk. And here it's saying print number one. It's going to look for spaces. And then it's going to print the output of that. I don't know. I mean, I could look at this and probably figure it out, but I haven't looked at this exact command. Let's put that all online here real quick. So all that's really doing is removing that leading white space. So I can run that and it does it. I quickly, I knew that's all it was doing. So I googled how to remove leading white space. And I came up with a separate awk command. So if I was to take this and erase what he put, as far as I can tell, this is the same exact thing. It's just a little bit shorter. So I can run that. And now we have it. So where there's no leading white space. And we can see how which folders have more directory or which folders have more files in them. If I was to go up to the top, it does list individual files as one. And then as we go down, you'll see if it doesn't have that extension on there that these are folders. Some of my folders only have one file in them. And then some of them have almost 200. So that's it for the command. And again, this is a command you can run. But there might be a tool out there. If you know of a command that might be in the repositories for Linux distributions that does this already, let me know below. I haven't even really looked that up. There could be something that already does this, but he shared this. And I thought it was just a good little example to go through that I could walk through what it means. And again, people go, oh, when I show commands like this, people go, oh, that's so long. Why would anyone use the shell? Why does anybody use Linux if you have to use this? Well, the thing is, that's what aliases and scripts are for. So I could actually take this command and I can type in alias, and I can say file count equals and then quotation marks put this. And then since there's dollar signs in here, I'm just going to backslash these real quick. And now I don't have to type out the command. I can just type file count, and it's going to count the files. It's going to run that command. And to make that permanent, you put it in your configuration file, your RC file for your shell. So again, I can go file count, and it runs all that command. So you don't need to know all that. And that's what scripting is for. It's writing out scripts. You're not going to write out a script if you're going to do something once. You're going to write out a script that does something repetitively. So here we have a string of commands, and we just put in alias, or you could put it in a script file or in a function. And those all have different uses in there. But yeah, so that's it. Again, it's just a little overlook, something I haven't needed. But if I ever need it, now I know how to do it. Now you know how to do it. I'll put a link in the description to a paceman with that string on there. And you can play around with it. If you have a better way of doing it, a way of shortening up something that is easier to read, or maybe know of a program that does this already that hopefully is already in the repositories. But if not, you know, just share what you know below. I would love to see if you have something else that does something similar to this. Thanks for watching. Please visit filmsbychris.com. That's Chris of the K. There's a link in the description. Again, this was shared with me by Mr. Newby, who's one of my Patreons. Patreons, one of my patrons on Patreon. So check out the link in the description for my Patreon page, patreon.com, forward slash metalx1000. You can support me there. And sometimes it's hard. I get a lot of questions. I have a lot of subscribers, but my patrons, my supporters come first. And if you become a patron and you want to hang out and chat with me and Mr. Newby, let me know and I can get you into the chat room we hang out in. And we can just talk and share stuff like this regularly. And I thank you for watching filmsbychris.com. I think I already said that. There's a link in the description. I hope that you have a great day.