Time
5 hours 21 minutes
Difficulty
Beginner
CEU/CPE
6

Video Transcription

00:00
Hello and welcome to command line basics. In this video, we're gonna go over command redirection. I'm Crystal Hyler and let's begin.
00:08
So are learning objectives for this video. We're gonna understand the purpose of redirection and why we should do it and how we can do it properly.
00:15
Learned what this greater than symbol does. Learn what the double greater than symbol does and what the less than symbol does.
00:25
So which operate in a pens to an existing files. Is it a single less than be a double less than see a double greater Than or D a single greater than
00:39
the answer is seat The double greater than with this stuns is it will upend to an existing file, whereas the single greater than will overwrite existing file if it exists already,
00:50
the single less than is used for input into a different command and the double less than is not a valid operate.
01:00
So the redirection, the whole purpose of redirection from the command line, is that we can use the output and either save it or we can direct input using the less than symbol so we can specify, you know, different standard out or standard area levels of redirection as well instead of just the normal ah,
01:19
output.
01:21
So it's just a useful way to be able to. You either gather information from the output of command that we can search through later or save it and keep it in a different place.
01:32
So the single greater than what this does is it just simply takes the output from a command. It will stuff it into a file on the system.
01:40
If the file does not exist, it will create it automatically. Now the big thing to keep in mind single Raider then, is that it will override a file if that file does exist. So that can be a problem if we want to keep you know, the rest of the information in that file ready?
01:57
So let's go ahead and give a quick demonstration.
02:02
So I'm going to you, dear. So we can see what's inside of this.
02:07
Now let's say echo
02:08
Hello.
02:09
And we're gonna use the single
02:13
and percent and put it into
02:15
and load, uh, 60
02:19
or excuse me, not 9% but single. Greater than And now if we do another dir Sylvia. Hello, Don t x t
02:25
and let's do type.
02:29
Hello. You could see Hello there.
02:30
And now if we do this again But instead of hello,
02:36
we have
02:38
Hi.
02:38
Now with the single greater than it's going to overwrite the helo
02:44
And now we can do type again. We can see the helo is gone and all we have left is a high.
02:51
Now, In order to overcome this, we can use the double greater that found what this does it pens to the end of the file if it does exist. So we don't want to override everything in the file ready. We use the double greater than I am, and it will automatically put it at the bottom of the file. So we don't keep overriding something that we want to keep in there already.
03:09
And just like the single greater than it will create a file if it doesn't exist already.
03:15
So let's give a quick demonstration of the double greater than and how we can use it to not overwrite what we're working on.
03:23
All right, so let's say go.
03:27
This didn't overwrite
03:30
and the double greater than
03:32
and load out t x t.
03:35
Now if we do type
03:37
for hello we can see we have This didn't overwrite on the end of the file that was already there, and it did not overwrite the high that we previously just did.
03:47
So this is very useful to keep in mind as we are redirecting output into files that we don't want to override things that we want to keep in there
03:59
when we had the single greater than what this does. It is used to provide input from a file into a command, and this file must exist because you are providing it as input and it will not change the input of the file
04:14
so it will leave the fallout. The same will simply provide that file as input into the command
04:18
so we can use the sort command and the less than to say, Hey, I'm pushing this filed off t X t into the sort command.
04:28
So let's give a demonstration of that. I'm gonna pull up by the Lenox machine
04:31
and just like always, I'm gonna type l s to see what is inside of this directory.
04:36
And now let's say I want a sort Latin words that t x t do sort
04:43
and letting words that t x t with my less sensible
04:47
and look that goes head takes it as input sorts more A lot for me, pretty useful.
04:55
And now we can also redirect standard error from commands as well. And this is something you may have seen already. Where If there is a lot of veritext, especially if you're doing search and you know the command doesn't have full access to the system, it will give you errors saying, Hey, I can't reach this. And that could take up a lot of screen information.
05:15
So we can do is we can redirect that standard error into a place that doesn't exist on limits. That is, a dead no, a slash, stab slash null. And anything sent to this directory on Lennox is immediately dropped and just ignored. And now, in order to specify standard error,
05:32
we're gonna use the two and then the single greater than And this says, Hey, uh, for any ever is that you get one. This command is running. I want you to take it and spit the standard error into this specific location and using the Dev. No, it says, just get rid of it.
05:53
So
05:54
this is a useful way to be able. Thio. Ignore all of the air information. So let's say I'm gonna use my legs machine and I'm gonna search for my program on the route of the drive.
06:04
Now, as you'll see, let's take that grip part I my program
06:12
forward slash for the room.
06:14
Now you'll see I will have a lot of
06:17
This is my bash history
06:20
and now I should have a lot of permission denied. There we go.
06:26
So there's way too much going on right here.
06:29
It's taken over my entire terminal. I I'm gonna use control. See, Stop this before it takes up more space
06:35
and then d'oh! Clear Nazi Less so that I can get back to that kind of default position on the terminal. Now if I do
06:46
two
06:48
active
06:49
No.
06:51
This saying I want you to take these standard error and pushing into Dev. No.
06:57
So, as we'll see, we won't get all those errors because it's all the error messages that it gets. It automatically sends. Do that slap have slashed no directory where it is dropped by the Lenox system. So that way we can focus on directly just what you know the output we have.
07:15
So here we go. So I can already see, you know, some different, uh,
07:19
information coming back. We don't have all these error messages coming all over screen and taking over our, uh, her
07:28
our command window. Excuse me?
07:30
Aren't supposed to. Says which operate overrides an existing file. Is it a single west than the double? Less than
07:39
see single greater Than or D A double greater than
07:45
answer is C a single greater than this will overwrite existing file. And it will not depend to the end of the double greater than well up in to the end of a file if you want to keep it. But if you don't care too much about it or you just need to create a new file the single greater than work exactly what you needed to
08:03
and the single less than is used to provide input to a command, not Thio. Get at the output for
08:11
All right. So in this video, we learned the purpose of redirection and how we can use at the command line learned what the single greater than does learn with the double greater van does and how it depends for the file and keeps the file and understood with single West, then does and how we can use it to our advantage, and I hope to see you in the next video.

Up Next

Command Line Basics

In this course you will learn the fundamentals of Command Line, a fundamental tool for any user of Windows and Linux machines. Command line allows developers to manipulate files easily and quickly. Learning command line saves developers time and resources.

Instructed By

Instructor Profile Image
Christopher Haller
Senior Intelligence Operations Analyst at Centripetal
Instructor