 Once again welcome to this class on Linux basics this is the ongoing sees the this is the class on Linux and all the programming languages today is our fourth session we will be continuing our discussion on the Linux we started talking about the basics as for the kernel the file system and then the shell then we went into more details about the commands and then last week we saw how the line in the last lecture we saw the commands how to do some managing the file access managing system resources from managing general disk usage or disk resources to be specific we saw the commands like change mode to modify the file permissions we had the different sections in the file permission list the first one is for the user the second one for the group and the third one is for everyone else we saw how to set these access codes exactly like a binary number so basically it's read write and execute so for each of these things so maximum that you can allocate a number is seven which is because read write execute each one is represented by one bit so if all of them are on that's one one one which stands for decimal number seven so you can say like seven seven seven that means that it's all ready for all the books and then based on how there you make because zero and you can control the access for example you want you want all the read write execute so you make yourself seven and then the group just wants to read and execute then you make it as one zero one which is five and then for all of them we just want them to read then you make it as one zero zero which goes to four so like like that you can control so 754 or any combination of that you can use to control the access then you also we saw the change the ownership of given file from yourself some of the owner some of the user and then you can also use the change group to change the group permissions itself of the group itself of the file this comes in handy because when we look at like various realized situations a particular file access may be restricted because of certain groups of particular directory and be accessed only by certain groups and you may have permission but you don't know like in which group you belong to and you may have like permissions of multiple groups you want to make sure that that particular file that is most at least that one group which enable you to do more stuff we also saw like how to manage the system resources in particular we saw some of the definitions of the processes how do we what we call process in Linux and how Linux assigns process ID for each of the processes and then we also saw how to report those processes using the ps command all the top command and then we also saw how to kill a particular process by using the kill and in which we also saw like the various priorities where the interrupt can be as high as minus 9 which means that instant kill of that process we also saw some commands related to the resources the disk usage in particular we saw like DU command the DU various options and which actually enabled us to get how much disk space is remaining and then we also like we saw the DF command which is essentially again another way of the estimating the usage here the DS reports on a particular file system as to mount point and how much memory then we also saw the command for free to also understand how how much memory is we name we also use some other commands I think we did the to end of the map analogy there want to find out like which who who is the user so there is a who command which gives you all the users in the system or who am I specific to which user name that you are using to access the system so today we will be looking at some more comments and before we start about today's lecture I want to assign you some more activities so just like what we did in the last lecture today I want you to use man command to find more details on the top top is the other command that we studied in the last lecture to get more details on top and then page on find out and see like what about in what are the options how to use it more detail especially the top will very widely used for now to see in the dynamic way as to what is happening with all the processes in the system and so particular interest is how do you fill within the top and also how do you exit off top those kind of things become very useful then use the PS command to find all the processes that are running and use the kill commands to stop also use top to find the processes running and then use the a command use at least one command to stop the process from the top top command itself so these are just one activity you can do so in today's topic we will be dealing with more in-depth file system commands specifically talk about grep f grep locate unique sort touch find other commands like this and T I kind of group them here and we will see why and also like some of the commands that it also like other commonly used among link how to link a file the linking is a very easy way of actually accessing a file without actually popping the file and long time as well as large so let's start start with these commands the first command that we will talk about f grep or grep the grep command is mainly used to search a keyword inside a file so the keyword is specified as a string and the grep provides a lot of options from essentially for doing various things so here are some options are given which is dash I for ignore case dash V for the lines which don't match so basically it excludes the lines that match and it gives you only the lines that don't match from file and then also you can say like the dash n and the following n specify some number so that the command actually displays the lines where it found the match and then after that the n number of lines which is specified here in that file so these are like very powerful and useful commands from typically we try to get use the grep to find a particular keyword whether that is there in the file and then once we find it then we can do all kinds of operation from those keywords and then we can use it in other programs even the string that we specify for matching that you can specify it as a regular expression again you can actually go into the man grep or man grep to get more information about this command itself there are also like other nifty features like dash L which only shows all the files where the keyword is present it doesn't output the exactly the line so these are all like some nifty things that we can do to gather information so again these are all like the information getting some else they themselves don't modify the file one of the features that we talked about earlier was this multitasking so even within a window you can do multitasking which is essentially we saw this pipe command where we can pipe the command output of a command into another so similarly you can use that command in that passion for example if you want to search multiple keywords and you want to see like I mean which file all the particular key was that person so you can actually get one keyword at a time and then pipe those outputs into the subsequent for now subsequent so it will be like that a from star dot AST pipe to get B pipe to get C pipe to get B so now the output of that this entire command will be one file which contains all a so so it is again very useful command and then you can you can put it in multiple orders and we can give you some exercise with electron to see how knowledge of the other command that we will talk about is fine the fine command is again used to find particular file in a hard drive so this is like mostly like the file name rather than using really a grip which is the more keyword so using this one you can find files by date and we can specify also the range of times then modify so here is an example find slash user slash bin type is F and then the dash a time plus 100 dash bin so so then you can actually like get this particular files so using the fine command you can also use find to show the postscript files you know directory and then one example is like find star dot Perl here you can see all the commands that end in what Perl so again the syntax follows the general syntax of the in a Linux command only thing is here the directory is also an argument which is essentially like find and then argument and then the options and then followed by what is action for example here the action is to print that all the files that for the post that perform in the example come on so here there is more stuff basically make sure if you just don't specify anything it assumes that it's actually the current directory so find star Perl will give you list all the PL extended files this is same as you can say think of right now at this LS star Perl that also gives the same of it so the find is also like used when the particular directory is or particular file is embedded in multiple levels of hierarchy and somewhere below the hierarchy is where this file is then you can use find to actually like find the file the other command is to locate locate is is much faster than the fine command so that is the reason why you choose to use locate instead of find so the the again the files are printed with the path if you use the command so the syntax is just locate and then the search string so it finds the file and then it prints the whole path where exactly the file is located for that string so here is an example again locate star Perl it gives you like the entire path and then followed by the varieties and then it goes under all the all the directory so from the current directory it goes under each hierarchy and then finds if that particular file exists in those hierarchies and all the entire path actually like I just wanted to be stating in the previous one the find also is that exactly the same thing instead even though it doesn't print out the the path name this set of files is not just limited to the current directory it's actually the from current directory onwards it goes all the way under and then find set files so that's the principle difference between the LS command and the fine command whereas LS only restricts to the particular hierarchy that we use for example if you just say LS star Perl it only finds all the Perl files in current directory if you do LS dot slash blah blah slash star Perl it only goes under one directory below and then look for the particular files whereas find searches across all the entire tree that is under that particular directory and gives you all the matches so this is a principle distinction in locate this also and then the other command that we will learn is WC it's not water closet so it actually stands for word count so here the key thing is you can actually determine how many lines are there in a file using this WC command you know it's the word actually it can give you the number of lines the words the number of characters all of them so again there are multiple options dash L gives you the lines and if you just specify WC without any options and then the file name it gives you all three it gives you the number of lines as the first column number of words as a second column and then you can specifically go ahead and ask WC minus L filename that gives you the number of lines WC minus W the filename gives you the number of words again the words means the the the non-white space characters group of characters separated by a white space character or a new line so those are the words that's the definition of word and so that collection will be treated as a word and then to just output the number of words and then WC minus the filename will give you the number of characters here the number of characters typically is the non-white space characters so all the white space characters are not reported so here there are some examples so here is a default WC test color V gives you the filename 8 characters 8 8 words 93 characters and then here you can also see like somebody is piping the LS command into WC myself so then it gives you the since it is LS basically like it gives you how many lines are there in the in this output of the LS command again as I said like when you use the pipe the commands output gets piped to the particular so in this one you can think that basically there are 68 files in that in this direction you know like the regular LS actually gives the files multiple files in the same line when you pipe the LS to the WC model if it gives each file as one line so that's why I'm going to get the actual number of files in the 68 so the next command that we will know is sort a sort command is used to sort the files in the alphabetical order so a sort filename sort and then the filename just takes all the lines and basically sees which line to be coming first and then it presents that way for example in this case the next one dot x C C plus net eclipse Java so you can see that actually let us see if just goes in the ports this so inside the content may be in any other order but when you do a sort it sorts it immediately into a lot again this is another useful command if you are collecting some data and then the data is all some form and you want to present it in a much better form so you can use this command to present it in an alphabetical order so now let us move on this command unique this command prints out the file removing all the duplicate adjacent lines so again you can say unique unique filename it gives you all the lines that are appearing only once and then there are options to it so for example unique minus you the filename dash dash unique prints all the unique lines and then if you say like repeat it with dash be option it only prints out the what are the duplicated lines so again this command is used many ways if you have a particular filename which you copied from multiple sources and you do not know whether something is repeated in time one way to find out is just use this unique command can also say like dash see filename which gives you the number of times a particular line has acted so for the unique lines it prints out one and then how many times that particular line has been occurring inside that file so let us look at some examples here create the test they create this one you can see that actually Dravid Dravid is repeated Sachin is actually repeated but different patient and ganguly is repeated twice so if you just give like just unique text now it gives you the Sachin twice but Dravid and ganguly are reported only once so that they are given so one way to eliminate these kind of situation is to use the soft so when you do a start the Sachin and Sachin whichever position actually like on the gang together so in this case it is Dravid cumle or Dravid ganguly cumle and then Sachin Sachin so now if you say like sort the text and then pass that to the unique command then you get really just the unique ones because the two such ins are coming now together and that gets eliminated and then you can say like unique minus you the text then it gives you like Sachin Sachin and cumle this is the this show illustrate how the unique command is getting used actually this the unique minus you is actually only depends on the unique lines in this case like if you have we have sorted this way then we will print out the oh actually like because the text is still this one so that is why Sachin is printed twice because it thinks it is still they are unique because they appear in different lines the entire Dravid is gone and then the ganguly is gone because they are repeated so that is why the unique minus you gives two such ins and one cumle so now let us go to the next one which is touch touch is way to create empty file so when you say like touch and then the file name an empty file with that file name you will be created you have a size of 0 bytes it does not exist if that particular file exist then it updates the timestamp so that it appears as if like that file was updated just recently okay so again this is useful in the terms of some some particular commands that we may work on will not run if the file is not updated to the latest time timestamp or you may have a requirement that this particular file needs to be updated much later than another part so and but the file that file may be created later than the file that you want to access in that sense you can just do a touch and then the file name which updates the timestamp and then you can do the processing because here as an example we want to change the access time of file 6 to 10 10 in May so here we can actually give a particular date and then we can say like touch so touch minus B second May and then followed by the time and then it will change the timestamp to that particular time so you can use an LS minus L to verify whether the timestamp change has happened so another way to do it is dash T touch dash T the touch dash T takes an expression which is month followed by day followed by hour followed by minutes so it is shown here month month day day hour hour once you specify this format it actually updates the timestamp to that particular format that particular time so that that's another way to change either month dash T with the date inside the quotation or just specify dash T and then it updates typically we don't use within the options you can just say touch and then the file name then it puts the the latest timestamp into that one so it just gets the timestamp from the date command and then it puts that particular date for that file so that it appears as if it is not modified and then if it is not there then it produces a file with 0 bytes so here are again some more examples so when we say like touch and then we see the what is the disk usage for that particular file which reports 0 bytes and if you do an ls-l it will report 0 bytes as the latest timestamp and then we say okay now we want to change the timestamp and then we provide this timestamp and then then we say like ls-l text we see an older timestamp so again you can advance the date or move it forward you cannot move it forward more than the system date itself so that's another thing that we want to make sure of so here it's also like you can dash T option to change the date on the particular file and change the system so as I mentioned earlier there are few commands to actually alter the programming behavior or managing the system also one thing that we saw was the pipe command then you also saw like how the process can be called in the other command is what is called the team the T command actually sends an output of a program or in our case it's a process output of a process to two different directions at the same time so you can specify T and then file name with options and then it can actually send it to two different locations so what are the two locations here one is it stores into the particular file so here we have an example ls-l by T filename file 2 so now let's see like I mean how this works so ls-l generates an output which is the list of all the file all the details inside the directory and then that is actually piped to T and then file 2 so when it is piped actually it is actually going to the output file in our case it's the terminal where it displays what is the output of that ls-l command but we say that is also do a P to file 2 that means that it actually writes out the same information into file 2 and the dash T minus a is an option to append to a particular file the same as the double arrow when we say double arrow it only like that just that portion of it has writing the file writing into file whereas the T will write into the file and also output into the terminal let's see an example so in this example we initially we store the output of ls-l using a greater than arrow into list 1 now the second command is interesting where we did the same ls-l but now 5 to T list 2 so now let's see what what is output so when you do the ls-l it's actually there are all these four zero size files file 1, file 2, file 4 and then there is some file form of size which is list 1 now what will happen when we do the list 2 so one thing is to notice is when we specify this ls-l 5 to TL list 2 it displays the output of the ls command in the terminal which is you can see like the four zero size files and then the list 1 which we created earlier this ls-l send to the list 1 but now you see that basically produces this output at the output terminal but also now it writes this information into another file called list 2 and that list 2 is not shown here because at the time of team basically they are two parallel processes so the list 2 was still getting updated so it doesn't even know that that particular file existed at that point so the ls-l produces normal output now the next command is the T with dash a which means that we want to append the list 2 with the same ls-l command so now if you look at it now the list 2 command list 2 is shown and pretty much you can see that actually they have like similar type of number of files so again you can see that actually there was two more some more information that gets written into the the list 2 but in general you can now see the how this thing works again in this case the T is still it works as two processes and those processes are executed independently so it still doesn't know the existence of this particular file list 2 so once we finish the execution that's when it gets written into the directory and then that's when we know that okay this file exists and so the subsequent commands will start using the file okay so now we will go into two more commands which are quite important because these are the commands that we will use more often in current in the current course as well as in go out in the real world we'll be using all these commands for much more details so the next command is cmp pmp stands for compare this actually compares two files and reports if they are identical and it records the position of the first character the program itself generates a zero output if they are identical otherwise it produces a one output and then shows the difference so the syntax is just cmp file 1 file 2 so here we give a cmp ex1 vertex and ex2 vertex here it says essentially like I mean differs by differs in byte one and there is line one so after it executes this it stops and it comes back to the shell so again cmp is another useful too to understand how to what is this what are the differences between two files and then how to bridge those differences like that so we already saw the find the command actually this line should have gone earlier home and we know that actually it's basically defined the path selection criteria essentially the path for the selection criteria so you can say find dot which means that start from the current directory and then the selection criteria will be the minus top of so it looks at looks for the top dot we the current directory and all the way under then you can also say like find dot and then slash type B which is then the type which is the directory and then it finds all the directories under the under under your current directory and anything like dot star as we know the hidden files start with dot in the as a beginning so again like when you do the find dot with type F and name is dot star then it finds all the hidden time again this all should be followed by also the second item which is what to do with this data so like dash print so here in the home there to exam dot text is what we want to find and then give you that basically and then it also gives you all the things under the home there too which is the two the four the five so now we come to another important command on the disk actually this is used to get the differences between two files so it compares the two files and reports what are the differences so there is a simple disk which is which basically compares the two files and then displays on a running actually it shows a running display of what are the differences and it tags one file by less than and then the other file by greater than to distinguish the difference between like those two and then it reports like all the lines which are different then there are specialized commands Vim disk file name one file name two or as this file name one from two these commands display the files side by side as a part of this Vim disk you can also have a PK disk which is using the TICL TA interface to create a UI so that you can compare those two files and the good thing about all these commands are you can display the the the differences and then you can merge the differences you will merge file a's differences to file B or file B's differences to file A in whichever way that you want to go to and then you can do several other things essentially you can even make both the files exactly the same so basically the differences between A and B motorizing A will go into B and B will go into A things like that so again this is another for two commands I want you to use this in realize the disk command is often used to find differences between two files either two scripts to run a particular tool or things like that and then it also helps us to find where what we changed which caused certain issues so you can compare the current version with the previous version to see like what changed and so then you can take some passive options against it or you can go the other way like to see that actually okay they are different then you can say say like okay throw away my old file and then I'll throw away my new file and then I'll use the old file things like that also you will do so it's a very helpful command and you've been using it and a lot in the real world rim this again it it shows side by side and uses the the editor called vi we will talk about this in the programming context in the next but there are these editor to edit files one such editor is them or so visual visual editor vi there are other editors like emacs is some of the one probably just heard about that vi is very common because it has a fairly easy to understand interface so we will like that a lot a couple of other things that I wanted to highlight are one is for the use of tab that you can use tab to complete a particular command or complete a particular file so this again is you don't have to type everything it's shaping the same today you can stop at some point and then let the tool continue and complete that particular role so that's also provided I also wanted to briefly touch upon one additional command which is lm or link so imagine you have a file system on your file which runs into like a couple of gigabytes or even more these days like I mean 50 hundred gigabytes is nothing and then now you have a another directory there a particular program that you want to run and which also needs this file as one of the now one way to do it is you can just copy that file over to the new location and then this particular fire this particular program will access that file and do whatever it wants and then basically it finishes essentially instead of that you can also think of another way where you don't copy the whole file into the new location which can lead to actually like two things one is to copy the file over to the new location since it is running into like multiple gigabytes probably to slow the whole system down take a long time for the particular file to be copied the second thing that it has is basically each of those memory like I mean now unnecessarily you are having like two copies of the same intervention and as a result you are just occupying the disk space with that and as a result some other important program will not be able to work because you are blocking the system with your your jobs so a better way is to actually link a particular file linking a file means essentially you are creating a pointer to that file in that directory so that the program that wants to access can chase that pointer and then go back to the the links can be created in two ways one is called the hard link and then the other one is called the soft link symbolic so let's look at what a hard link is a hard link is when we specify the command followed by the target file name and then the link so here both of them are arguments and the first argument is what the link that you want to create and then the second one is what sorry first one is the file name which you want to create a link form and then the second one is the name of the link that you want to it so if you specify ln target file name link me then it creates a hard link to target file name all link so why what is the why is it called a hard link so the hard link is because even though if you remove the original program the program content still lives under this form this targeting or essentially the link so if you want to delete a file it is not just enough to delete that particular file from the file system you also need to delete all the hard link references to that file so because otherwise like and still keep the data for the link to work and it will make it work the other command is ln minus there and then target name and then the link name this creates a soft link or a symbolic link to the particular time of the target so here if you if you have a soft link and then if you saying like okay delete my original file then the next time when you run the program generates an error saying that hey that particular file is not found even though like it exists in the symbolic link so you need to pretty much chase through the symbolic link find out where exactly it is and then add it to the file but the key thing is again the symbolic link you want the program to fail if it doesn't find the file so again so that is the reason why a symbolic link is preferred over a hard link in most of the programs in this lecture we covered a number of topics all very important and very useful commands grab if grab locate unique sort touch this t also like the link commands so I think this should give you a good flavor of the entire Linux system we will be start starting to talk about the Linux networking in the next session but with this actually like I mean we cover all the basics of Linux so and we will also have some more assignments that will be coming your way but we will also do a recap of this lecture in the next one when we start the Linux networking so again thanks a lot thank you very much and good luck