Command Lines Basics in Linux

What are command lines?

Command lines are text-based interfaces used to write commands directly onto your device's OS. Similar to how tasks can be performed on windows using clicks and icons, tasks can be performed on the command line using commands and operands.

For this tutorial, we will be using Lubuntu, an OS based on Linux. In fact, any OS that has the same underlying commands as Linux will work just fine if you wish to follow along.


How do I access my Terminal?

Click on the Start button on your taskbar and go to System Tools > QTerminal. You should be met with a screen like the image on the right.



Command Lines for navigation

Now we are ready to start writing some commands. Those commands operate on the computer's directories, similar to how you would use a File Explorer on Windows.

To ease visualizing the processes, please find included the corresponding directories as well as the changes made as screenshots after the commands.

Before we start:

Keen-eyed readers may have spotted the 'man' command at the terminal. This takes in the name of commands as an operand and produces an explanation of the command. This will be further elaborated on but feel free to use it to gain some insight into your commands as you follow along.

Also, for the sake of clarity, I will be clearing my Terminal so that past, irrelevant commands do not appear again.

Print Working Directory

Print Working Directory is a command used to indicate which directory the user is currently on.

Syntax: pwd


For instance, after typing the command and hitting Enter, the terminal tells me that I am currently on /home/lubuntu.


Which, using a GUI File Explorer, means I am here.

List

List is used to output all the directories/files found within the directory we are currently on. Notice how on the Terminal above, while we were told which directory we were in, there were no mention of its contents, we can use list to remedy that.

Syntax: ls

The L switch

You can include an " -l" after the "ls" command so that the output come out in a list format. Note that there is a space between the command and the switch.

Syntax: ls -l

The A switch

You can use this switch after the "ls" to list hidden directories. Try inputting "ls -a".

Syntax: ls -a


Clear

By now, your terminal could be quite a mess and hard to read. Use "clear" to reset the terminal back to its starting point.

Syntax: clear


Becomes:


Change Directory

cd is used to navigate across directories on your terminal itself. 
It can be used in several ways. Note that there is a space between the command and the operand and that operand itself is case-sensitive; "Downloads" and "downloads" are not the same.

Syntax: cd operand(optional)

Using Listed Directory as Operand

Let's say we wish to access a directory from what we have seen using ls. We could simply type out "cd" followed by a space and the name of the directory or file.

Syntax: cd directory/file name

In this example, I will be accessing my Downloads folder.


Now, if I were to use pwd again...


...my working directory is now Downloads.


Above is the GUI equivalent.

Go back

To go back one directory, we use this command:

Syntax: cd ..

In my case, I will go back to '/home/lubuntu' from '/home/lubuntu/Downloads'

Go to home

To go to your /home directory you can simply input the cd command with a space as your operand. Note that this works while being in any directory and not just while in a directory inside /home.

Syntax: cd 

Go to root

This shortcut allows you to go to to the root of your directories.

Syntax: cd /


Go through multiple directories

You can also use a / to navigate through multiple directories at once.

Syntax: cd directory name/directory name etc.

In this example, I will navigate from my root directory to /home then /lubuntu then /Downloads in one line.


Command Lines for file manipulation

In this section we will discuss the multiple commands that allow for the creation, editing, deletion and movement of files while inside the terminal. The changes made to the files in this simple exercise will be displayed as screenshots for both in the terminal and the File Explorer.

Make Directory

This allows you to create directories within your working directory. 

Syntax: mkdir directoryname

In this example, I will create a directory called TestDirectory inside Documents.



As you can see, my Documents folder starts off completely empty.



After the process, it contains the TestDirectory folder.

Remove Directory

This allows you to delete unwanted empty folders/directories.

Syntax: rmdir directoryname

In this instance, I have created an empty directory called Directory2Delete and then removed it using rmdir Directory2Delete.


Touch

This is used to create files within directories.

Syntax: touch filename

I will now demonstrate how to create a file called TestFile.txt inside Documents.



Echo

This can be used to either output a message to the screen

Syntax: echo "message"

Or to write some text into a file

Syntax: echo "content">filename



Concatenate

Used to display the contents of a file.

Syntax: cat filename


Copy

Used to copy the contents of a file into another. This also allows for renaming. 
Syntax: cp filetocopy newfilename 

I created a copy of TestFile.txt, which I named CopyFile.txt


Copy to

You can also include a destination to copy the file into any other directory you want.

Syntax: cp filetocopy newfilename destination

Now, I will make a copy of CopyFile.txt and paste in into TestDirectory.



Move

Used to rename a file 

Syntax: mv filetorename newfilename

Or to move a file into another directory without copying the file.

Syntax: mv filetomove destination

This how to rename TestFile.txt to RenamedFile.txt and move it into TestDirectory.



Remove

Used to delete a file in your directory.

Syntax: rm filename


Recursive remove switch

The rmdir command only works with empty directories. If you wish to delete a directory and its contents, use this:

Syntax: rm -r directory

In this example, I will remove TestDirectory, which contains RenamedFile.txt as well as the copy of CopyFile.txt.



Guide commands

The command line interfaces also includes commands that take a command as an operand and displays its function. These are very useful in the even you forget how the function works or if you want to learn more about the possibilities of said functions.

What is

This function gives you a short line detailing what the command you have input as operand does. 

Syntax: whatis commandname


Manual

This works the same as whatis but gives you a fully-detailed explanation of the command.

Syntax: man commandname



You can scroll using arrow keys, Enter, or your mouse wheel. Press q to exit the manual and return back to the command line.

Closing words

Thank you for taking the time to read my tutorial and I hope it has been of use to you. 
Should you happen to have any criticisms of this blog or if you happen to spot a mistake, be sure to leave a comment.

Comments

Popular posts from this blog

NetBSD

Simple Git tools using a Command Line Interface

How to install a virtual machine [A step-by-step guide]