Compare Files in Linux | How to Use the diff Command

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to lenode in today's video we're going to check out the div command the div command is very useful when you want to understand what's different between one file and another file it's a simple command but it's a very powerful command so let's go ahead and dive in and check it out so first and foremost what exactly is the diff command well in the intro I mentioned that it's a command that you can use to determine what's different between one file and another when you use it special symbols will appear that'll give you a better idea of what changes you'd have to make in order to make the files identical if you've never used it before then it might be hard to get a mental picture of the div command based on the explanation that I just gave you so let's see it in action on your end you'll need some text files to work with you'll need two text files so you could use some text files that you might already have or you could create your own just keep in mind that the purpose of the diff command is to show you what's different between files so if you decide to use two completely different files then that's not really a good test one thing you could do is make a copy of a file that you already have and just make a few changes to that file but keep the rest of it the same on my end I've created these two files right here file 1 and file 2. so how do you use the div command well it's actually quite easy you simply type the word diff and then we give it the name of the first file so I'll give it file1.txt and then we give it the name of the second file in my case file2.txt just like pretty much every other command out there if the files that you're using are not part of your current working directory then what you do is just provide the entire path to the file just make sure that the diff command is able to find the files and it should work just fine so what's different between file1.txt and file2.txt well I have the command typed out right here so let's run it and see if anything is different between these two files well that's certainly interesting there's no output at all so let's go ahead and check the exit code to see if anything went wrong I've covered the concept of exit codes in a previous video so I'm not really going to get into that here but if the command is successful then the exit code should be zero and it is and actually in exocode of zero when it comes to the div command means that nothing is different between the two files in fact what we could do is add a special option right here that'll make the output a bit more verbose and actually tell us explicitly that the files are the same and when I use that option it then tells us that the two files are identical but what exactly is inside those files anyway I mean it would probably be helpful for you to know what's inside the file so you can confirm or deny that what I'm telling you is true so let's cut out the contents of the first file and what I did was I just created some random text around Marvel Comics I don't know why but I was thinking about Marvel Comics at the time I created the script for this video but we have the output right here of the first file at least so let's do the same thing against the second file and based on the output I can confirm that the diff command is telling us the truth these two files are in fact identical so what I'll do now is actually change one of those two files that way the diff command will in fact have a change that it should detect and what I'll do is change this line right here I really do like Wolverine but actually Wolverine is not my favorite Marvel Comics character in actuality Spider-Man is my favorite character in the Marvel Universe so now that I've made that change I'll go ahead and save the file and then I'll exit out so now that we do have a change in the second file let's run the div command again and now we're actually seeing output and it looks like the div command has indeed noticed that I've changed the file so yeah I love comic books they're a ton of fun but what do comic books have to do with Linux Administration well actually nothing absolutely nothing it's a fun topic but has nothing to do with what we're talking about today I just used it as an example so what I want to do right now is show you guys an example of using the diff command or a situation that actually is related to managing a Linux server but the thing is though I was going to show you guys an example but it looks like my SSH connection was dropped I was using a linode instance for the example in today's video that's where those text files actually were they were in a lenode instance and now for some reason I'm back to my local command prompt but that's fine all I have to do is press the up Arrow to recall the previous command this one right here this is the command that I've used to get into the server that I'm using for today's example and I'll just press enter to reconnect but I can't well that's interesting is telling me that the connection is refused but it was just working what's up with that now the irony here is that in order to determine what exactly happened I'll need to connect to the server and do some investigation but I can't connect to the server so what can I do well thankfully the note's platform has a remote access utility built right in and right here in my browser I'm still logged into the dashboard so what I could do here is just click on the instance name in this case File serve 1 and I'll open the Lish console which will actually give me remote access to the server so what I'll do is log in Via this console right here and then I'll type in my super secret password so now that I'm logged into the server let's have a look around and see what happened and you might be wondering what does this actually have to do with the div command well you're about to find out anyway since I wasn't able to access the server via ssh what I'm going to do is go into the configuration directory for the SSH Daemon and that's in the Etsy SSH directory so that's where I'll go and right here inside that directory as you can see I have a number of config files now what's interesting is that I not only have the sshd underscore config file I also have that same name but with a DOT Bak extension at the end well that's certainly interesting and that's also very common when a Linux administrator makes a change to a file they usually back up the file before they make the change and if they don't do that then they should be doing that and thankfully even though it looks like somebody made a change to the SSH server and I'm just assuming that based on the presence of the dot Bak file at least they had the courtesy to back up the original file first so what I'm going to do is use the div command to find out what in particular is different between those two config files so I'll type diff and then I'll type the name of the first file which is sshd underscore config and what I want to do is compare that against the backup file so I'll press enter and we should see what's actually different between those two files well check that out apparently the port has been changed so basically anytime you don't specify the port when you connect to an SSH server via the SSH client on your local machine then it's going to assume that you want to connect via the default of port 22. in this case we can see that the port has been changed to Port 222 and I was able to find that out by using the div command again the entire purpose of the div command is to compare two different files I'm going to compared the SSH Daemon config file against its backup file it found that the port was changed or the line that contains the port itself was changed so what I'll do is edit the sshdman config file and set the port back to Port 22. so let's go all the way down here and I'll change it back I'll save the file and now that that's done what I'll do is restart the SSA service to make sure that the change actually takes effect so with that done I should be able to connect to the server let's go ahead and give it a shot I'll go back to my terminal and as you can see I still have the output for when the connection was refused obviously if SSH isn't listening in Port 22 then Port 22 will be refused that makes sense but let's try it again well I have a password prompt that's certainly a very good sign and now I'm back into the server so now that I'm back in my terminal emulator and I'm able to use SSH to get back into the server I'm going to reproduce that same output so that way I can go over the output of the div command itself and make sure that you guys understand completely what you're seeing when you run the div command and off camera what I've done is I went ahead and changed the port back to Port 2222 I didn't restart SSH so technically it's still listening on Port 22 but I wanted to replicate the same scenario so that's what I've done so I'll use the div command yet again and I'll do the exact same thing again I'll compare those same files so here we have the output and from this output even if we don't really understand fully what this output represents we already know that something about the port is different we could tell that one of the files has Port 222 for that line and the other file just has Port 22 and we can also see a hash symbol in front of port 22. that's very common if something is set at its default it's still going to be listed in the config file at least most of the time but there's going to be a comment character a hash symbol in this case that's going to tell the service in this case SSH to ignore that line and by SSH ignoring that line well it's just using the default but how do we know which file has which line is Port 22 22 within the file on the left or the right well the arrow that's in front of it actually gives it away the arrow beside the port 2222 line that's actually pointing towards the left sure that's a less than symbol but in this case it really doesn't represent less than it's just pointing to the direction of the file that it represents so we have sshd underscore config on the left and sshd underscore config.back on the right so therefore we understand from this output that Port 22 22 is declared in the first file now the second line that's shown here is pointing towards the right that's actually the backup file and that makes sense when somebody makes a change they usually or at least they should make a backup file of that change before they make the change so when somebody actually me but we're going to pretend it was someone else made a change to that file they made a change to the original file after backing it up so that's why we have the new information in the primary file the one that's not a backup and then we have the original declaration there in the backup file but there's definitely more to the output than just the arrows for example I've been avoiding the 15c 15 that we see there at the top but I definitely want to make sure that I explain that and I definitely wanted to save that for after the explanation of the arrows because the arrows are the most important thing here but the 15c 15 part that's actually important too the first 15 refers to the file on the left and the second 15 refers to the file on the right so first of all the numbers here are referring to line numbers the port number is actually configured on line number 15 when it comes to the config file on the server the line number for this particular config entry might be on a different line for you and if it is that's okay but basically it's giving us line numbers and it's telling us that in order to make the files the same then line 15 would need to be changed that's what C stands for change and C is not the only character that you might see here for example the letter A if you see the letter A then that would mean that something would need to be added in order to make the line the same between the two files in our case the line number that the port declaration is on was not changed between the two files so we don't need to add anything in order to make the files the same if we see the letter D instead that stands for delete in that case that would mean that a line was completely removed now let's go ahead and see another change that'll help us understand this even better so what I've done off camera is I've created another scenario when I run the ls command here you can see that I have two additional files that were not there before I have two Apache config files I just added a DOT 1 and Dot two to the end of each file name and I made some changes in one of these files let's see if we could find out exactly what was changed so again I'll type diff and then I'll start off with the first file here and then I'll type the second file name so I'll press enter and we'll see immediately what's different between these two files and there's quite a few changes here aren't there so basically what I've done here is I've copied the default Apache config file and I've made some completely random changes now the changes that I've made in particular might not be something that you'd want to do in production when I say random changes I really do mean random I just randomly change some things but what I've done in particular is I've made several changes to one of these files that way you can see what it looks like when you have multiple changes to contend with one of the lines reads this file is managed by ansible directly underneath that we have a greater than symbol but there's nothing after they're greater than symbol well actually that means that a blank line was added so two new lines were added the first one being the verbiage that I've just read followed by a blank line and since the arrow for that particular change is pointing toward the right the second file then that tells us that the new line was added to the second file in particular the characters at the top of that change 0a 1 comma two actually tells us that lines one and two were changed on the right hand side and on the first file it shows line number zero because those lines were not actually present in that file they were added to the second file so one comma two is a range of lines so basically that's lines one through two continuing along with the list of changes here we next see that the timeout option was changed it was changed to 150 in the second file but the original file had 300 for that setting the first file had that line on line number 92 but that line is now line number 94 on the right hand file because we added a few lines to that file and since we see the letter c here when it comes to the change code then that means that this line would need to be changed in order to reconcile the files now of course we have a few more examples that have letter C telling us that something was changed but I think given what I've gone over so far you should Now understand what exactly those particular changes represent now the last change is a little bit interesting though because for that one we have a d character showing up which I've mentioned earlier refers to delete so for that one something was deleted but that actually makes sense because what I've done is I've removed a blank line that didn't need to be there so what it's telling us is that a blank line was removed and I'm okay with that there was three blank lines between sections when there normally was two so I just removed one of them but you know what perhaps the output might be even easier to understand if we colorize the output and for that what we could do is install a special package I'll just go ahead and see if it's installed and in my case it is but if you don't have color diff on your system you could just install it with your distributions package manager and what color diff is is actually a wrapper around the div command so it doesn't do any diff checking itself it just colorizes the output for the diff command but anyway what I can do is just change diff right here to color diff just like that and when I press enter you should see the difference right away as you can see the output is colorized and for some people that might actually help if that's something that benefits you then well feel free to install color diff and you could even consider aliasing the diff command to color diff if you'd like I do have an entire video about creating aliases so if color diff looks like something that you might want to use well definitely check it out but anyway it's the exact same set of changes here the only difference is that we have colorized output now now let's see another variation of the diff command so for me I'll just change this back to diff and then what I'll do is add the dash U option right here and what does the dash U option actually do well when I press enter you'll notice that well there's a lot more information but it's also easier to read so as you can see we have a lot more output here so I'll scroll up and you can see that well it looks quite a bit different actually so at the top of the output we're actually seeing the modification time and modification date of the files which is always good to have when you're dealing with changes anyway and then we also see some additional lines here so we can see some lines before the change and after the change and that gives us a bit of context around where in particular the change is make sure we can look at line numbers but this is a good way to do it too and it's also easy to understand which lines actually contain our change codes we actually see the at symbols two at symbols on either side of those change codes which actually draws our attention right to that and makes it a lot easier to find we can see plus symbols when something is added and minus symbols when something is removed which is really good to have so the dash U option might actually be something that you might like as well and if so consider making that the default when you go to use the diff command but with the examples that you've seen in this particular video you should have no problem understanding not only what was changed between two text files but also where the particular change actually is inside the file so there you go in this video we checked out the div command the div command while simple is a powerful command a command as you saw earlier in the video allows you to easily understand what's different between two files now if this video helped you out then please click the like button and that'll let YouTube know that this video helped you out and who knows maybe more Linux content will be showing up all over YouTube if you click that like button if nothing else please consider subscribing there's some additional content coming very soon that you definitely don't want to miss and I'll see you in the next video thank you
Info
Channel: Akamai Developer
Views: 7,136
Rating: undefined out of 5
Keywords: linode, linux, cloud computing, alternative cloud, linux server, open source, sysadmin, linux commands, command line, command line basics, basic linux commands, linux basic commands, linux for beginners, linux commands overview, diff linux, diff command, linux command line, learn linux, learn linux commands, find differences in files, linux diff command, linux command to find diferences in files, compare files, compare files in linux, linux command to compare two files
Id: 3s1kselxQmQ
Channel Id: undefined
Length: 19min 59sec (1199 seconds)
Published: Fri Jan 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.