Linux-Lab2-en

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Lebanese University Dept.

: Electricity - Electronics
Faculty of Engineering 3rd Year – 5th Semester
Branch I Lab : Linux
Num. 2: More on files

I- Hidden files :
It should be noted first that in a Linux partition the system manipulates everything as files, even
directories are considered "Directory type" files. When we talk about hidden files, this covers directories
as well.
A file is considered hidden by the system when the first character of its name is simply a dot ".", For
example ".my_file".
In general, we hide files make them invisible during normal operations, this is done more particularly
for critical files in order to avoid human deletion errors, for example: In this case, the "ls" command
does not display hidden files without adding the –a option.

Lab  Try to test this option with the "ls" command.

II- Vi Editor :
"vi" is a text editor, its name comes from the word "Visual Instrument ”,“ vi ”works in two modes:
command mode where you can control the text file, and insert mode where you can edit the content of
the text file. The esc (Esc) key is used to switch from insert mode to command mode.

a- Essential commands (command mode ):


 to Run the editor : "vi name_of_file_to_edit"
 to Save the file : ":w name_of_file "
 to Quit the editor with saving the file: ":x"
 to Quit without saving : ":q!"

b- Basic commands for inputting text : (These commands lead directly to edit mode)
 "a" to Append to the right of the cursor
 "i" to Insert text to the left of the cursor
 "o" to insert a new empty line below the cursor
 "O" to insert a new empty line above the cursor
 Enter insert an end of line

c- Moving the cursor through the text (command mode) :


We can use the arrows to move the cursor left or right of a character, or upward or downward of
a line. Also we can use the keys : [h (left), j (down), k (up), l (right)]
Commands by line :
 "0" put the cursor at the beginning of the line
 "$" put the cursor at the end of the line
 "^g" (press simultaneously on the keys CTRL and g) display the number of the current
line
 Enter put the cursor at the beginning of the next line
 To go to a particular line : "#G" put the cursor at the line number #
 Commands of screen :
 "^f" (press simultaneously on the keys CTRL and f) advance one screen
 "^b" (press simultaneously on the keys CTRL and b) move back one screen
 "1G" put the cursor at the beginning of the file
 "G" put the cursor at the last line of the file

d- Deletion, replacement, copying (command mode) :


 "x" cut the selected character by the cursor and save it in the tampon
 "#x" cut # characters and save them in the tampon, where # is a number
Page 1/3
 "dd" cut the current line and save it in the tampon
 "#dd" cuts # lines from the current line and saves them in the tampon
 "yy" copies the current line to the tampon
 "#yy" copies # consecutive lines to the tampon
 "p" paste the content of the tampon to the right of the cursor or to the next line (if one or
more characters exist in the tampon)
 "P" paste the content of the tampon to the previous line.
e- More complex commands:
- Recherche d'une chaîne de caractères particulière :
"/abc" cherche la chaine de caractère "abc" en avançant vers la fin du fichier, "n" est utilisée
pour trouver la prochaine occurrence de la dernière chaîne recherchée.

- Substitution :
:s/ceci/cela/options substitue la première occurrence de ceci par cela dans la ligne courante,
l'option g substitue toutes les occurrences dans la ligne courante ;
l'option c demande de confirmer la substitution.
:3,9s/ceci/cela remplace dans les lignes 3 à 9 la première occurrence de ceci par cela.
:%s/ceci/cela remplace dans tout le fichier la première occurrence de ceci par cela
:%s/ceci/cela/g remplace dans tout le fichier toutes les occurrences de ceci par cela

- ~ change la casse (majiscules/minuscules) d'une lettre


- J joint la ligne courante à la suivante
- . répète la dernière commande
- #commande exécute une commande # fois
- u annule la dernière commande
- U annule les commandes affectant la ligne courante.
Lab  :

a- Create, edit and save the following text file as it is (name it "notes") with vi :
M1 M2 M3 M4 M5
Salim Salem 60 87 91 75 67
Nader Fawzi 73 45 88 90 78
Nabil Harb 81 72 68 96 55
Salwa Halabi 70 67 85 85 61

b- Copy the file being created to another hidden file


c- Compare the two files with the command "diff", consult "man"
d- Edit again the hidden file with "vi" and delete the last line (last student)
e- Quit "vi" with saving the modifications.
f- Compare the two files again
g- Find the size of the file notes" in bytes (use the command ls –l)
h- Compress the file notes with the command "gzip" (by default "gzip notes" compresses the original
file)
i- Find the size of the file "notes.gz" in bytes (this is the name after compression)
j- Decompress the file with the command "gunzip"
k- Discover how we can compress the file "notes" into another file without altering the original one.
l- Do statistics on the number of lines, words and characters of the file notes using the command
"wc".

Page 2/3
III- Search a text file:
The command "grep" allows looking for a string in a text file.
e.g. grep Nabil notes
symbolic expressions can be used to compose criteria for the search:
[….] range of allowed characters (use - to consider a range or , to consider a list)
[^...] range of forbidden characters
e.g. : grep 9[0-9] notes gives the lines where the grades in the file notes >= 90.

IV- Search for a file:


The command "find" allows to find the files based on some criteria. The syntax is as follows :
find <Ssearch directory> <criteria>
e.g. : find / -name passwd
This command returns starting from the root of the partition all the files with name "passwd" expressed
with their full pathes.
Other options may be used, search by user, group or the date of file, for more information use "man".

V- The command "cut":


The command "cut" allows to cut and display specific zones of files.
e.g. : cut -c1-21 notes (- composes a range). Here the command returns the range of columns 1 till 21.
Or also : cut –c1,11-21 notes (, specifies discontinued ranges). This returns the column 1 and the range
of columns 11-21.
Also we can specify a field separator with the option -d.
e.g.: cut -d: -f1 /etc/passwd where : is the separator, f1 mentions the first field and of course
/etc/passwd is the file subject of the command.

VI- Using pipes " | ":


In the Lab #1, we have seen how the output of a command can be redirected using « > ». Also many
commands require one or more arguments as input files like "grep", "cut", "cat", "more",… Pipes
allow to link directly the output of a command to the input of another one, e.g. :
grep Nader notes –i | cut –c1,11-12,22-24
Here the output of the command "grep" which is exactly the line of the student "Nader", composes the
input to the command "cut" that will return the grade of Nader in M1.
Pipes can be numerous in a line to link more than two Linux commands.

Lab  :
Compose and execute the commands capable to:
a- Extract only the first names and last names of students having their grades less than 60
b- Repeat (a) with the output displayed on the screen sorted alphabetically (use the command "sort")
c- Repeat (b) with saving the result in a text file named « failures »

Page 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy