terminal

Linux Terminal Basics

Graphical User Interface (GUI) has been useful and easy to use for all types of computer users. We all use it every day to get things done. However, the computing history has the provision to do command-line instructions also to do the same things. Let us take a few examples where one beats the other.

Suppose you wanted to rename 3 files by including their date of creation. You can easily do that in GUI. What if you had to do the same for 100 files?. Would you be still happy to do it via GUI, selecting each file, right-click, rename and type?.

Commandline tools give us the option to do in single-line instructions. Both command-line and GUI have their pros and cons. It is up to us to identify the ideal tool for the scenario and use it.

Terminal Basics for Programmers/ML/AI Enthusiast

The command-line tool in Linux is often called a terminal. When it comes to the professional skill set of any programmer/machine learning/artificial intelligence enthusiast, he/she needs to have at least some familiarity with command-line usage. In this blog post, we will see the minimum required basics that will help someone in this technology space.

Some examples of terminals include Python shell, command prompt in Windows, Console in a browser where you can execute Javascript, Database terminals. So we are already familiar with the terminals in one way or other. A historical look at the terminal usage shows some interesting aspects.

Although these days terminal is a piece of software it originally meant a physical piece of hardware for interfacing with the computer. The computing device and the hardware interface were often different. The computing part was too big to fit in a table. So the immediate interaction of a user was with the physical hardware like a typewriter which is used to instruct the computer.

A similar theme we might see these days could be the fact that most of the AI/ML engines run on the cloud and our laptop can be thought of as an interface to the cloud. Another situation is where you have to leverage a high-performance computing or parallel computing facility, you might be remotely instructing the supercomputing facility from your laptop.

When it comes to the Linux terminal we have a piece of software via which we can give commands to the operating system and get the tasks done. Some key plus points of command-line interface compared to GUI is that are:

  • Programmable
  • Efficient in terms of Bandwidth
  • You can work remotely

The second and third points are obvious when we think about it in terms of using a cloud service or supercomputing facility.

Now let us get started with the actual terminal usage practice

Launching Terminal in Ubuntu

We can launch the terminal in Ubuntu by pressing Control+ALT+T. There is also the GUI way of doing this. But the whole point of learning terminal is to realize its use-cases and power. So we will stick to the keyboard shortcut way.

Just to keep in mind that Unix and Linux programmers over the years have written many different shell programs like Bourne shell, C shell, Korn shell, Bash shell, etc. The one we will be using is the Bash shell.

Upon opening the terminal we will get a window with a prompt. The general form of the prompt will be like ‘user@host$’. This means the user has been logged into the host computer and the computer is at its home directory ready to receive instructions.

Basic Commands and its Usage

Date: The command date gives the present date and time in the system.

sajil@sajil-KY652AA-ACJ-CQ3050IL:~$ date
Wed Nov 25 17:02:04 IST 2015

Mathematical Expression: We can evaluate mathematical expressions in the terminal, just like a calculator.

sajil@sajil-KY652AA-ACJ-CQ3050IL:~$ expr 10 + 21 
31 

Printing Message: We can display custom messages as outputs by using the echo command.

sajil@sajil-KY652AA-ACJ-CQ3050IL:~$ echo Namaste 
Namaste 

Clearing Terminal: At any point, we can clear the terminal screen using the clear command.

sajil@sajil-KY652AA-ACJ-CQ3050IL:~$ clear

Present Working Directory: To know the present working location we can use the pwd command.

sajil@sajil-KY652AA-ACJ- CQ3050IL:~$pwd
/home/sajil

Listing Files and Directories: To list out all files and folders in a directory, the ls command is used.

sajil@sajil-KY652AA-ACJ- CQ3050IL:~$ls
11.pdf
Activity files
Adder.ipynb
Desktop
Dist3D

Here we can see it lists in addition to the PDF and Ipython files the folders also.

Navigating in and out of directories: This could be the most frequent usage you ever going to encounter. To move inside a directory you can type.

sajil@sajil-KY652AA-ACJ- CQ3050IL:~$cd documents

To move one step out type cd<space>.. To step back to the home folder type cd<space>/

sajil@sajil-KY652AA-ACJ- CQ3050IL:~$cd ..
sajil@sajil-KY652AA-ACJ- CQ3050IL:~$cd /

Auto-Completion

Another key feature that is very useful to the terminal is its auto-completion feature. Suppose you want to navigate inside into a set of folders. You don’t have to type the name of all the files and folders. You can simply type the starting 2-3 letters and press Tab. If there is only one matching file or folder starting with that name the terminal completes the typing for you. Otherwise, it lists out all the matches, with that information you can add one more letter to the starting and press Tab to complete.

Also the up and down arrows on the keyboard lets you recall all the past commands used so that if you want to use it again with minor modifications, just select from the past and use it with the auto-completion feature.

Moving Files: You can move files from one location to another using the ‘mv’ command. The usage is simply mv<space>Source<space>Destination.

sajil@sajil-KY652AA-ACJ- CQ3050IL:~$mv *.txt ../

Here I am moving all text files from the present directory to the home directory. The ‘*’ here represents any character. This way the source here represents all files with ‘.txt’ as an extension. The destination here is ‘../’ which means step back to the home location where the terminal opens up by default and move all these files to there.

History: If you want to get a history of all commands used so far, you can use the history command.

sajil@sajil-KY652AA-ACJ- CQ3050IL:~$history

Run as Admin: In situations where you want to run code with admin privileges, you can prepend with the keyword ‘sudo’. The terminal will ask for a password before executing it.

sajil@sajil-KY652AA-ACJ- CQ3050IL:~$sudo snap install vlc

Command Documentation: To check the Manuel/documentation for any Bash command prepend with the keyword ‘man’ with that command.

sajil@sajil-KY652AA-ACJ- CQ3050IL:~$man ls

Printing a Message: The command ‘echo’ can be used to output a message to the terminal. This feature is especially useful when writing bash script files (a file containing a set of bash commands to accomplish a relatively complex task).

user@host~$ echo Have a good day
Have a good day

Listing out the structure of files and folders: We know that we can organize files inside folders or subfolders according to our convenience. The command ‘tree’ lets us visualize the hierarchical structure in a visual form.

sajil@sajil-KY652AA-ACJ-CQ3050IL:~/a$ tree
├ ── 1
└── 2
├ ── 3
├ ── 4
└── 5
└── 6
6 directories, 0 files

Creating and deleting folders: Folders/directories can be created or deleted using ‘mkdir’ and ‘rmdir’ commands. The other most frequent commands are changing directory and exiting from a directory.

Creating Directories
sajil@sajil-KY652AA-ACJ-CQ3050IL:~/a$ mkdir directoryname

Changing Directories
sajil@sajil-KY652AA-ACJ-CQ3050IL:~/a$ cd directoryname

Removing Directories
sajil@sajil-KY652AA-ACJ-CQ3050IL:~/a$ rmdir directoryname

Exiting from current Directory
sajil@sajil-KY652AA-ACJ-CQ3050IL:~/a$ cd ..

Saving Output of a Command to Text File: We can log the output messages given by a command to a text file using a ‘>’ character. The usage is as follows.

ls >outputlog.txt

Here the command ‘ls’ will list out all files and folders in the current directory which is written into a text file named ‘output.txt’.

Some other useful and simple commands are explained below:

Checking System Performance
$ top

Check Web Connection
$ ping google.com

Listing specific type of files
$ ls *.pdf

Compressing files
$ gzip file.txt

Uncompressing files
$ gzip -d file.txt.gz
$ unzip text.zip

Getting Arguements
$ echo a b c d e f |xargs -n 3

Finding files
$ find . -name “*.txt“

Creating Bash Script: We can combine many of these commands and create a bash script. This can be done by writing the commands in a file with the extension ‘sh’ and changing its administrative permission as executable. To run this file we will call its name prepending a ‘./’ characters.

$gedit new.sh
    echo first command
    echo second command
    echo third command

$ chmod a+x new.sh

$./new.sh >outputlog.txt

Here the first commands invoke the inbuilt text editor in Ubuntu to create a text file with ‘sh’ extension. In the next step, a bunch of simple echo print commands is written onto that file. The ‘chmod’ command and its permission setting make the file executable. The last step runs the executable file and logs its terminal outputs to another text file.

Exiting From Terminal: The final command to exit from the terminal can be done by simply typing ‘exit’.

$ exit

Concluding Thoughts

In this post, we have seen the most frequent commands which a developer or programmer might encounter while working with terminal commands. A combination of two or more of such commands might make many tasks easier which otherwise might be harder to do in a GUI. The usage of such commands creatively and practice of it is crucial to be productive in your workspace. I hope these examples will get start you to learn about the first steps, start realizing its power and save you a lot of your time from mundane tasks.

Leave a Comment

Your email address will not be published. Required fields are marked *