Using the command line

Hey there, this is the third part of the Linux Series.

Until now, we got an introduction to Linux and installed Ubuntu and used it GUI. At the end of the last blog post in this series, I’ve asked you guys to explore the GUI and do the common things you do on your Windows or Mac computer.

One of the most important parts of Linux OS’s is the terminal.

The terminal is what which is called as the Command Line Interface or the CLI.

So, why the delay let’s launch the terminal app.

Just as you launch your terminal app, you see a window as below. With some text in it. This is where you’ll be working on now. You need to use your keyboard and forget using your mouse from now on, to work only with the terminal. Believe me, once you master the terminal, you’ll realize it is faster than clicking things with your mouse.

On your first launch, you’ll see something like this,

Screen Shot 2017-07-12 at 11.32.39 AM
Terminal: First Launch

The cursor is ready, blinking waiting for you to type in commands through which you’ll ask the terminal do the jobs for you. That’s the reason it is called a Command-Line Interface. The line in which you see the cursor blinking is the prompt, with ‘techmaster404’ being my username and ‘ubuntu’ being my computer’s hostname or rather the name of your computer you gave while installing Ubuntu. The tilde symbol ‘~’, after the hostname followed by a ‘:’, represents the home directory which is the default directory which is opened when you open the terminal. This home directory is also the first default directory to open when you open the Files App.

Enough of the intro, let’s start learning some commands. Type ‘ls’ and press Enter. You’ll see something like this.

Screen Shot 2017-07-12 at 11.39.14 AM
Command ‘ls’ executed in the home directory.

The command gets executed and the output is the list of files and directories in the ‘~’, the home directory. And the prompt shifts to a new line waiting for the next command.

To make the window fullscreen. maximize the terminal app by clicking on the maximize button on the terminal window, in the top-left.

Each command in the shell (the prompt you see), has variations and options which you can give. You cannot possibly remember all the option for each and every Linux command, so to look up such options we have man pages (short for Manual Pages). You can access the manual pages for each and every Linux command by typing ‘man’ followed by a space then followed by the command whose man page you want to look at.

Let’s look at the man page of ‘ls’ command. Type in ‘man ls’ and press Enter.

Screen Shot 2017-07-12 at 11.45.38 AM
‘man ls’ : The man page of ‘ls’ command

Here we see, the various options we can give to the ls command. You can use your mouse here to scroll :), or rather choose to use the arrow keys. Go ahead and find the -l option, better called as the -l flag. So you found this flag, whose description says ‘use a long listing format’. To use this flag, or the syntax to use is in the ‘SYNOPSIS’ section of the man page at the very top. It says, ls followed by a space and then followed by an optional OPTION. Optional, because OPTION is in square brackets. Now you know which flag you want to use and the syntax too. Off to the shell. To go back to the shell, press ‘q’ in the man page to exit.

Now type in ‘ls -l’ command and press Enter.

Screen Shot 2017-07-12 at 11.56.08 AM
ls -l : Command executed

You’ll see something like above. The output means this,

Consider this one line,

‘drwxr-xr-x 2 techmaster404 techmaster404 4096 Jul 5 13:55 Desktop’

The first column, drwxr-xr-x part is the file permissions. First ‘d’ means, Desktop is a directory. This first character gives the type of the file it is. It is ‘-’ for regular files. Next 9 characters, gives us the permissions of that file to every user using the linux system. The first 3 of these, (the rwx here), are for the user/owner who created the file. He has all Read,Write,and Execute permissions. Next 3, the r-x part here, are for a group of users, the group of users who has access to the file. Last 3. the r-x part here, are for all that is every other person/user, who are not included in the previous two categories. The second column, is not important right now, I’ll talk about this later. The third is the owner name, the one who created the file. The fourth column is the group name, who has respective permissions to the file as I’ve said before. Next is the file size in bytes. Next two are the date stamp (last modified time) and the filename itself.

Check out the man page for ls -h flag. What it does is it prints the sizes in human readable units, that is in KB’s, MB’s GB’s etc.

You can use more than one flag at once, here if you want both -l and -h , you use ‘ls -lh’.

Screen Shot 2017-07-12 at 12.12.09 PM
Using ‘ls -lh’ : Command

As you see the file sizes are now in human readable format, here in Kilobytes.

Do check out other variants/flags for the ls command through the man pages.

Next command to learn is ‘cd’, short for change (working) directory.

Type in ‘cd Documents’ and press enter.

Screen Shot 2017-07-12 at 12.17.15 PM
cd Command

You see that the prompt changes from the current location, ‘~’ to ‘~/Documents’ that is you are now in the Documents folder which is inside the home folder. Now perform an ‘ls’ command. The output is nothing, meaning that you have no files/folders inside this folder.

To create a new file, use the touch command. Do ‘touch myfile.txt’ to create a file with name myfile.txt. Next do an ‘ls’.

Screen Shot 2017-07-12 at 12.21.46 PM
touch command

You see that the file is created. Now, use the ‘cat’ command to display the file contents. Do ‘cat myfile.txt’ to display the contents of the file myfile.txt. The output would be nothing as the file is empty and you have entered nothing into it.

Let’s learn a command called ‘echo’. What it does is that, it prints what you give to it. Do echo “I am a cat!” , and the shell prints ‘I am a cat!’.

Now we can use this output to give to our file by doing echo “I am a cat!” >> myfile.txt. What this does is it appends the output of echo, “I am a cat!” to the file ‘myfile.txt’. Appending, means to add to the ending of the file. If you give only a single > instead of a >> , that would mean overwriting the contents of the file with the echo output.

Try appending, “I am a cat” to myfile.txt and then print/cat it’s contents.

Screen Shot 2017-07-12 at 12.29.41 PM
Appending to myfile.txt

You shall now see, that the file myfile.txt now has the contents, I am a cat!.

Keep checking the man pages of the commands, to get to know and use other flags and options you might need.

Next, do a ‘cp myfile.txt copied.txt’ to make a copy of the name copied.txt of the file myfile.txt. Then do an ‘ls’ to see both files. ‘cat’, copied.txt to check if the content’s are the same.

Screen Shot 2017-07-12 at 12.37.06 PM
cp command

Use the ‘mv’ command to rename files. Do a ‘mv copied.txt mycopy.txt’, to rename copied.txt to mycopy.txt. Do an ls to verify. You can use the mv command to move files. Do ‘mv filename newlocation’ to move the file to the newlocation. The locations in the terminal is in the form of an expression containing backslashes, just as now you are in ‘~/Documents/’ . To view the location you are in use the ‘pwd’ command, short for present working directory.

Screen Shot 2017-07-12 at 12.41.43 PM
Using the pwd command

The output is “/home/techmaster404/Documents”. The ‘~’ symbol is short for “/home/techmater404/” which represents the home directory of the current user.

To move the file ‘mycopy.txt’, to the Desktop, in the Documents directory, do ‘mv mycopy.txt ~/Desktop”. Now check your Desktop, to check it has the file mycopy.txt.

To change the current directory to Desktop, do ‘cd ~/Desktop’ and do an ‘ls’ there.

Do ‘cd ~’ , to cd into home directory. Do, ‘cd ..’ , to cd into the previous directory, that is if you are in “~/Documents” and then do a ‘cd ..’ , you’ll cd to “~”, the home folder.

To remove the file mycopy.txt, do ‘rm mycopy.txt’ which removes the file. Do an ‘ls’ to verify.

Screen Shot 2017-07-12 at 12.49.12 PM
rm command

You now, know how to list files in a directory, make files, rename them, remove files, move them, make copies of them, list a file’s contents, navigate the file system using the cd command, show the current working directory using pwd, how to use man pages, how Linux paths work, how to append to files using echo etc.

That’s might be too much to digest to newbies in one go. But that’s a lot of skills for one post. Do keep reading the man pages for other options and documentation.

Reread this article and keep practicing commands until you feel free to use them.

This is just the tip of the iceberg. Let’s explore lot more in the further posts. Keep following.

Techmaster404.

P.S : Keep Experimenting with the commands , that’s how you’ll learn the command line well.