Linux for Dummies: Ubuntu Terminal

Linux for Dummies: Ubuntu Terminal

I introduced in the previous article, available here, the basic concepts concerning the Linux world. Today we are going to have a look to some basic operations that we can perform using the terminal in an Ubuntu-like operating system.

What is the command line?

Shell, terminal and command line are words that commonly stand for a text interface device. It can be used to perform the majority of the tasks like moving across the file system and manage it, download, install and uninstall programs and also to configure the hardware, create scripts and many other stuffs.

Basically it is an alternative way to the common graphical interface to manage the machine. In fact, we can perform exactly all the operations we carry out through the graphical interface using the command line.

Usually, a classical Ubuntu terminal appear like the one above.

Terminal

Let's have a look to some operations that we can perform via the terminal.

How do I start the terminal? 

Abbiamo due modi per utilizzare il terminale: avviare la macchina in modalità terminale oppure avviare la shell da interfaccia grafica.

There are two ways to use the terminal: starting the computer in textual interface mode or starting the shell from the graphical interface.

In order to start the terminal via the graphical interface you have to:

  • Ubuntu 18.04 and following: select the application menu and type terminal;
  • Previous releases: select the Ubuntu button and type terminal;

Commands manual and syntax

When we have to type commands, it may happen that you don't remember the syntax or the function of some options that you can write next to the command. In that case two commands help us:  man  and  help .

help

The  help  command is the one that reminds us the syntax of the command that we are using. Let's have a look to how we use it.

ls --help  is the guide that explains the syntax of the   ls  command;

ls --help | less : it allows us to visualize the guide of the  ls  command on more pages;

man

The command  man  shows us the manual pages of the command that we want to use, if it exists.

We just have to write:

man <command>  where with <command> we mean the command whose manual page we want to consult.

Move across the file system

We must first of all understand how the file system is structured in the Ubuntu environment. In this environment the file system is represented as a tree, whose root is everyone's parent node.

ROOT

By the command line we can also move far and wide across the file system. The command that we need is  cd. Let's look how it works.

  • cd Desktop  if the current directory is home it takes us to the Desktop folder. Generally we can replace Desktop with the name of any directory as long as it is a sub-directory of the one we are. In alternatively we are forced to use an absolute path to reach the folder;
  • cd ..  ite allows us to move from the current directory to the parent directory;
  • cd /directory  from whatever folder we are it allows us to move to the directory folder;
  • cd ~  or  cd  leads to the home directory of the user;
  • cd -  leads to the previous directory;

Show the current directory

pwd  shows the current directory we are in.

Show the content of a directory

The moment we want to consult the contents of a folder, the ls command comes to our rescue. Let's see some examples of use.

  • ls -l  show the detailed directory's content;
  • ls -la  show the detailed directory's content including the hidden files;
  • ls -S  show the file list sorted by size;
  • ls -X  show the file list sorted by extension;

Copy files and directories

In this case the right command is  cp . Let's see how we use it.

  • cp file1 cart1  copies the file called file1 into the directory called cart1. We can also use the paths, absolute or relative, of both the elements;
  • cp -r cart1 cart2  copies every element inside cart1 into cart2;
  • sudo cp -a cart1 cart2  copies all the elements inside cart1 into cart2 mantaining the same permissions and information on creation date and time;

Move or rename files and directories

The command that we use is  mv .

  • mv old new  renames the file old into new;
  • mv file1 cart1  moves the file file1 into the directory cart1;

Delete files and directories, create directories

The command that we use is  rm .

Let's see how we use it.

  • rm file1 file2 ...  removes the files in the list (file1, file2, ...);
  • rm *.*  deletes everything from the directory. Be careful when you use it! The command can be modified writing  rm *.extension  deleting all the files with extension .extension;
  • rm -rf cart1  deletes all the content of the directory cart1;

In case we want to remove a folder, as long as it is empty, we will use the command  rmdir nameOfTheDirectory .

If we want to create a new directory, we will use the command mkdir new_dir, so creating a new empty directory called new_dir.

Show the content of a file

The command that allows us to see the content of one or more files is the command cat. Let's have a look to some examples. It is good practice to explicitly write the file extension, in order to avoid ambiguity between any files with the same name but different extension (for examples .java files and .class files).

  • cat file  shows the content of the file called file;
  • cat file1 file2 > file3  creates the file file3 with the content of file1 and file2;
  • cat file1 file2 >> file3  adds to file3 the content of file1 and file2;
  • tac file  shows the content of the file but in reverse order;

If we need to cisualize the content of a file on more pages we will not use cat but we will use more. The Enter key advances the view line by line while the space bar advances page by page.

  • if we want to show the file content on more pages we will write  more file where file stands for the name or the path of the file to show;
  • if we want to visualize the content of a directory on more pages we will write  ls -l | more ;

If instead we want to display the contents of files or directories always on multiple video pages but with the possibility of scrolling back and forth we will use the less command. To stop use CTRL + Z.

  • less myFile  shows the content of the file called myFile on more pages;
  • ls -l | less  shows the detailed content of a directory on more pages; 

We have given a very general overview of basic Ubuntu commands. Further on, addressing other aspects of the penguin world, we will introduce new commands. For now, experience people, experience!wink