Linux For Dummies: Ubuntu Terminal

Basic commands for the file system management



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

 
 
Alessio Mungelli

Alessio Mungelli

Computer Science student at UniTo (University of Turin), Network specializtion, blogger and writer. I am a kind of expert in Java desktop developement with interests in AI and web developement. Unix lover (but not Windows hater). I am interested in Linux scripting. I am very inquisitive and I love learning new stuffs.

 
 
 

Related Posts

    A Java approach: condtional structures

    Hello everyone and welcome back! The previous times we have introduced the concept of variable, trying to define some basic concepts about it.  However, some situations suggest that the concept of…

    Hashmap: hashing, collisions and first functions

    Today we are going to study some concepts closely related to hashmaps. The concepts we are going to see are hashing and collisions. Hashing The idea of hashing with chaining is to…

    Hashmap: Overflow Lists

    In this short series of articles we will go to see how it is possible to create the Hashmap data structure in C. In the implementation we're going to use the…

    Data structures in Java - Linked Lists

    With 2020 we are going to look at a new aspect of programming: data structures. It is often the case that everyone uses structures provided by the various programming languages.…

    A Java approach: variables - use case

    Hello all friends and welcome back! After the introduction made on the variables, we try to analyse some critical issues that may arise in quite common situations. Let's start by analysing…

    A Java approach: variables

    Hello everyone and welcome back! Today we will begin a journey that will lead us to study, and possibly review, what are the basics of programming. We will start by talking…

    How to install a Linux partition on a Windows 10 PC

    In spite of a past we could say almost confronted, the approach between Windows and Linux is accelerating more and more, drawing a story closer to love than to hate.…

    Why is Website Performance Important for Online Businesses?

    Your web is the site where most individuals will connect with your business. Your digital marketing processes will most probably send customers to your website. It might be to get…

    WSL2 is released to run Linux distributions on Windows

    If you are reading about this for the first time, the Windows Subsystem for Linux is a kind of virtual machine that allows you to run the Linux terminal on…

    JavaScript: Promises explained with simple real life examples

    Handling asynchronous data flows is complex, who hasn't faced codes like this one?: checkWeather('palma de mallorca', (error, weather) => { if (error) throw error; if (weather === 'well') { return checkFlights('palma…

    Web shells as a security thread for web applications

    Over the past two decades, web applications as an alternative to traditional desktop application have been on to rise. As their name suggests, they are installed on web servers and accessed…

    Linux For Dummies: Permissions

    In the previous articles I made a short introduction to the Unix world and in the following article I have dealt with the basic commands for the file system management. Today we are…

ALL USERSUSER