Linux Tutorial ASP2024
Linux Tutorial ASP2024
Table of contents
1. Introduction
2. File system
3. Information commands
4. Directory manipulation commands
7. Redirections
8. Filters
9. User management
Page | 1
Introduction
Operating system:
An operating system (OS) is system software that manages computer
hardware and software resources, and provides common services for
Computer programs.
Page | 2
Examples:
Windows, Unix, Linux, Apple iOS, Mac OS, Xenomai, Android.
Page | 3
Easy to install applications.
Secure
Stability
Community
Free
Distributions:
Ubuntu, Fedora, Debian, Arch Linux, CentOS, Kali Linux, Mint,
OpenSUSE, Red Hat, Slackware, AImaLinux,…etc.
Page | 4
File system
Linux structure
Kernel:
Kernel is the core part of Linux. It is responsible for all major activities
of this operating system. It consists of various modules and it interacts
directly with the underlying hardware. . It allocates CPU time and
memory to each program and determines when each program will run.
The kernel also provides an interface to programs whereby they may
access files, the network, and devices.
Page | 5
Shell:
It is an interface among the kernel and user. The shell is a command
line interpreter (CLI). It interprets the commands the user types in and
executes them. The commands are themselves programs.
Hardware layer:
The hardware layer consists of several peripheral devices
like CPU, HDD, and RAM.
Types of shells:
File system
Linux Files:
Normal files: data files, executables files and text files
Directory files: are simply containers for files and other directories.
Special files: represent interfaces with the devices managed by
the system
In the Linux operating system files are stored in a tree-like structure
starting with the root directory as shown in the below diagram. The
Linux file system hierarchy base begins at the root and everything
starts with the root directory.
Page | 6
Linux file system structure
/ - Root directory that forms the base of the file system. All files
and directories are logically contained inside the root directory
regardless of their physical locations.
/bin - Contains the executable programs that are part of the Linux
operating system. Many Linux commands, such as cat, cp, ls,
more, and tar, are locate in /bin
/boot - Contains the Linux kernel and other files needed by LILO
and GRUB boot managers.
/dev - Contains all device files. Linux treats each device as a
special file.
/etc - Contains most system configuration files.
/home - Home directory is the parent to the home directories for
users.
Page | 7
/lib - Contains library files, including loadable driver modules
needed to boot the system.
/media - Directory for mounting files systems on removable media
like DVD-ROM drives, flash drives, …..
/mnt - A directory for temporarily mounted file systems.
/opt - Optional software packages copy/install files here.
/proc - A special directory in a virtual memory file system. It
contains the information about various aspects of a Linux system.
/root - Home directory of the root user.
/run - Gives applications a standard place to store transient files
they require like sockets and process IDs.
/sbin - Contains administrative binary files. (mount, shutdown,
umount, ….).
/srv - Contains data for services (HTTP, FTP, etc.) offered by the
system.
/sys - A special directory that contains information about the
devices, as viewed by the Linux kernel.
/tmp - Temporary directory which can be used as a scratch
directory (storage for temporary files). The contents of this
directory are cleared each time the system boots.
/usr - Contains subdirectories for many programs such as the X
or GUI Window System.
/usr/bin - Contains executable files for many Linux commands. It
is not part of the core Linux operating system.
/usr/include - Contains header files for C programming
languages
/usr/lib - Contains libraries for C programming languages.
Page | 8
/usr/sbin - Contains administrative commands.
/var - Contains various system files such as log, mail directories,
print spool, etc. which tend to change in numbers and size over
time.
Directories can be accessed by their name. Linux uses also the symbols
to represent directories.
Symbols :
. This directory.
~ Home directory.
.. The parent directory.
/ The root directory.
Example:
/user/lib
/etc/network/interfaces
Page | 9
User session
login: usernamre<rc>
Password: xxxxxxxxxxxxxxxxx<rc>
If the login or password is not correct the system give you the response:
Login incorrect
Login:username <rc>
Password:xxxxxxxxxxxxxxxx <rc>
username@sysname:~$
If the login is correct the prompt of the user appears in the terminal in a
common format of username@sysname:~$. In this example, the
prompt is displaying the username, the sysname, and if that user is
using the system as a normal user ($) or a super user (#). The user can
now type a program. Once programs terminate, control is returned to
the shell and the user receives another prompt ($), indicating that
another command may be entered.
The super user on a Linux system is called root. Anything that can
be done on system can be done by root.
Example:
$shutdown –h now
Page | 10
Login shell startup files:
When you run a login shell it reads and executes a number of
commands from the files on start-up, in the following order:
/etc/profile
~/.bash_profile
~/.bash_login
~/.profile
When an interactive shell that is not a login shell is started. The shell
in this case reads and executes commands from:
/etc/bash.bashrc
~/.bashrc
Page | 11
Information commands
They are commands used by the user to obtain information about the
system.
Examples:
$ date
Tue Jan 25 14:20:34 EST 2022
$ date +%F
2022-01-25
$ date -u
Tue Jan 25 14:20:34 UTC 2022
$who [option] [filename]
The who command is a simple and effective way to display information
about currently logged-in users.
Page | 12
Example:
$ cal 4 2010
April 2010
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
$
$man [option][command]
The man command is used to display the manual pages for other
commands and utilities. It provides detailed documentation about the
usage, options, and functionality of commands. To quit the man page
you must press q.
$echo [argument]
echo displays argument to the screen
Page | 13
Example:
$echo Hello World
Hello World
$
$echo "Hello World"
Hello World
$
$echo Hello ; echo World
Hello
World
$
$clear
The clear command clears the screen.
Page | 14
Directory manipulation commands
$mkdir directory
The mkdir command creates a new directory in the working
directory.
$rmdir directory
The command rmdir command removes a directory in the
working directory if it is empty.
$cd directory
The cd command changes the current directory to directory. If you
execute this command without specifying a directory, it changes the
current directory to your home directory.
$pwd
This command displays the path of present working directory.
Page | 15
The shell has special characters (wildcards) to define search criteria
for file names:
To treat *,?,[ ] as literals in the text and not as wildcards, you can
escape the wildcard by adding ‘\’ before the wildcard.
Examples:
$ls *.cpp
FFT_prog.cpp Spline2.cpp Prog1.cpp
$
$ls *[0-9]
Calendar2024 classe1 classe2 Texte30
$
$ls ??z
Abz
SOz
77z
$
$tree
The tree command-line program is used to recursively list or display
the content of a directory in a tree-like format.
Page | 16
File manipulation commands
$touch file
This command creates a new empty file inside the working directory or
update modification time of the file if it exists.
$rm file
rm removes a file.
$rm –rf directory
rm –rf recursively removes the directory and all files and sub
directories in the directory structure.
Page | 17
$ln oldname newname
The ln command is used to create hard or symbolic links to files or
directories. A hard link creates a new name for a file or directory (the
same i-node). A symbolic link (option –s) creates a new file that contains
the path to the original file or directory.
Example:
$ls *.cpp
FFT_prog.cpp Spline2.cpp Prog1.cpp
$ln Spline2.cpp Splinesource.cpp
$ls –i Spline*.cpp
4540031 Spline2.cpp 3540031 Splinesource.cpp
$
Page | 18
[command]:
-print print the pathname of each file found
-exec command {}\; executes a command on each file found.
-ok <command> {}\; execute command on each file with
conversational mode
Examples:
$locate pattern
The locate command lists files that match pattern. The locate
command is much faster than find command.
Page | 19
Working with file contents
$file pathfile
The command file determine the type file of pathfile .
$head file
The head command writes the first ten lines of a file to the screen.
$head –n N file
The head command can also display the first N lines of a file
$tail file
Similar to head, the tail command writes the last ten lines of a file
to the screen
$less file
The command less writes the contents of a file onto the screen a
page at a time.
$more file
Similar to less, the more command is useful for displaying the
contents of the file page by page. To see the next page the user must
use the space bar, or q to quit.
Page | 20
Redirections
Standard input/output
When a user logs into the system three streams are opened and one
number called descriptor is assigned to each of those streams:
Redirections
In Linux we can redirect the input and the output of commands.
> redirect the standard output to a file, overwriting any existing
contents of the file. If no file exists, it creates one.
>> redirect the standard output to a file and appends to any existing
contents. If no file exists, it creates one.
< redirect the standard input from a file to the command preceding
the less-than sign.
<< redirect the standard input to here-is-document is a way to append
input until a certain sequence (usually EOF) is encountered.
Page | 21
Examples:
$ls
Student
Professeur
Spline.cpp
$ls > F
$ls
Student
Professeur
Spline.cpp
F
$cat F
Student
Professeur
Spline.cpp
$date >> F
$cat F
Student
Professeur
Spline.cpp
Tue Jan 25 14:20:34 EST 2022
$
$wc –l< F > G wc –l count lines number of F and prints it in file G
$cat G
4
$
$wc << end
a b c d
e f g h
end
2 8 16
$
Page | 22
Pipes
Piping is when you take the output of one command and use it as an
input to another command. The pipe (|) metacharacter is placed
between two commands to achieve this.
$command1 | command2
Pipes are unidirectional and usually used to avoid using temporary files.
Example:
$who|wc –l count the number of users connected to the system
Page | 23
Filters
$sort file
The sort filter sorts the file content in an alphabetical order.
Example:
$cat Fruits
Banana
Apple
Orange
Kiwi
Lemon
Cheery
Avocados
Pear
Peach
$sort Fruits
Apple
Avocados
Banana
Cheery
Kiwi
Lemon
Orange
Peach
Pear
$
$grep ‘string’ textfile
The most common use of grep is to filter lines of text containing (or not
containing) a certain string.
Example:
$cat Fruits
Banana
Page | 24
Apple
Orange
Kiwi
Lemon
Cheery
Avocados
Pear
Peach
$
$grep Pea Fruits
Pear
Peach
$
$wc textfile
wc counts words, lines and characters in the text.
$cut file
The cut filter can select columns from files, depending on a delimiter
or a count of bytes.
Example:
$ls –l |cut –d “ ” –f 1 displays the column of permissions
Page | 25
Example:
$cat Fruits | tr ‘o’ ‘O’
Banana
Apple
Orange
Kiwi
LemOn
Cheery
AvOcadOs
Pear
Peach
$
$uniq file
uniq removes duplicates lines from a file.
Page | 26
User management
In Linux each user is registered in two system files: /etc/passwd and
/etc/group.
Example:
sara:x:3450:3450: Cadi Ayyad:sara:/bin/bash
$useradd username
The useradd command creates a new user account.
$userdel username
The userdel command deletes a user account.
$usermod username
The usermod command modifies user account attributes such as
username.
Page | 27
$whoami
The whoami command is used to display the username of the current
user.
$id
The id command will give you your user id, primary group id, and a list
of the groups that you belong to.
$su username
The su command lets you switch to another user's account or execute
commands as a different user.
$sudo command
sudo is a command in Linux that allows users to run commands with
privileges that only root user have.
The /etc/group file contains basic group attributes. This is an ASCII file
that contains records for system groups. Each record appears on a
single line and is the following format:
Name:Password:ID:User1,User2,...,Usern
Example:
student:x:3450:sara
$groupadd groupname
The groupadd command creates a new group
Page | 28
$groupdel groupname
The groupdel command removes a group
$groups
The groups command to see a list of groups where the user belongs
to.
Example:
sara:$1$NAnoMEmP$GgRfy2.YxwJ6Mnb/cDyM3.O/:14564:0:90:7:::
Username: sara
Encrypted password: $1$NAnoMEmP$GgRfy2.YxwJ6Mnb/cDyM3.O/
Last password change: 14564 days (since January 1, 1970).
Minimum password age: 0 days
Maximum password age: 90 days
Password warning period: 7 days
Account Expiration Date: (empty, indicating no expiration)
$passwd username
The passwd command sets and changes passwords for users. For
changing password users will have to provide their old password before
twice entering the new one.
Page | 29
Example:
$passwd
Changing password for sara
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
$
Page | 30
File permissions
Page | 31
Example:
$ls –l
-rwxrwxr-x 1 sara sara 5224 Dec 30 03:22 hello
-rw-rw-r-- 1 sara sara 221 Dec 30 03:59 hello.c
drwxrwxr-x 5 sara sara 1024 Dec 31 14:52 data
$
The first column contains ten characters for each file. The first character
indicates the file type followed by three groups of three characters.
<type><user><group><others>
- rwx rwx r-x
All access restrictions apply to users, only one user is exempt from
access controls: the super user with login: root and UID=0
Page | 32
Changing file permissions
To change the file permissions we use the chmod command. Only the
owner of a file can change the permissions for user (u), group(g), or
others (o), by adding (+)or subtracting(-) the read, write, and execute
permissions.
The first way is the symbolic method, which lets you specify permissions
with combination of abbreviations.
User class Operator Access type
u(user) +(add access) r
g(group) -(remove access) w
o(others) =(set exact access) x
a(all)
Examples:
$chmod u+x F Add the read permission to the user
$chmod u+x F Add the write permission to the group
$chmod o=x F the permissions of others are only set to x
$chmod u+x F Add the x permission to all
Page | 33
The other way to use the chmod command is the absolute form, in
which you specify a set of three numbers that together determine all
the access classes and types. Add the numbers of the permissions
you want to give for each type of users.
Example:
$chmod 664 F 664 = -rw-rw-r--
Example:
- ……
Process Creation
A new process can be created by the fork mechanism. The new process
consists of a copy of the address space of the original process. Fork
mechanism creates new process from existing process. Existing
process is called the parent process and the process created newly is
called child process. Each process has its own environment, which is
copied from the parent process's environment.
Page | 35
Linux process creation
Page | 36
Foreground process: Every process when started runs in foreground
by default, receives input from the keyboard, and sends output to the
screen.
Zombie state is when a process is dead but the entry for the process is
still present in the process table.
Page | 37
Process management commands
$ps [option]
The ps command can be used to list all the running processes .
$ps
PID TTY TIME CMD
16524 pts/0 00:00:00 bash
17319 pts/0 00:00:00 ps
$
$top
The top command is used to show all the running processes within
the working environment of Linux.
$jobs [option]
The jobs command lists all currently running background jobs.
$sleep <nombre>[suffix]
The sleep command is used to delay the execution of scripts or
commands for a specified amount of time. The default delay time si in
seconds and you can set it in minutes (m), hours (h), and days (d).
$sleep 5;date
Sat Apr 17 13:08:27 CEST 2024
$
Page | 38
$nice –n <–nice value> <command>
The nice command starts a new process and assigns it a priority nice
value at the same time. Nice value ranges from -20 to 19, where -20
is of the highest priority. 0 is the default value.
Once a job is started or executed using the nohup command, stdin will
not be available to the user and nohup.out file is used as the default
file for stdout and stderr. If the output of the nohup command is
redirected to some other file, nohup.out file is not generated.
Example:
$time date
Sat Apr 17 13:08:27 CEST 2010
real 0m0.014s
user 0m0.008s
sys 0m0.006s
$
Page | 39
Examples:
$at 16:00 command
$at 17:15 Fri command
$at noon command
$at noon
>command1
>command2
>command3
…
Ctrl+D
$
$df [option]
The df command shows the amount of disk space available being
used by the file systems.
$free [option]
The free command shows the free space and used space of the
memory RAM in the system.
Page | 40
Environment variables
In Linux environment variables are a set of dynamic named values,
stored within the system that are used by applications launched in shell.
These variables have a name and their respected value. They allow the
user to customize how the system works and the behavior of the
applications on.
$echo $VARIABLE_NAME
echo displays the specified environment variable.
Example:
$echo $HOME
/home/sara
$
$printenv [VARIABLE_NAME]
printenv displays all or the specified environment variables.
Page | 41
Example:
$printenv HOME
/home/sara
$echo $HOME
/home/sara
$printenv
TERM=xterm-256color
SHELL=/bin/bash
USER=sara
PATH=/usr/local/bin:/usr/bin:/bin
MAIL=/var/mail/sara
PWD=/home/sara
LANG=en_US.UTF-8
HOME=/home/sar
…
$
$export Variable=[value]
Example:
$ TUTORIAL=Linux
$echo $TUTORIAL
Linux
$bash
$echo $TUTORIAL
$exit
exit
Page | 42
$export TUTORIAL
$bash
$echo $TUTORIAL
Linux
$exit
exit
$
In the above example TUTORIAL was defined in the current
environment. When you start a child process it inherits all the
environment variables that were exported in your current environment.
Since TUTORIAL was not exported it was not set in the spawned bash
shell. When you have exported TUTORIAL you saw that it was indeed
available in the child process.
$export –p
This command lists all names that are exported in the current shell.
$set [option] [arguments]
The set command displays local and environment variables.
$unset VARIABLE_NAME
Example:
$echo $TUTORIAL
Linux
$unset TUTORIAL
$echo $TUTORIAL
$
$history
histoy displays a list of commands in the shell history.
$alias [name=[value]]
Page | 43
alias lists or creates aliases. If no arguments are provided the
current list of aliases is displayed.
Example:
$ls -l
total 4
-rw-r--r-- 1 sara sara 221 Nov 13 11:30 file.txt
….
Page | 44
Basic Linux tools
$compress [option] Textfilename
This command compress the Textfilename and place it in a file
called Textfilename.Z
Page | 45
$tar -cxf Archivename.tar
The tar program extracts an archive in the current directory.
Example:
$gcc –o hello hello.c
$./hello
Hello World
$
$nano hello.c
nano is a command line text editor like vi, vim, et emacs
Page | 46