 I've done several videos on this channel covering a lot of the basic GNU core utilities, a lot of the shell utilities on Linux, but I realized the other day I haven't covered three of the most basic shell utilities, three of the most useful shell utilities. Those commands are cat, tech, and T. So let me switch over to my desktop and I'm going to go ahead and open a terminal and let me zoom way in, clear that screen there. What I'm going to do is I'm in my home directory, I'm going to make a test directory to play around in. So I'm going to do a MKDIR space test, so make directory test, then let's CD into this new test directory. By doing LS, of course, it's an empty directory. So let's copy something over. So I'm going to copy over my bash RC from my home directory over into this directory. So let's hit enter on that. Now when I LS, of course, I have the bash RC. The most basic use of cat is simply cat name a file. And all it does is it prints the contents of the file to standard output. That's all cat does when you give it cat name a file and no other arguments. All it does is print the standard output. And that's really what most people use cat for. But here's the thing, although that's a valid use case for cat, cat actually stands for concatenate. That's what it was originally meant for. Concatenate means after you split files up, concatenate means put them back together into one file. And I did a video about the command line tools split and C split split takes a file and splits it up. I don't want to cover everything about split and C split, but go check out those videos about those commands. But if I do a split dash L 50 for line count 50, and then dot bash RC, every 50 lines, it's going to split the bash RC. And my bash RC was long enough that it was split into six files, right? And I realized on that video the other day about split and C split, I never told you guys how to put these files back together, because I kind of assumed everybody knew about the cat command. It's like the first command you really learned in the term was one of the first ones. Anyway, all you do is cat and then the files that you want to put back together, yada, yada, yada. And then at the end, you know, new file dot txt or whatever it is, you want to name the file that you're actually creating from all of those split files. So what I could do here is that actually I could do cat x a asterix, because all of these six files are actually x a, you know, a through elf. So that'll save me on typing. And then you need to redirect that, right? Direct that over to whatever file you're writing to. In this case, I'm going to create a new file. I'm going to call it new file dot txt. And now if I LS, you see we have new file dot txt that was created from all of those splits. And if I cat new file dot txt, you will see it is an exact copy of my bash RC. So we took the bash RC, we split it up into six chunks. Then we concatenated it all back together into this other file to make that copy. So that is what cat is mainly meant for is printing out a file to read it and print it out to standard output or you use cat to concatenate split files. Now before going any further, I do think we should talk briefly about what redirection is, especially IO, we should talk about standard input, standard output and standard error. So when we talk about standard in, it's typically symbolized with the number zero standard out is one and standard error is two. And for those of you that are not sure what that is, standard input is what I'm entering at the command line. So, you know, when I do an LS, for example, that's the input, and that's the output, right? That's the standard output, right? So that's the input. That's the output. And if I did something that didn't make sense, like a give it a Z flag, which in my case, my LS doesn't have a Z flag because my LS is really the EXA command, which doesn't recognize that flag. And this is standard error, which is also output. But we have two different forms of output. If the output is normal output, it's just standard output. But if it's an error, it's standard error. I hope that makes sense. So you have standard input, standard output, standard error. And just like everything else on a Unix like system, everything's a file, right, including standard input, standard output and standard error. So actually slash div slash STDN is standard input slash dev slash STDL is standard out and then slash dev slash STDERR is standard error. Those are where those files are on your system. Now I bring all of this up because cat name a file, for example, cat dot bash RC, you know, I could run that. But what is that? Well, what it is is it's taking standard input and the input it's taking is dot bash RC, the contents of the dot bash RC. And then of course, it's writing it to standard output. And that's what the cat command is doing. But it has saved you on some of the typing because technically what's what it did is zero and then the less than sign, right? So that is, Hey, what are we doing? Well, we're taking standard input, what are we taking as standard input, the dot bash RC. So I hit enter on that. It's the same command. It's just you didn't have to type zero less than sign because it knows the way the cat command was written that if it's cat file name, that is the input. But that's technically what you're doing. And of course, you could do output. And if you wanted to, you could specify standard out. But you don't have to do any of that because that command really, you don't need standard output if it's just going to standard output, it always goes to standard output by default and you don't need to specify the input if you're just doing cat file name either. So I just wanted to clear some of that up. Now, why did I bring that up is because sometimes you don't necessarily want the output to go to standard output, right? Sometimes you want to cat the dot bash RC and then write it to a new file, for example, right? You see the redirection now. So instead of by default, if I didn't give it a specific place to write it to, it's going to write it to standard out. But in this case, it doesn't write it the standard output, right? It writes it to new file dot txt. And what the single greater than sign here does is it takes the new file dot txt, whatever was there before, it gets overwritten. The bash RC is this is an exact copy of the bash RC. Now, now if you wanted to append something to a file instead of completely overriding it, you do two greater than signs. So now if I do cat dot bash RC and then two greater thans new file dot txt, let me hit enter. If I cat that, well, let's not even cat it. I'm going to open that in vim just to make reading it a little easier. I've actually now if I scroll down, we get to the end of my bash RC, which should be right here. And then we get the beginning of another bash RC because we added it twice, and it didn't override it, right? It just appended it. But if I want to completely override it, you know, I can do something like echo one into new file dot txt. And now when I cat new file dot txt, it's simply one, right? We completely overwrote the 1000 lines that were there before with just that single character. So be careful when you're doing the redirection. Remember, single greater than is overriding the entire file. And then the two greater than signs is appending the already existing file, meaning you're adding to the end of it. And of course, sometimes you don't want to write to standard output. Sometimes you want to write to standard error or sometimes you want to suppress error messages. For example, if I did that ls dash z command earlier, you know, we get this printed to standard error, right? Well, you know, if I really didn't want to see that error message, I could do two greater than sign for standard error. And I want to direct all of the messages that normally would get printed to standard error. I want to direct those to slash dev slash null, which is a file on your system. Think of it as a black hole is where things go to die. You don't ever have to see it. So if I hit enter, it ran that command. And that command had an error, but we didn't have to see it because we sent standard error to slash dev slash null. Now this here, the two greater than and then slash dev slash null, you will see that a lot in scripts, you will see people use it a lot interactively at the command line, because sometimes you're testing things out. Sometimes you know they're going to give you an error. Sometimes those error messages are long, you don't want to see them. And that's a way to suppress that error message. Now enough about redirection and standard in out an error, but I needed to explain that because that's really important with the cat command. Now back to some of the flags you can use with cat cat dash in name of file adds line numbers, which is really useful. Some other flags you could use really cat. Most people never use a flag with it. The only flag that I've ever used with cat is actually that dash in flag for the numbers because sometimes you do find it useful. But if you do the man page for cat, it has some other things. For example, dash capital E is interesting. I run that you will see it adds a dollar sign at the end of every single line in the file. Why is that important? If you're doing pattern matching based on regular expressions and you need that dollar sign actually printed at the end of the line for something for your pattern matching in your script, that could be useful. There is actually something similar for tabs, if cat dash t, but I don't use tabs. I actually have them and emacs configured to where they actually don't tab and they replace tabs with spaces. But what that would do is if there were any tabs in here, you would see a carrot symbol capital I in their place. Let me see if I can actually find a file that actually has that. So if I go, let's go into slash at C slash sudo dot com. So this is a system file. So I didn't write it. So let's see if I can find yeah, there's a tab, right? Somebody tabbed right there. So that is the dash t flag. But again, I don't typically use those. But if you do a man cat, there is a few flags and options for cat not that many. Now let me clear the screen. Let's talk about tack. So what is tack? Well, you take cat and reverse it, you get tack. And that's kind of what the command is. If I do a tack on the dot bash RC here, it prints it to standard output just like cat does. But it does it in reverse order. So all the lines are reversed. So this was the first line of my bash RC is now the last line in the output. This was the second line. Now it's actually second to last. So it just prints it all out in reverse order. And that's kind of really all tack is that useful for it's because so many people needed a way to cat a file to print something out in reverse order. We needed a specific command for that because without the tack command, you know, you have to create your own bash functions essentially to make cat print out things in the reverse order. And because enough people needed that it became its own command. Now if I do a man on tack, you will see tack it concatenates and prints files in reverse. So it does exactly what cat does in reverse. So it prints things in reverse to standard output, it also concatenates things in reverse. So if I do attack, well, let me LS to verify that those files are still there. Remember the split files. Now, what if I do tack x a and then the asterisk because I'm going to tack all six files, right to new file dot txt. And I'll just override the file. Now what did that do? Well, let's cat new file dot txt to see exactly what that did. Well, it put the split files, it put them all back together. But it did it in a very strange way. If you didn't, if I didn't explain what it did, it wouldn't be that obvious. But what it does is it takes those six files. So the very first file x a a was line one through 50 tack took it in reverse, you know, 50 down to one. And then the next file x a b it did the same thing line 50 is first down to one and it put the so it puts the files together in order from x a a x a b x a c. But the individual files are reversed. So that may not be, you know, what you were expecting with that command. But that is exactly what a concatenation with tack does. Let me clear the screen. Now let's talk about T. What is the T command TEE? Well, the T command is used when you need to cat something. Meaning I want you to print the standard output, but I also want you to write that to a file. Because by default, you know, of course, you know, cat can't do that, right? Cat, if I cat, you know, the dot bash RC and write it to new file dot txt, right? It writes the bash RC to the new file, right? But it doesn't actually print it to standard output. And how you achieve this is using T. But be careful with how you use T because T is not like cat, you're not gonna T and then, you know, like name a file or something because if you do that, all you're going to do is you're going to just blow away that file, you're going to empty the file, it's just going to become zero bytes because the way the T command is actually designed to work, you're supposed to run some command and then pipe it into T and then, you know, name a file. So let's try this, I'm going to go ahead and let's just do a echo and how about I echo hello world and we're going to echo that. I didn't want to pipe that into XRs, I wanted to pipe that into T and we'll do this into new file dot txt. And you see, what it does is new file dot txt now actually reads hello world, but we also got it outputted, right, to standard output. Now notice we're not doing this with a single greater than sign or the two greater than signs if we want to append something. So what if we want to append something instead of overwriting the whole file with T? Well, T has a flag for it. So if you don't want to overwrite the whole file, you just tack on the dash a flag. And then let me do this here. So it's obvious what we just did. Line two, I'm going to echo line two into T dash a for append new file dot txt. And it echoed line two, if I do a cat on new file dot txt, there's actually two lines, right, hello world's first line and then line two, but it only catted out or outputted here the T command what we were appending in that case. So T is pretty neat. You know, I can do something like I could take LS and then I could pipe it into T, you know, new file dot txt. And there is the output from LS and that output was written, of course, to new file dot txt. So there's the cat of that. And that's cool. It even kept the escape sequences and the coloring and everything as well. So kind of like tack was needed, even though you could technically make cat do things in reverse, you know, if you pipe things into other shell commands, is T really needed? Well, I mean, I could accomplish what I did with that LS command. Could I do that with just using cat? I'd have to probably use a couple of commands. What I'd have to do is I would have to LS and then I'd have to write that to the new file. So that takes care of the writing part. But how do we get the standard output part? Well, I'd have to and and meaning run another command and then cat, you know, new file dot txt. And that would essentially be the same thing as that command there. But again, you're running two commands. We're in this case, of course, we're just piping LS entity. Now T is interesting because I don't use it that often. But there are a couple of use cases that I find really valuable for T because you're going to run into some issues occasionally where you have to use T. One of the most obvious examples, those of you that use VIM and when I talk about VIM, I mean proper VIM, not Neo VIM. So I'm actually going to do command VIM here because my VIM is alias to Neo VIM. I want to run the actual VIM, you know, the standard VIM command because this only works in standard VIM, not Neo VIM. But if I open something that requires root privileges, sudo privileges, so I'm going to open this particular file here. It could be any file, but that just happened to be one that was in the history. So I'm going to edit this first line. I'm going to add a second pound symbol, you know, hashtag there at the top. And if I hit escape and do a colon W for write here in VIM, it's going to complain, hey, you didn't open that as root. You didn't open that with sudo. So you have no privileges to write to this file. Well, how do you get around that? Well, in standard VIM, how do you get around this as you get into command mode? So what you do, you do the colon and then W for write and then do exclamation sudo t. So you're going to use the t command with sudo privileges and then space and then the percent sign. What this is going to do is use the t command to actually write to the file. Well, it's going to use the t command with sudo privileges to write to the file. If I hit enter, it's going to open a terminal in a split. It's going to ask for my root password. I guess I could give it my password and hit enter and wrong password. All right. And that was the right password. And now yeah, I can just hit L for load the file. And now we're good. Now I could quit out of that. And if I come back to it, you will see my changes were actually taken. So if you're a VIM user, you are going to use the t command. I'm just going to tell you now another thing you're going to find is sometimes you're trying to do things with redirection redirection once again, where you're trying to take a command output of a command and redirect it into a file. But that file is protected. For example, I did a video not too long ago on how to shut down and reboot on Linux. And one of the things was using the magic sysrq key, right? And there's a file on your system. If I cat slash proc slash this slash kernel slash sysrq, I'm going to cat that command. This is the privileges I currently have with that particular key, the sysrq key. Now, what if I wanted to change that? Well, I could change those to instead of 16, I could change it to one, right? And then I could put that in slash proc sysrq. And already I know the command is not going to work. I'm in the fish shell, by the way. And you see the red is letting me know the command you're trying to type is not going to work. I'm going to hit enter anyway. But the reason it's not going to work is because it already knew I don't have permission. I don't have permission to echo something into this. Well, let me up arrow. And, you know, if you don't have permissions for something, what do you do? Well, sudo, right? sudo echo one, and then direct that into sysrq, actually, because I'm overriding the file. I really don't need the double greater than I just need the single one. But that is an error too. Now what do you do? Well, now you have to really think about things because the problem here is you just gave echo sudo privileges. You don't need sudo privileges to echo, right? But on the other side of the redirection, you didn't give yourself sudo privileges for this. So how do you get around this problem? I'm going to tell you now, anytime you encountered this problem with any protected file, T is your lifesaver. So what you want to do, take away the sudo privileges from echo, echo doesn't need that. And then echo one, what you want to do is pipe that into sudo T. So you're going to give T elevated privileges to write to a proxess colonel sysrq. If I do that, now it has changed that file. I'm going to go back and set that though to the settings as before I'll change it back to 16. So that's just a little trick that you can do with T. I found those two tricks very useful, especially the Vim one. And of course, you know, I showed you a little bit of what you can do with both cat and tech. Now before I go, I need to thank a few special people. I need to thank the producers of the show. I need to thank Devin Gabe James, Matt, Michael Mitchell, Paul Scott, Wes Allen, Armoredragon, Chuck Commander, Ingrid, Diyokai, Dylan Dorsley, Lennox, Ninja, Maxim, Mike, Erion, Alexander of Peace, Archon, Vitor, Palatinate, Rip, Rif, Steven, Willie, Red Prophet. I don't know what I said, but I don't think I pronounced it right. Red Prophet, Steven, Willie. All right, these guys, they're my highest tiered patrons over on Patreon. And all of these ladies and gentlemen as well, all these names are seeing on this screen right now. Each and every one of these ladies and gentlemen, they're awesome because without them, I couldn't do what I do. I don't have any corporate sponsors. I'm sponsored by you guys, the community. If you like my work, please check out Distro Tube over on Patreon. All right, guys. Peace.