 One of the things about doing things at the command line in the terminal on Linux is that many of the commands that I run I Could probably run at least half a dozen other commands to achieve the same result It's one of those things so many things at the command line can be done Using a variety of different tools one example is getting a line count You guys often see me getting a line count of for example packages installed on a Linux system When I'm doing a distro review. Let me switch over to my desktop and I'll show you exactly what you guys will often see me do I'll open up a Linux distribution in a virtual machine For a review and I want a an exact number of how many packages are installed on that system And on a Debian or an Ubuntu based system You would run the command apt space list space dash dash installed and that will spit out Line by line every program that was installed using the apt package manager on your system now I use a arch base distribution so on pac-man to get that information You would do pac-man dash capital Q lowercase Q and it will spit out again line by line Every package that was installed using pac-man the package manager now if I wanted a Line count of this all I would do is pipe that into WC Which is the word count program part of the standard GNU core utils and give WC the dash L flag for a line Count because I want a line count not necessarily a word count and you see 2004 lines are in that output So I know that through pac-man 2004 packages were installed on my system now the other day. I ran this Command on Linux mint. I was taking a look at the most recent release of Linux mint and I ran the apt list dash dash installed command and piped it through WC dash L to get a line count and I got a response in the comments. Hey, man Why didn't you use the nl command to get a line count because it works just like WC? It is simply you don't have to add a dash L flag I guess you know saving a couple of characters maybe is an advantage It's up. The problem is WC dash L and nl are not actually the same thing If I actually do pac-man dash capital Q lowercase q and then pipe it into nl Which is number of lines that's a standard GNU core utility as well what this does It doesn't actually spit out the number of lines what this does it cats out The output and gives a line number in front of every line So that gives me you know 2004 lines of information where I didn't need 2004 lines of information. I just needed the very last line and I didn't even need the entire last line I just needed the actual Number and that's where you know WC dash L comes in handy because the dash L flag Specifically says give me the number where WC if you didn't give it the dash L flag It actually prints out more information. It gives you the line count It gives you the word count which is the same in the output because every line is exactly one word because it's a package name And then it gives you a bite count a character count the cool thing with WC though is it has the flags So if I only want a line count, you know, I give it dash L if I only want a word count I would give it dash W and if I only want a bite count a character count I would give it dash C and if I do the dash say you can see it returns the bite count 24,736 so that is the reason I typically use WC rather than in L because you know WC is very terse, right? It gives you really just the number you're looking for the line number or the word number or word count number Whatever it is you're looking for rather than NL which cats out, you know the entire File for example, if you were doing this on a file it would cat out the entire file and give you the line number Proceeding each line. There's also one other advantage to WC over NL And that is that WC actually gives you an accurate line count where NL does not out of the box And for me to actually demonstrate this let me clear the screen and I'm gonna cat out my bash RC So I'm gonna run cat on the bash RC and I'm going to pipe that into WC and give it the dash L flag for line count and you can see there are 255 lines in my bash RC now I'm gonna hit the up arrow to rerun that last command it's up I'm gonna remove WC and instead pipe it into NL for number of lines now number of lines You would expect to also be 255 but of course it cats out the entire bash RC and you can see it adds line numbers Proceeding each line except the last line which should be 255 is only line 212 and the reason this is is because by default NL does not count empty lines it only counts lines that are non-empty and Obviously, there's several empty lines in my bash RC where I spaced things out Those lines are not actually counted by default in NL where they are counted in WC Now NL does have some flags you could use to force it to also count the empty line So if I instead did a cat on the bash RC pipe it into NL and then give NL the following flags dash B You can see now it actually numbers every line Including the empty lines and you can see we actually get the correct number now 255 lines But again WC and NL there even though you can get similar information from them They're not really the same command and most of the time. I I just need the exact line count number Nothing else no other output, especially with scripting many times. I just need that number right there So I can pass that number along to some other command, you know So all of this extra output. It's just superfluous stuff that I don't really need That's why I prefer WC over NL now There are ways of getting a line count an accurate line count that is just the line count number without any other Extra cruft and you can use several other GNU core utilities to do this the most obvious examples is I can use grip I can also use said and I can also use all to get a line count So let's start with grip I'm gonna run grip dash C and the dash C is a flag for actually a line count It's basically telling grip get a line count of everything that matches this pattern and the pattern we're gonna match is period asterisks, so we're gonna match Essentially everything every line where there's an empty line or it has anything on it So it's gonna grip every line and it's gonna give us a count and let's do this on the bash or C since we know The bash or C should have 255 lines in it and that is what is returned 255 so grip dash C Period asterisks and then the name of fall will get you an accurate line count to do something similar with said you could run said space Dash in give it the dash in flag and then inside quotes do a dollar sign equals and Then name of fall so I will do once again a bash or C and let's see if we get an accurate line count I got an error the bash shell does not like me using those double quotes right there So I'm gonna do single quotes so I'm gonna do said dash in and then inside single quotes dollar sign equals and then name A file in this case the dot bash RC and I get 255 now one thing to note about the said command here while it is a neat command if You ever run that particular command on an empty file meaning a file that has no lines You actually won't get anything returned You'll get no output because that particular said command can't handle anything that doesn't have at least one or more lines So if you do that on an empty file, just know you're not gonna get any output with said so a better Program to actually use will be all and you could actually run the following command with all can then inside single quotes capital end and then inside the squirrely braces. Let's do a print space in our and then the trailing single quotes and then name a file and we did this correctly we should get 255 returned and all if you run this on an empty file It actually can handle that it will actually return a zero if you run this on a file that actually has no lines So you don't necessarily have to use WC I mean if you really just need to get a quick line count you could use NL You could also use grip you could also use said you could also use all and just for funsies I will show you a Convoluted way of getting an accurate line count I could actually do cat dash in on the bash RC and if I run that you can see cat dash in just cats out the Bash RC and gives me a line count and then what I could do I could tell dash in One so I'm gonna get the tail which is the end of the file dash in for the number the line count One so I'm gonna get the very last line is what that does and then what I could do is pipe that Into cut and give cut the following flag dash F for field One so the very first field which of the very first field is that right there? And if I did that accurately I again get 255 return now that is kind of again just a fun example It's convoluted you run three different commands right cat and then tail and then cut to get 255 Where you know using something like wc or even grip said or all would be much more efficient So that was about a half dozen different ways that I know of to quickly get a line count from any output in the terminal So whichever program you want to use whether it be wc Which is the program I prefer to use or nl or grip said all Cut cat tail, you know whatever programs you want to use to get that line count They're all part of the canoe core utils every program. I showed you today They're all standard core utils that are installed on every single Linux distribution So use whatever you prefer now before I go. I need to thank a few special people I need to thank the producers of this episode Devon Gabe James Matt Michael Mitchell Paul Scott Well, so Tommy Allen Linux ninja Chuck commander angry Kurt die Okay, Dylan David Gregory Heiko got some people out of order Cascalee Maxim Mike Nitrix Alexander P sergeant for door Polly day River Red Prophet Steven Willie. All right, these guys They're my highest-eared patrons over on patreon without these guys This quick episode about getting a line count in the terminal It wouldn't have been possible the shows also brought to you by each and every one of these fine ladies and gentlemen all these names You're seeing on the screen right now. These are all my supporters over on patreon because I don't have any corporate sponsors I'm just sponsored by you guys the community if you like this work I'm doing about Linux free and open source software and these great command-line programs and you want to support me look for distro Tube over on patreon. All right guys. Peace Is there anything you can't do with grep said rock