Difference between revisions of "Warming up with Unix commands"
|Line 145:||Line 145:|
*Stop the process again
*Stop the process again
*kill the process
*kill the process
Revision as of 10:45, 24 November 2020
In this lesson, you will learn some basic commands of the Unix operative system. The operating system (OS) is the system software that manages computer hardware, software resources, and provides common services for computer programs, i.e. allow the communication between hardware and software. In order to use a computer, you need an OS. At this moment you are using a Virtual Machine containing the Ubuntu OS, a Linux distribution, a Unix-like OS. The command used in Linux and Unix they are essentially the same.
If you open a terminal window a prompt will appear where you can type your command. A snapshot of the prompt is shown here below:
You can inspect your environment by typing the following commands:
$> whoami # (my username) $> hostname [-A] # (machine name) $> pwd # (current directory) $> ls [-ltr] # list files and dirs $> ps # running processes $> top # more on running procs $> date # show date and time
As seen above, the command $pwd (present working directory) shows the directory you are presently working on. Now we want to see how to navigate to other directories. This is done by using the command
cd (change directory) followed by a path.
If we want to move for instance to the folder LabQSM we need to provide its path. A path is the address of a file or a folder.
We can use an absolute path:
$> cd /home/max/LabQSM
or a relative path:
$> cd ./LabQSM
provided that you are already in
/home/max. This avoids typing complete paths.
You can then navigate up on level by typing:
$> cd ..
and you will be back to the HOME directory. You can also navigate to multiple directories as:
$> cd ./LabQSM/LAB_1/test_diamond
Finally the command
$> cd or $> cd ~ or $> cd $HOME
takes you to the HOME directory, independently of the present working directory.
Creating, removing, copying and moving files and directories
A way to create a new file is the following:
$> cat > filename
- Enter the content e.g. your name
ctrl + dto return to command prompt.
A file named
filename containing a text with your name has been created.
You can find it in the list using the
ls (list) command,
and you can inspect it by typing:
$> cat filename
We will see later other ways to create and edit files using the editor named
To create a new directory use the command:
$> mkdir dirname
Files and directory are removed using the command
$> rm filename $> rm -r dirname
To move a file from a directory to another, use the command
$> mv filename ./LabQSM
The same syntax applies also for moving directories. The same command is also used to rename (or overwrite) files:
$> mv filename newfilename
File and directory can be also copied (duplicated) in the same directory or in other directories:
$> cp filename ./LabQSM/ #copy the file in the LabQSM directory with the same name $> cp filename ./LabQSM/newname #copy the file in the LabQSM directory with a new name $> cp -r dirname ./LabQSM/ #copy the directory dirname and all its content in the LabQSM directory
In all cases, when the possibility of losing information exists, a confirmation is asked to the user (who has to provide a y/n answer).
This behaviour can be switched off by using the flag
-f (force) on each of the commands above (rm, mv, cp).
Instead the option
-i (interactive), makes the command require the user interaction.
Users, Groups & Permissions
Now let's inspect the content of a directory in details, by typing
$> ls -al
You can see the list of the files, containing information for each file including the owner and the group, the permissions (who can read, write, or execute that file), the dates it was modified, the size etc. Here is an example:
Permissions can be changed using the
$> chmod a+r file # makes file readable by everyone $> chmod u+x file # makes file executable by the user $> chmod go-rw file # makes file not readable nor writable by group and others
In the example above, the community of users is described by means of
u,g,o,a (user, group, others, all). As an example, users belonging to the same group of the file will have permissions specified for
As an example, you can try to create a file in the root directory. You can go to the root directory by typing
$cd ../.. from your home (relative path) or directly by typing
$cd / (absolute path).
$cat > filename will not take effect as you do not have permission to write in the root directory.
Processes and Environment
Linux (Unix) is a multitasking and multi-user systems. So, it allows multiple processes to operate simultaneously without interfering with each other. A process is an executing instance of a program and carry out different tasks within the operating system.
You can monitor the active process by using the command
$>ps #shows the processes for the current shell $>ps -a #shows all processes not associated with a terminal $>ps -x #shows all process owned by you
ps command also shows the unique process id (PID), the terminal type that the user is logged into (TTY),
the amount of CPU in minutes and seconds that the process has been running (TIME), and the name of the command that launched the process (CMD).
in order to see all the option related with a command e.g. ps, you can use the
man command (manual) e.g.
$> man ps #shows all the option for the ps command $> man ls #shows all the option for the ls command
It is possible to kill a process by typing:
$> kill [-9] <pid>
Processes that are launched interactively can be stopped by typing
and can be sent in background or foreground using
- Launch the process
bc -l(This is an arbitrary precision calculator language with interactive execution of statements) and perform an arbitrary operation.
- Stop the process
- Send it in foreground (resume the process)
- Stop the process again
- kill the process
- Verify that the process is not running anymore