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.
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:
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 --help | less : it allows us to visualize the guide of the
ls command on more pages;
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.
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 Desktopif 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 /directoryfrom whatever folder we are it allows us to move to the directory folder;
cdleads 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 -lshow the detailed directory's content;
ls -lashow the detailed directory's content including the hidden files;
ls -Sshow the file list sorted by size;
ls -Xshow 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 cart1copies 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 cart2copies every element inside cart1 into cart2;
sudo cp -a cart1 cart2copies 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 old newrenames the file old into new;
mv file1 cart1moves the file file1 into the directory cart1;
Delete files and directories, create directories
The command that we use is
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 *.extensiondeleting all the files with extension .extension;
rm -rf cart1deletes 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 fileshows the content of the file called file;
cat file1 file2 > file3creates the file file3 with the content of file1 and file2;
cat file1 file2 >> file3adds to file3 the content of file1 and file2;
tac fileshows 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 filewhere 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 myFileshows the content of the file called myFile on more pages;
ls -l | lessshows 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!
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.
How To Use Varnish As A Highly Available Load Balancer On Ubuntu 20.04 With SSL
Load balancing with high availability can be tough to set up. Fortunately, Varnish HTTP Cache server provides a dead simple highly available load balancer that will also work as a…
A Java approach: While loop
Hello everyone and welcome back! After having made a short, but full-bodied, introduction about cycles, today we are finally going to see the first implementations that use what we have called…
A Java approach: The Cycles - Introduction
Hello everyone and welcome back! Until now, we have been talking about variables and selection structures, going to consider some of the fundamental aspects of these two concepts. Theoretically, to…
A Java Approach: Selection Structures - Use Cases
Hello everyone and welcome back! Up to now we have been concerned to make as complete an overview as possible of the fundamental concepts we need to approach the use…
A Java approach: boolean variables
The previous time, we talked extensively about Boolean variables, trying to outline the main operations that can be carried out at a practical level. Of all the cases examined, we have…
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.…