Hi and welcome to the course.
If you already have access to a Linux machine or a Mac machine, you can simply open up the terminal and start with the rest of the course by skipping this video.
However, if you’re on Windows, I’ll show you how you can set up a virtual machine on Windows so that you can get to the command line as quickly as possible. The first thing that you need to do is download VirtualBox.
If you already have VMware or another virtual machine software you can use that as well, but VirtualBox is free and quite small so you should be fine with it.
You can go ahead and download the Windows hosts file from the VirtualBox site, then you can head over to Ubuntu MATE, ubuntu.-mate.org/download. The reason I’m suggesting Ubuntu MATE is that it has a 32–bit version available and VirtualBox will only run the 32-bit.
This is a minimal resistance path. So you go to ubuntu.-mate.org/download and then you download the 32-bit version from this site over here. If you are comfortable with setting up Linux you can go ahead and do whichever distribution you want. It doesn’t make any difference to our course. Then you go ahead and download Ubuntu MATE 17.10.1 or any other version. You can download the ISO file and we’ll show you how to set this up in the virtual machine.
I’ve downloaded both of these files and we’re going to start with the setup of VirtualBox, so we are going to run it as administrator because it’s going to have to make some changes to your system.
When the setup comes up all you have to do is keep hitting next and agree to basically anything that it says, and it should set up quite cleanly on your system. It’s going to ask for confirmation quite a number of times because it has two setups and some networking bridges so once that is done it’s going to start using virtual machine software for us.
Let’s go ahead and view this.
What we want to do is create a new virtual machine. For that, simply click on New Give this virtual machine name and then select the Linux type so we’re going to have Ubuntu 32-bit over here because we don’t have a 64-bit option in VirtualBox at the moment. You can simply select Ubuntu 32-bit, give it a name. I’m going to call it old box and hit next and basically the default should work fine for us.
We can keep hitting next and it will ask you to create a virtual hard drive, click Next, everything should be fine. The defaults work fine in most cases so you can go ahead and create that.
Once the virtual machine has been created, we want to insert our CD.ISO that we downloaded into this virtual machine. In order to do that, we have to go to settings, go to the storage tab, go to the empty, and in the optical drive select choose virtual optical disk file.
Once the window comes up you select the Ubuntu MATE ISO that we downloaded earlier. Click open and then once it has been mounted you click OK.
Now the cd-rom has been inserted and we can start the virtual machine by clicking on the start button. This is going to take a little while for the first time so when it starts booting up you will see the boot screen, and the startup screen for the CD drive should appear in a while.
When the CD has finished booting up, you will see this screen or something similar to it. You can click on install Ubuntu MATE to begin the installation process.
You can simply select the defaults and keep them unchecked, you can click continue and this will bring up the next screen. Linux installation in a virtual environment is very easy.
You can simply go ahead and select erase disk and install Ubuntu MATE because this is working on a virtual disk and it’s not going to affect your host operating system.
When you click on Install Now and then it should start installing the software after giving you this pop up which you can click on continue. You will see some other screens that ask you for time zone and your name, so we can click on continue or select your time zone and then click on continue.
Then we have some keyboard layout selections, the defaults should work fine because it picks it from your host system. You can click on continue and when you get the user information screen, make sure you enter your name and your computer’s name. I’m going to call it oldbox, you can name it anything that you want. Pick a username, nauman, and you can pick any user name that you want and choose a password. Since I am in a virtual machine I am going to pick a very simple password and click on continue.
Beyond the screen the installation should run uninterrupted, and you should have the Linux operating system installed in your virtual machine. I’m going to skip ahead in the time when you have to do something.
When you see the installation complete message you can go ahead and click on restart now, and this is going to restart your virtual machine with your new operating system.
When you see this message you can go to devices, optical drives, and remove discs from virtual drive. Force Umount to make sure that the disk has been removed in case you get this message, and then your virtual machine should reboot to the new operating system.
On the login screen, enter the password that you input during the installation and you should be able to log in to your account.
Go ahead and close the welcome screen and then go to applications, system tools and MATE Terminal.
If you installed another version of Linux you can start its corresponding terminal and you will be at the command line. Sometimes we are going to have to run multiple terminals so that you can go to file and Open Terminal and you have two terminals open so we will let you know when you need those. For now, your installation is done and now you’re ready to begin experimenting with the next lectures.
If you will enjoy reading and contributing to the discussion for this post, will you please join us on the YouTube video above and leave a comment there because I read and respond to most comments on YouTube?
If you find anything helpful in this video or funny, will you please leave a like because you will feel great helping other people find it?
Section Two: Linux Basics
Welcome to the course. Let’s get started. As the name suggests we’re going to go from Zero to Expert so if you have had a little bit of experience with the command line before you might find the initial few lectures a little bit boring, and a little bit too basic, but I would strongly suggest that you do go through them at least.
You can put the speed to 2x but please do go through them so we’re sure that we’re not missing anything.
Let’s begin by taking a look at what the difference is between Windows and Linux file systems.
You should be aware of the hierarchy of folders and files in Windows. So at the root you have what are called the Drive letters. So you have C:\ drive letters over here and within this you have what are called folders.
In Windows, you might have a Program Files folder in C:\, you might have a Users folder, you might have a Windows folder or any other folder that you might want to create.
Within the user folder you have your own home directory for me, it is going to be Nauman and within that I can create further directories.
That is essentially what the window hierarchy looks like. Folders within root drives. If you have a D Drive that is going to be completely separate free from this one.
Windows are arranged in drives so drives are the route letters of these trees.
Linux is a little bit different, what it does is it puts everything under one root which is called surprisingly, root. This / stands for that root. Everything, every different partition that you have, every drive, every external device that you add to Linux is going to go within this one tree.
It is a singular tree and within this tree everything is going to fall under this /. This is called your root. If you have a home directory that goes into the / yhis is going to turn into /home just as we had C:/windows. Within this home, we are going to have my home folder so this nam is my home folder because this is my user name, and within this I can again create my hierarchy of folders. Except in Linux terminology, we are not going to call them folders we’re going to call them directories, so a minor difference in terminology.
In Windows a path looks like this.
So we’re going to have C:\Windows\System32\file.txt, whatever the extension is. In Linux the convention for separating one folder from another or a file from a folder is a little different again.
All the Linux flavors use /, so this / is our root and then we have a directory, and then we have another separator which is / forward slash instead of a (back slash) \, and then another directory/, another directory /, file.
That is the primary difference that we have between paths in Linux and Windows so you should be aware of this that you should be using / (forward slash) whenever you’re working with Linux.
Once we have that understanding, we need to take a look at one last concept that is that /home/nam/ is my home directory. In windows typically you can create another partition and that’s what normally people do that they create another partition for their documents and they put them over there for safekeeping.
In Linux, everything that I do is going to go into my home folder /home/nauman. There are ways of creating different partitions for different users but we’re not going to go into that right now. What we should be aware of is that typically in a Linux environment you are going to be working within your home folder so for me it’s going to be /home/nam. For you, this is going to be whatever username you picked during the installation as we saw in the last videos.
Another thing to remember is that for instance, I have /home/nam as my home directory this can also be written as a ~ sign. This (tilde) ~ is next to the exclamation mark so you have to add on top of two you have the exclamation mark on top of one and then you have the tilde ~ to the left of the exclamation mark.
This (tilde) ~sign stands for home directory in almost all Linux flavors, so you should remember that. You can see that in my home folder so this is my home folder, you will be seeing this something very similar to this in your Linux environment. In my home directory I have created a folder called cli.
You do not have to create this when you start the terminal, your command line it’s going to be in your home directory and you can immediately start working over here. I have just kept it separate for ease of use.
So once we are over here we are going to immediately start working on some files. Let’s go ahead and issue a command.
So this is a command line, every interaction is through commands. So I’m going to say in date and it’s going to show me the current date.
That’s how we interact with the Linux command line, we issue commands and we get output from it.
Let’s see another command. We have cal and that shows me the calendar for the current month.
Immediately what you would be wondering is how am I going to remember all these commands? Don’t worry with just a little bit of practice let’s say a week you’re going to be able to recall all the commands that you need very easily. It’s not that difficult. We’ll see how that happens.
By the way, if you’re wondering why your terminal looks kind of bland without all these colors, we have a separate video by the end of this course in which we tell you how you can make your terminal look like this. If you’re going to stare at your terminal for a long while it might as well look pretty.
Well, let’s go ahead and download some of the files that we are going to be working with. So if we were in a GUI, I would tell you to go to a URL download the file to a folder, copy it somewhere, and then extract it, and you would get the files. This is where the power of the command line comes in.
I am simply going to tell you to issue the wget command, so this is a web get command. Get it from the web and we’re going to say wget http://bit.ly/cli-files. With that command you have all the information that you need to download these files, and so issue that it’s going to go ahead and it’s going to output some of the stuff, and it’s going to make the file available to you. It’s telling you over here that cli-files are now available.
Let’s go ahead because I know that this is a zip file. Let’s go ahead and unzip it. Unzip is the command that unzips the zip files, so I’m going to say unzip cli-files and it tells you that it has extracted all of these files which are going to be in this folder cli-files-0.0.
All of these files have been extracted. You might see a few more than these as I add more content to this course but at the moment, that is what we are going to have.
We want to see what kind of files we already have over here. We will go ahead and issue the command ls. So when we say ls that means to list the directory and we see that we have this file over here that we just downloaded, and this folder over here that we have extracted that has become available as a result of extracting this zip file. It doesn’t have the .zip extension over here but Linux doesn’t really care about the extensions as long as the file contents are properly available.
What we want to do is because we’ve extracted the file and we’ve taken a look at it, we want to clear all of this junk that is available on the screen. We want to get rid of this so that we have a clear terminal so that we have the command clear, so issue that and everything goes away and now we can simply do ls again to see what kind of files we have.
Note that we don’t have any spaces in the folder name over here. If you’re coming from a Windows background you would be very used to having these folders that look like this, so it would look like CLI Files capitals and spaces and everything is just mixed together. Even the Windows program files look like Program Files. It looks like that with the space in it. It’s a really bad idea to have spaces in your folder names in your file names. It’s one of those things that leads to a lot of problems down the line and we’ll see why this is in one of the future videos, but for now just take my word for it that you want to make sure that your folders do not have spaces in them.
Your folder names should not have spaces in them. It leads to a lot of headaches down the line and whenever you post a bug somewhere you ask somebody for help and they see some spaces in your file names, in your folder names, that’s the first thing that they’re going to tell you to fix. Make sure that you get into the habit of replacing all the spaces in your folders with a dash, and it’s in general a good idea to have everything in lowercase.
As you can see LS is not a command, so it’s a command not found. That means the Linux command line does not recognize the capital LS because the command is lowercase ls so this is case sensitive.
This is a very brief introduction to how you can issue a command and get some feedback from the console about the output of your command. I also shared with you a very important tit-bit about how to name your folders. In the next section we’re going to start doing some more interesting stuff.
Section Three: More on Command Lines Operations.
Let’s continue with our command line operations. So we’re going to change our directory, so currently we are in the /home/cli. For you it’s probably just going to be a home directory. So what we want to do is we want to change our current working directory to this folder over here that we just extracted.
For that we just said cd cli-files-0.0 and so we hit that enter, and now we are over here. We can hit ls again to see what we have in this folder.
Here are some folders. All these blue ones are folders and this README.md is a file, so we have these files and folders over here. What we want to do is first take a look at how we can create our own folder. If you are in a GUI what you would do is you would find the file menu or you would right click, and then you would say new, and then a folder. It takes approximately five seconds.
In the command line you’re going to say mkdir and we will see how we can speed this up but this is already pretty quick. Mkdir temp, say enter, and now this temp directory has been created.
How do we know?
We can say ls and as you can see this temp directory has been created. If you want to change to this directory, we can go ahead and say cd temp, enter, and now we are in this temp directory over here. You can hit ls and we see nothing because the temp directory has just been created, and it has no files in it.
We want to go back to our parent directory so we hit [cd..].. So this might be familiar from the background of your windows. As you can see there is nothing really difficult going on over here.
Another way is to go to cd temp and if you want to go back to whatever directory we were working on prior to coming over here, we can hit cd -. This is very useful. It’s going to come in handy in a little while.
What we want to do is to first hit clear so that we have a clean slate, and we want to create a directory d3 within a directory d2 within a directory d1.
In a GUI, you would create a directory, go in there create another directory, go in there, and then create another directory, right? Here we can simply say mkdir d1/d2/d3. That means creating a directory d1 within that directory create another d2, and within that create another directory d3. Except when you do this you’re going to get an error.
What that means is it’s trying to create this d3 when d2 does not exist and so for that what we can do is we can say mkdir -p d1/d2/d3. p essentially means that you create them one by one so that you don’t get this error over here. You hit enter and now you can see that if we do ls, we have this d1 over here. We can go into d1, cd d1 and say ls. We have d2. We can go into d2 and then we can say ls, and we can go into d3 similarly and we can say ls, and now this is empty.
If you want to go back to our parent directory so this is going to be the parent director of d3 which is d2, and then we are in d1.. and we are in our current working directory.
Another way to do this is to go to cd d1/d2/d3 so you are over here and now you can hit cd – and you go immediately to the previous working directory that you were in. This is quite handy when you’re working on this, and this comes in handy when you’re trying to create several directories in a hierarchy and you know where you want things to be, and this becomes really easy and it’s a real great time-saver.
As I mentioned earlier, we don’t just want to tell you what the commands of Linux are, we want to motivate why you want to use them. This is the theme that we’re going to follow throughout this course.
Everything that you can do on the command line it’s going to save you time. It’s not just that it’s more powerful it also saves you time. If you have a terminal open and you’re trying to do something that is productive, and you’re trying to get things done, you have to fight your GUI that’s going to take a lot of time.
Once you have an understanding of the commands, they come in naturally to you, and you’ll be able to type them really quickly. We’re going to take a look at how you can speed the typing up even more, but even if you’re typing slowly it’s still going to save you a lot of time.
To recap, we created a directory and then we created a hierarchy of directories, and now what we want to do is we want to see what kind of directory structure we have in d1.
We can say ls -R d1.
You will notice over here that up until now we have been giving some commands and then we are passing some arguments to those commands. Now this is a special type of an argument and this is called a switch. So -R is a switch which says that I should be showing the d1 directory recursively, so R stands for recursively, and when you hit enter you will see that it will show you that d1 has the contents d2, d1 /d2 has the contents of d3 and d1, d2, d3 is an empty directory.
ls -R you can use it to view the whole hierarchy within a specific folder over here.
We’ve done a little bit of stuff with the directories. In the next section, we’re going to take a look at how to manipulate files, and what kind of things we can do with files and we’ll get to some really interesting stuff.
Section Four: Linux Current Working Directory
I’ve been using the term current working directory very informally up until now, but now we’re going to see what it means.
We have this information over here which is before our prompt and that tells us where we are currently standing. This is called your current working directory. Sometimes depending on the shell that you’re in this might not be visible over here, and you might lose track of where you are.
If that’s the case you can hit a command pwd which is print working directory and you will be told exactly where in your current file system you’re standing.
Even if you can’t see this one over here, you can simply hit pwd and you will be shown where you are, so let’s go ahead and create a file now. We’re going to say touch – touch essentially means create a new file if it doesn’t already exist – and give it a name. This is going to be hello.txt, so we’re going to touch hello.txt. If you ls you can see that this hello.txt has been created.
Now, we want to see what is within this file. So we can say cat which is short for concatenation, and essentially what it means is take the contents of the file that I’m going to tell you, and concatenate them or put them over here on the console. We’re going to say hello.txt, but because this is an empty file we don’t see anything.
So let’s go ahead and take lesson 01. So we can say cd lesson-01. We are going to ls and we see that we have a dummy file over here. We can say cat dummy – file.txt and hit enter, and you will see that the contents of the file have been output over here. That is how you see what the file contents are.
If it’s a very large file this is going to flood your console, but we will see how to take care of that later on. For now, what we want to see is how many words, for instance, are in this file. We want to do that. For that, we have a command which is called wc which is for word count, and then we say dummy – file.txt.
So it’s the same concept. Cat is a process, it is a command that takes a file name and simply outputs it to a console. Wc is a command that is not output to the console, instead it counts the words and writes down that count on the console. It has some output over here 5 38 185.
How do we know what these numbers really mean?
For that we have a separate command – it’s kind of a meta command – which operates on commands and helps you out. We can say whatis wc. So it tells you that wc is a command which prints a newline, word, and byte counts, for each file. This 5 is the new nine which essentially means how many lines it has, how many words it has, and how many bytes it’s taking.
This whatis a is a very useful command. Whenever you come across a command and you’re not sure what that means you can simply say whatis and then the command name, and you will be given a hint on what that command means.
We can say whatis cat and it says it concatenates these files, and prints them on this standard output, which is your console over here.
Another command that gives you a lot more information than whatis is called the man, so this is short for manual and if you say man wc it’s going to open up a huge file that is going to tell you everything that you need to know about WC.
It tells you that it prints whatever and then it tells you what options you can give it, what each option means, what all these switches are that you can pass through it, whatever you put, what every switch does, who the author is, if you’re reporting bugs, a lot more information than you’re going to effectively need.
It tells you how to get rid of this space so you can simply hit q.
What we’ve done over here is take a look at the basics of creating files and performing some simple operations on them such as outputting the console output, such as outputting the contents of the file to the console, and counting the words.
Now, we’re going to finish this section by taking a look at a very simple concept which is we can ls lesson-02. Lesson-02 it’s going to tell us that we have these files in lesson-02 folders. If you just say ls is going to tell us what the current directories content is, and if we pass it to a folder it’s going to tell me what is in there.
We can also say ls lesson-02 /*.csv so it’s going to tell me all the files, it’s going to list all the files which match this patterns, so anything .csv. There is only one file over there in lesson-02/iris.csv, so it tells me what csv files are present in there.
In the next section, we’re going to see how you can speed up your command input for instance, what if I want to do this again after a little while? Do I have to retype it? It seems like a lot of work, so we’re going to see how you can speed up your command input by looking at a couple of different ideas.
Section Five: Speeding Up Linux Command Line Interactions.
In this section, we’re going to take a look at how you can speed up your interaction with the command line.
The first thing that we want to do is to issue this command again. I want to say ls lesson-02/*.csv so instead of typing all that I can simply hit the up arrow key, all of this appears just as I had entered it, and I can simply hit enter, and it will issue the command again.
I can hit the up arrow key and left to modify it so for instance, I want to do lesson-03. I can do that as well. It is telling me that it also has all of these commands.
It also has the same csv file. That’s the first thing that you need to understand. You can hit up, up, up, and it’s going to keep cycling through whatever I have in history. All of these commands that I entered earlier on, they are sitting over there in my history and they are available if you keep hitting up, and they’re going to be visible to you. That’s the first thing that you should be aware of. You can hit up and you can cycle through the previous commands. So essentially, it’s a free macro recording of whatever you did earlier.
If you perform a very complicated operation such as compressing a folder into a file, and then copying that file into some other location, everything that you have performed earlier on that can be done in an instance.
If you’re not sure when you did the command you can hit another special command which is called history, and this is going to list everything that you did, so all of these commands that I have been issuing over here are visible to me through the history command.
It also has a number over here, for instance, I can take a look at the cat dummy-file.txt command. This has been given to the number 540. If I want to issue this command again, I can either go through the up key cycle and keep doing it until I reach over there or there is another easier method which is called a bank command, so you can say a !540. You can simply hit enter and it’s going to execute the command again so if you don’t have that file over here dummy.file.txt so let’s go ahead and say ls and that was in lesson-01, so let’s say ls dummy-file.txt is over here, and I can say !540. The whole command is going to be executed.
Even if I was doing something else and I remember that 540 has this command there are certain commands that you keep doing again and again, for instance, copying a file to your server. If you’re going to be doing that repeatedly, your brain is going to memorize what the number of that command is. You can simply hit !540. What if I don’t remember what the number is? Then there’s another way.
Let’s go ahead and clear this up. If you don’t remember what the number of the command was, you can simply hold ctrl hit R so that’s Ctrl+R and you can say wc and then you can see that the commands matching with wc are available. You can simply hit enter and that command is going to be executed.
Now, we’ve seen three methods of entering the same commands that you have previously entered.
One is through the up arrow key, another is by issuing the history command and then you have the Ctrl + R, and then you type whatever you remember, and it’s going to search the command in your whole history.
Using those three commands, you can speed up the input of your commands to the terminal. You should get into the habit of using these especially the Ctrl+ R. It saves you a lot of time when you get comfortable with it.
Let’s begin with some other exercises. If you don’t have the terminal already open make sure you start it, and then hit pwd to make sure that you are in a working directory of cli files. We are going to go over to our lesson-02 and we’re going to take a look at the files over here.
What we have is iris.csv file and what we want to do is we want to take a look at what the contents of this are. As before we’re going to do cat iris.csv and when we hit enter we see that it is a data set of classes belonging to three different species of flowers. It’s not really important what the data set is, all you need to know is that these are some of the features of the data set and these are the classes.
We have three types of classes; iris-virginica, iris-versicolor, and iris-setosa. We’re not interested in working with the data set but this is a file that we are going to be using as a case study. The first thing that we want to do is measure how many data points we have. That is going to be the number of lines. So what we can do is we can say wc iris.csv, and we will see that we have 150 data points over here.
Another thing that we are interested in is finding out if this data set or this csv file has a header at the top of the file. I can go ahead and open this file in an editor or I can write a Python script for this, but really we have a very, very handy utility for this which is called the head. We can say head iris.csv and all it’s going to do is it’s going to output just the top 10 lines. These are the 10 lines at the top of the file and as you can see there is no header over here. The data set immediately begins with the data points. It’s not telling us what these things are, so these are the first 10 lines of this file.
Another thing that we can do is we can say that we want to cat iris.csv file, and we want to pass it to wc. This is another way of doing the same thing that we have done over here wc iris.csv. It’s the same objective but we are going to do it in a slightly different way and you see why, so we’re going to get the whole output, and then we’re going to type the pipe | symbol. This is the pipe symbol | vertical bar and then we’re going to pass it to wc. What this means is that you should take the output of the cat iris.csv. This command output should be taken and it should be input to the wc command so these two processes are going to be running in parallel to whatever the output is produced by this file. This command is going to go into wc and by the end of the output produced by this one wc is going to have counted the number of lines.
This is a very powerful construct over here. The people who wrote cat and the people who wrote wc had no idea that we were going to bridge them together using this | pipe symbol.
What they did know was that the people who wrote cat knew that they are going to output these files content somewhere, and these wc people knew that they are going to input data from somewhere, lines from somewhere, and what this pipe | symbol does is, it creates a medium between cat and wc. Whatever cat produces is fed to wc. This leads to very powerful constructs and it’s extremely decoupled coding because cat does not know what wc wants, wc does not know where the input is coming from, but they can work together to create very powerful constructs for us.
Now, this we could have done without the pipe symbol | but we’re going to come to an example which really helps us figure out why the pipe symbol is important. Before that let’s do another example and that is going to be grep. So what grep does is it goes into a file, let’s say iris.csv and find for us a specific string. We are interested in the lines which contain the word setosa so we’re going to hit that. What this means is grep setosa iris.csv so it’s going to return all those lines which have the word setosa in it and it’s going to omit all the lines which do not have the word setosa.
When we hit enter we get this output over here and as you can see all the lines that have been output do have the word setosa in there.
This grep here works on regular expressions. If you are not aware of regular expressions I would highly recommend that you go ahead and learn them. They are very useful too, but they are really outside the scope of our current tutorial. All you need to know right now is grep takes a regular expression and this setosa is kind of a regular expression because it has specific letters over here. It’s going to search for this specific word and only output those lines which have this stuff. Another way that we can do it is we can say cat iris.csv | grep “setosa” in whatever is being fed to it, and that whatever is coming from this cat file.
We are trying to cat this thing and we’re trying to grep it, so now it’s going to produce the same thing except now, we can do something really interesting. What we can do is in this iris.csv file find these setosa lines and then count them cat iris.csv | grep “setosa” |wc. What this is going to do is it’s going to tell us that we only have 50 setosa data points. What we’ve done is we’ve created a counter utility which counts selectively, so this is the selection and then wc is the counting.
This is a very powerful construct you should get into the habit of understanding this stuff and trying to use it whenever you can. Of course, you can write a simple script in any language of your choice, but this is readily available and you can run these tools at a moment’s notice. They are very good. Another example we can do is cat iris.csv | grep “3.5”. We can say all the lines that have 3.5 in them, and it’s going to output everything that has 3.5.
Another thing that we can do is we can say cat iris.csv | grep “setosa” | grep “3.5”. Now, we have all these setosa lines which have 3.5. You can chain them together to your liking, for instance, we can go ahead and say cat iris.csv | grep “setosa” | grep “3.5” | wc and those are six. This is a very useful concept.
Another thing that we can do is we can say ls | grep CSV so it’s going to find all the files which have the word csv in them. You can go ahead and experiment with this and try to find, for instance, all the data points, all the lines in this iris.csv which has versicolor in it and 2.0 in it.
First filter them out then count them. Do these exercises and make sure that you understand them really well because they come in very handy when you’re working with large files. Because these utilities are extremely efficient. If you do them using your own code you would have to do a lot of optimization to get to the speed that these utilities can give you.
Let’s go ahead and clear the output, go back to the parent directory, and head over to lesson-03. What we can do is we can go to cd../lesson-02 like that, so parent directory lesson-02. We’ve done that already so let’s go ahead and take a look that we already have a iris.csv in this folder over here. We can output something on the console, so echo “Something”. Echo essentially outputs whatever you get to the console. Let’s cat temp folder over here temp file. As you can see the temp file is empty at the moment.
What we want to do is you want to echo “Something” > temp directory. When we do that you will notice that something is no longer output to the console. The reason is that it has been redirected to the temp file.
Notice that this is different from the pipe | symbol in which the output of one command is passed to another command echo “Something” > temp. This is greater than the bracket > symbol or greater than the symbol or the angular bracket, this stands for redirecting the output to a file. This has to be a file over here, not a program that you can run. Whatever output is produced by this command, whatever it is, it is redirected to temp.
Let’s take an example to see what we mean.
So we can say cat iris.csv | grep “setosa” from it. It outputs this thing over here. What we want to do instead is to output this or redirect this cat iris.csv | grep “setosa” | setosa.csv. Let me go ahead and make a slight mistake over here. A mistake in double quotes so that I’m going to use that to show something later on. I am going to redirect this file over here. We can say setosat.csv and it’s going to output on everything that is in that file. What we’ve done is we’ve taken this file and we’ve filtered out some of the none setosa data points and we are left with only the setosa’s data points which we save to another file. This is a very handy tool for splitting files according to some criteria.
As you can see I made a mistake over here setosat.csv instead of just setosa.csv. Let’s fix that. We say mv setosat.csv setosa.csv. If you’re moving a file and you are moving it to the same directory with a different name, it means rename the file. When we do that notice that setosat.csv is no setosa.csv.
Our file name is correct using the mv command. So this mv command is very useful. You will come across it again and again and most of the time, it’s used for the purpose of renaming files instead of moving files. It means to move this file to this file which renames it. Now that we’ve seen the mv command let’s take a look at its sister command which is the cp command. What cp does is it creates a copy, so let’s go ahead and create a copy of iris.csv as iris-backup.csv. What this is going to do is it’s going to create iris-backup.csv file which is going to be the same as iris.csv.
What we want to do is we want to create a directory for backups so mkdir backups – make directory backups – as before, and we’re going to hit enter. This has been created now and you want to move the iris-backup.csv to the backup folder. You want to move it to backup and you want to name it iris.csv, so that’s what this means. Move this file to this file and this file happens to be within a folder, so we hit enter, and now we can see that the iris-backup.csv has been moved. Let’s see whether it is available here, and it is now here with its new name iris.csv. That’s how this works.
What we want to do is we want to go ahead and remove the file. For that, we have the command rm and we want to remove the setosa.csv file so that’s gone, so rm setosa.csv. What we want to do is we want to create a copy of the backup folder so what we can do is cp backup backup-2. When we try to do that we get an error. It’s telling us that it’s omitting the directory backup.
The reason is that cp can only work with files by default. If you give it a directory it’s not going to work with it, it’s not going to create a copy. For that, you need to do a simple switch which is that you are going to say cp -r backup backup-2. Now, this is going to work perfectly well. We have the backup folder over here, we have the backup-2 folder over here, and anything that was in the backups is now going to be in the backup-2. Both of these are the same directory.
Remember when you are trying to create a copy of a directory, you want to pass the -R switch which stands for recursive. If you recall we also had a – R switch in ls and it’s kind of the same deal. What we want to do is we want to remove this backups folder so rm backup. Again, the same issue it’s not going to delete it because it’s a directory. What we can do is we can rm backup /iris.csv then go ahead and say rmdir backup, so that works but this becomes quite problematic when you have quite a lot of files within a directory.
Another thing that we can do is instead of deleting the contents of the folder, and then deleting the folder, what we can do is we can simply say rm -r backup-2. This is going to delete the folder, the directory, and anything that is within that directory.
You have to be very careful with this. There is no recycle bin or trash here. If you delete a directory it’s gone unless you go through the trouble of undeleting utilities and restoring from back ups.
GET MORE OF THIS COURSE
Would you like to continue learning about the Linux Command Line – From Zero to Expert Course? If you are interested will you please buy the complete course, Linux Command Line – From Zero to Expert Course, on the Uthena Education Platform..
You can also get the first 1 hour and 3 minutes of the course completely for free on YouTube.
Thank you for reading the blog post or watching the course on YouTube.
I love you.
Thank you very much for checking out the Linux Command Line – From Zero to Expert Course and I hope to see you again in the next blog post or video.
You may like this post: Learn Elixir Programming! Elixir Language Fundaments with Mohammad Nauman, PhD