0% found this document useful (0 votes)
5 views

Introduction to Linux Lab Report 2

This lab report by Maria Ines Raheb details the topics covered in the Introduction to Linux course, including file and directory management, wildcards, keyboard shortcuts, command execution, text file operations, and file compression. The report includes step-by-step instructions and commands used during the lab sessions, along with explanations of various Linux commands and their functionalities. Screenshots and specific examples are provided to illustrate the processes discussed.

Uploaded by

mariainesraheb08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Introduction to Linux Lab Report 2

This lab report by Maria Ines Raheb details the topics covered in the Introduction to Linux course, including file and directory management, wildcards, keyboard shortcuts, command execution, text file operations, and file compression. The report includes step-by-step instructions and commands used during the lab sessions, along with explanations of various Linux commands and their functionalities. Screenshots and specific examples are provided to illustrate the processes discussed.

Uploaded by

mariainesraheb08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

INTRODUCTION TO LINUX

LAB 2
REPORT
FULL NAME : MARIA INES RAHEB
GROUP : 01
LECTURER : DR.BERRANI
LAB INSTRUCTOR : DR.BENSALEM
INTRODUCTION TO LINUX

LAB REPORT 02

Hello miss, I hope you are doing well . During


the last lab session , we covered the basics
and main elements of :
●​File / Directory management .
●​Wild cards .
●​Useful keyboard shortcuts .
●​Running Commands Sequentially ,
Redirection , pipping .
●​Basics operations on text files.
●​Searching files , grep command .
●​File compression .

The upcoming pages of the reports contain


the details and the screenshots of specific
steps related to each topic of the listed above .

1
1.​File / Directory management:
First of all I would like to inform you that according to the
instructions mentioned in the lab sheet , this report is made
using LibreOffice .

1)​Let’s use the commands we learned previously to create a


subdirectory lab2 in which files will be created and
processed .

2)​Now , we’ll create a file “file1.txt” using the command touch:


➔​We need to change the directory to “lab2” then create
the file.

2
3)​Then we write three lines of text in “file1.txt” using vi and
save the file :

4)​Let’s create a sub-directory “dir1” and copy “file1.txt” into


it:

5)​Let’s rename the file “file1.txt” of dir1 to “d1file1.txt” .

6)​Let’s create a second sub-directory “dir2” in the directory


“lab2” .

7)​Then we’ll create 2 files and a subdirectory in “dir2” .

8)​Let’s copy the directory “dir2” (including the content) into


the directory “dir1”:
-The required option here is ‘-r’ which is used to copy
directories recursively (copy the entire content of dir2 into
dir1):

3
9)​Finally we’ll remove dir2 :
-The required option here is ‘-r’ which is used to remove a
directory and its contents recursively .

Command ls:
1.​We start by displaying the contents of /usr/local .

2.​Then we display the size of ‘.bashrc’ .

3.​The option ‘-R’ of command &ls is used to :


-Lists directories and their contents recursively .
-Displays the files , directories and subdirectories of the
specified / working directory .

4
4.​By default , file and directories are arranged (displayed) in
alphabetical order . The specific order may be determined
by the underlying file system or the way the file system
organizes directory entries :
5.​The possible options which can be used to modify this
classification criterion :
A.​Sort by modification time :

5
B.​Sort by file size :

6
C.​Sort by reverse order :

7
D.​Sort alphabetically by file extension :

8
6.​Let’s display the contents of the current directory so that
most recently modified files appear first :

9
Directory management :
1.​Yes , it’s possible to make a copy of a directory and all
of its subdirectories in a single command line :

2.​If you want to make a copy of a directory “dir1” to a


directory “dir2” and “dir2” already exists : then “dir1”
will be copied into “dir2” , preserving the structure of
dir1 .
3.​Yes , it is possible to use a command like ‘$ cp file1
file2 file3 directory/” . This command will copy “file1” ,
“file2” and “file3” into the specified directory .

4.​The syntax of ‘$ mv file1 file2’ , “file1” is renamed to


“file2”.If “file2” already exists : the existing “file2” will
be overwritten without any warning , the content of
“file1” will replace the content of “file2” .

5.​Supposing that we have the following directory at


home ‘~/reports/docs/’ and a file “report.tex” at home
.The current directory is ‘~/reports/docs/’ :
-​ To move to “report.tex” in docs :

-​ To return this file to its original location :

10
6.​To rename a directory “dir1” to “dir2” , you can use :
$ mv dir1 dir2
If “dir2” already exists , “dir1” will be moved to “dir2” , unless
“dir2” is a file , in which case mv will return an error .

7.​To both remove a file and change its name :


$ mv source_directory/file1 destination/file2
If a file with the new name already exists in the
destination directory , it will be overwritten .

2.​WILD CARDS :
The wildcard functionality , through an asterisk , allows
to simply say : $ls *.txt
The wildcard can represent a string of any length
consisting of any characters - including the empty string .

1.​The order character that can be used to replace any


character is : the question mark ‘?’ .
2.​Illustration of its usage through 2 examples .
●​ Example 1: Display all the files in the specified
directory which have name composed of the
word “file” and an additional character .

●​ Example 2: Display all the files in the specified


directory which have the extension ‘.txt’ .

11
3.​ USEFUL KEYBOARD
SHORTCUTS :

➔​ Ctrl-A: move to the beginning of a line.


➔​Ctrl-E: move to the end of a line.
➔​Ctrl-P: roll back to the previous command.
➔​Ctrl-N: move forward.
➔​Ctrl-U: erase everything from where you are in a line back to
the beginning.
➔​Ctrl-K: erase everything from where you are to the end of
the line.
➔​Ctrl-L: clear the text from current terminal.

4.​ Running Commands ,


Sequentially , Redirection ,
Pipping:
1)​We can execute two or more commands sequentially (in a
single command line) by separating them with a semicolon
‘;’ :
$ command1 ; command2 ; command3 2.
2)​Illustration through an example in which 3 commands are
run sequentially :

3)​Running the following commands :

12
Commentary and explanation:
1.​ Create two empty files “test0.txt” and “test1.txt”.
2.​ List the files in the current directory and redirect the
output to the file “text2.txt”.
3.​ Display the content of “test2.txt”.
4.​ List the files of the current directory with detailed files
and subdirectories and redirect the output to
“test2.txt”.
5.​ Display the updated content of the file “test2.txt”.

13
4)​We call this operator (>) : the output redirection operator. It
manages the flow of data between commands and file :
5)​Running the commands :

14
15
6)​We notice that in this example we used the operator ‘>>’ to
append the output to the file instead of overwriting it.
●​ When using ‘>’ the file is overwritten with the new content.
●​ When using ‘>>’ the content is appended to the existing file.

7)​The operator ‘<’ is used for : input redirection .

8)​Running the command.

➔​ Explanation: We use the command ‘$ wc -c’ to count


the number of characters in the content of the file
“text2.txt”.
9)​Running the command:

➔​Explanation: In this command, we use piping to search


for manual pages related to “directory”, then filter the
results to display only lines containing “create” using
‘$ grep’.

5.​ Basic operations on text files:


1.​ First, let’s create a text file “numtext.txt”.

2.​ Using the command less, we will print and navigate


within the content of the file.

16
Options for less command:
●​ q: Quit.
●​ Spacebar: Scroll forward.
●​ b: Scroll backward. /pattern: Search for a pattern.
●​ :n: Jump to the next file if multiple files are opened.

17
3.​ Let’s display the 10 first lines of the file.

4.​ Let’s display the 15 last lines of the file.

5.​ Let’s display line number 38.

6.​ Let’s save the 25 first lines of “numtext.txt” to another


file (fist25lines.txt).

7.​ Let’s save the lines from 13 to 37 of “numtext.txt” to


another file (lines13-37.txt).

8.​ Let’s count and display the number of characters in


“lines13 37.txt”.

18
9.​ Let’s count and display the number of words in
“lines13-37.txt”.

10.​ Let’s count and display the number of lines in


“lines13-37.txt”.

11.​ Let’s search for a specific string “bla” in “numtext.txt”.


Display the number of lines where that string appears.

6.​ Searching Files , Grep


Command:
1.​ First, we will describe the grep command and its main
options using only documentation from command.

19
To sum up the output of this command:
Description: The grep command is a powerful utility available on
Unix and Linux systems for searching through text using
patterns.
Main Options:
●​ -c: Counts the number of lines that match the pattern.
●​ -n: Displays the line numbers with the matching lines
●​ -i: Ignores case distinctions in both the pattern and the
input files.
●​ -l: Lists the filenames with matching lines, without
displaying the lines.
●​ -v: Inverts the match, showing non-matching lines.
●​ -w: Select only those lines containing matches that form
whole words.
●​ -r / -R: Recursively searches directories for the pattern.

In these questions, we will work with 3 files named


respectively: “file1.txt”, ”file2.txt”, “file3.txt”.

2.The grep options that allow you to obtain context lines


(which precede and/or follow the line where the searched
word appears) are:
➔​ ‘-B NUM “pattern” file ‘: Print NUM lines of leading
context (before the match).

20
➔​ ‘-A NUM “pattern” file ’: Print NUM lines of trailing
context (after the match).

➔​ ‘-C NUM “pattern” file ‘: Print NUM lines of output


context (before and after the match).

3.To display the number of the line where the searched word
appears:

●​ Display Line Numbers: use -n option: ‘$ grep -n "pattern"


file.

●​ Now, if we also want context lines (lines preceding


and/or following the line with the searched word), We
can also use “context options”.
4.To display the number of occurrences of the searched word:
Use -c option: ‘ $ grep -c "pattern" file’.

5.To make grep ignore the character case (difference between


upper and lower case) in its search: Use -i option: ‘ $ grep -i
"pattern" file’.

21
6.To display not the lines where the word appears, but the file
names: Use -l option: ‘ $ grep -l "pattern" file’.

7. To display lines where the search word does not appear:


Use -v option: ‘ $ grep -v "pattern" file’.

8. To display the names of files that do not contain the search


word: ‘$ grep -L "pattern" file’.

9. To make grep only search lines where the word appears as is,
not its variants: ‘$ grep -w "pattern" file’.

10. To search for several words at once by displaying the line


numbers: ‘ $ grep -n -e "pattern1" -e "pattern2" file’.

11.Let’s apply now using an example, where we work with these


three files:

22
a. Let’s find all lines starting with “a” or “A”.

b. Let’s find all lines ending with “rs”.

c. Let’s find all lines containing at least one digit.

d. Let’s find all lines starting with a capital letter.

e. Let’s search for all lines starting with “B”, “E” or “Q”.

f. Let’s find all lines ending with an exclamation point.

23
g. Let’s find all lines that do not end with a punctuation mark
(period, comma, semicolon, colon, question mark, exclamation
point).

h. Let’s search for all words containing an “r” preceded by any


upper or lower case letter.

i. Let’s find all words with an “r” as the second letter.

7.​ File compression :


1.First, let’s create 3 text files and add text to these files.

24
2.Let’s copy a text file whose size is greater than 10 k bytes from
your system.
-First, we look for a text file whose size >10k, then copy its
path.

3.Let’s download 2 images.

4.Then we store all of these files into one directory named


“myDir” under your home.

25
5.Let’s compute the total size of files in this directory.

6.Now, we put all the content of the directory in a single


uncompressed file (“file1.tar”) and copy it to a sub-directory
(“myDir2”).

7.Let’s compute the size of the “file1.tar”.

●​ We notice that the size reported is likely smaller than


the sum of individual file sizes because of
compression achieved by tar.
8.Let’s compress “file1.tar” and compute its size.

26
●​ After compressing the file using gzip, we will typically
notice a reduction in file size.
9.Finally, let’s decompress the resulting file, then check the
produced files and their size.

●​ We notice that after decompression, the size is now similar


to the size reported in step 8. The compression and
decompression process is generally lossless, preserving
the original data.

That’s all for these last two lab sessions


Please miss , let me know if there are any errors."

27
28

You might also like