M01: Introduction to Operating Systems
TU1: Installing, configuring and exploting a computer system
ASIX1/DAW1/DAM1
Practical Exercise 2: Basic command-line utilities. The Linux directory tree
4-10-21

Practical Exercise 2: Basic command-line utilities. The Linux directory tree


GENERAL CONDITIONS

1-
Deadline: 24-10-2021.

2-
Send your report attached to an e-mail with the following specifications:
     a) E-mail address:
cf(at)collados.org or jordi.binefa(at)fje.edu depending on who is your teacher
     b) File Name:
        b.1)
ASIX1: asix1_surname_name_m01tu01pr02.pdf and asix1_surname_name_m01tu01pr02.odt
        b.2) DAW1: daw1_surname_name_m01tu01pr02.pdf
and daw1_surname_name_m01tu01pr02.odt
    
c) Subject:
       c.1)
ASIX1: asix1_surname_name_m01tu01pr02
       c.2) DAW1: daw1_surname_name_m01tu01pr02

3-
Make this report individually.

4-
Left, right, top and bottom margins: 2cm.


5-
Character format: a) Font: Arial, b) Size: 10, c) Questions typeface: Bold, d) Answers typeface: Regular




DOCUMENTATION

1- The Linux directory tree structure. Important files and their paths

Read the following link:

https://www.howtogeek.com/117435/htg-explains-the-linux-directory-structure-explained/

At the moment, you should pay special attention to the following directories (or folders):

a) /
d) /home
g) /usr/bin
b) /bin
e) /root
h) /usr/sbin
c) /etc
f) /sbin


But in the future we will pay special attention to /boot, /dev, proc, mnt and /var directories as well.

2- ls
a) The ls command-line utility lists directory (folder) contents.
b) Examples:
    1) ls  --> Lists the contents of the present directory.
    2) ls  -ls --> Lists the contents of the present directory and  extra information about each file or directory (owner, size, permissions,.....)
 
   3) If you want to list the contents of any folder, you have to write its path. For instance:
        ls  -ls  / --> Lists the contents (and extra informatiosn) of  the / directory.
        ls -ls  /home --> Lists the contents (and extra information) of  the /home directory.
        ls  -ls  /var/lib --> Lists the contents (and extra informatiosn) of  the /var/lib directory.
     4)
If you want to list information about an specific folder but not about its contents  you have to add the option -d. For instance:
         ls  -ls -d /home or also ls  -lsd  /home
     5) If you want to show hidden files or directories inside  a directory, you have to add the option -a. For instance:
         ls -a /etc/skel  --> Lists hidden files inside /etc/skel
     

3- tree
a) tree is a command-line utility that shows recursively the contents of a directory (including files and subdirectories) in a tree-like format. Additionaly, tree can show information about permissions and ownership.
b) Example: tree /usr/share shows recursively the contents of /usr/share in a tree-like format
c) How to install tree on your system:
    * As a root user run: aptitude update
    * As a root user run: aptitude install tree

4- cat

a) The cat command-line  displays the contents of a text file. For instance: cat  /etc/resolv.conf displays the contents of resolv.conf.
b) If  a text is longer than one screen,  you can use the special character | and the command more  that displays as much as can fit on the current screen and waits for user input (enter for a new line or space bar for a new screen)  to advance. For instance: cat /var/log/messages  |  more.

5- cd
a) Description: The cd command-line utility changes the current working directory to a new working directory.
b) Synopsis:  cd  directory_name where directory_name is the name of the new working directory.
c) Examples: cd  /home, cd /var/log, .......
d) Special cases and special characters:
    cd .  --> Changes to the current working directory. The "." character means "the current working directory".
    cd ..
-->
Changes to the parent directory of the current working directory. The ".." character means "the parent directory of the current working directory".
   
cd ~  --> Changes to the user's personal directory which is "/home/username". The "~" indicates the user's personal directory.
    cd -  --> Changes to the previous directory. The "-" indicates the previous directory

6- Special characters * (asterisk)
a) The asterisk * character is a special character that causes the shell to generate filenames. It matches any number of characters in a filename. For instance, if you want a list of files in a folder that begin with the string "prov" then you can run the command: ls  -ls  prov*.
b) Examples:
dacomo@inf1-dacomo:~>ls     (with no *)
amemo   mem   memalx   memo   memo.0612   memoa   memoalx.0620  
memoalx.keep   memorandum   memosally   sallymemo   typescript   user.memo

It shows all files in the working directory


dacomo@inf1-dacomo:~>ls  memo*      
memo      memo.0612    memoa    memoalx.0620    memoalx.keep    memorandum    memosally

It shows all files in the working directory
whose name begin with the string memo

dacomo@inf1-dacomo:~>ls  *mo           
amemo   memo   sallymemo   user.memo

It shows all files in the working directory
whose name end with the string mo                 

dacomo@inf1-dacomo:~>ls  *alx*     
memalx   memoalx.0620   memoalx.keep

It shows all files in the working directory
whose name contain the string alx

dacomo@inf1-dacomo:~>ls  *.*     
memo.0612   memoalx.0620   memoalx.keep   user.memo

It shows all files in the working directory whose name contains one or more extensions  

dacomo@inf1-dacomo:~>ls  !(*.*)     
amemo   mem   memalx   memo   memoa   memorandum   memosally   sallymemo   typescript

It shows all files in the working directory whose name does not contain any extension.  The special character ! means: the opposite of, reverse, negate, logical operator NOT.


7- cp
a) Description: The cp command-line utility copies one or more files and directories to a destination directory.
b) Synopsis:  cp source_file  destination_file where source_file is the name of the file that cp will copy, and  destination_file is the name that cp assigns to the new copy of the file. By default,  the  destination_file will be equal to the source_file.
c) Examples:
    cp  /var/log/messages
      /media/usb/  --> A file called messages in the directory /var/log will be copied in the directory /media/usb. The name of the new file will be messages.       
    cp  /var/log/messages   /media/usb/messages.20131110  --> /var/log/messages  will be copied in the directory /media/usb. The name of the new file will messages.20131110.
    cp  /var/log/*  /media/usb  --> All the files in the directory /var/log will be copied in the folder /media/usb
d) Note: you can use .,..,- and ~.

8- mv

a) Description: The mv command-line utility moves one o more source files and directories to a destination directory. The original files will be removed. Refers to cp for Synopsis and examples.
b) Note 1 : you can use the following special characters:  .  ..  -   ~
c) Note 2: You can use  mv to rename a file -->  mv  old_name  new_name
d) Note 3: You can use mv to move a directory --> mv  existing_directory  new_directory

9- mkdir
a) Description: The mkdir command-line utility creates a new directory..
b) Synopsis: mkdir  directory_name where directory_name is the name of the new created directory.
c) Examples:
     mkdir  test00  -> Creates a new directory called test00 as a child of the current working directory.
    mkdir  /home/etpclot/test01 -> Creates a new directory called test00 as a child of the /home/etpclot directory.
d) Note: you can use .,..,- and ~.

10- rm

a) Description: The rm command-line utility remove files or directories. By default it does not remove directories.
b) Synopsis 1: rm  file_name where file_name is the name of the file that will be removed. Example: rm /home/usuari/INDEX.gz.
c) Synopsis 2: rm  -r directory_name where directory_name is the name of the directory that will be removed. Example: rm  -r  /home/etpclot/test00.
d) Note 1 : you can use .  ..  -   ~
e) Note 2:  rm  !(filename) removes all files with the only exception of  filename. For instance, if you run rm !(ex01.c), every file in the directory is removed
with the only exception of ex01.c. The special character ! means: the opposite of, reverse, negate, logical operator NOT.


11- gzip
a)Description: The gzip command-line utility compresses or expand files. A file compressed with gzip is marked by a .gz filename extension.
b) Synopsis 1: gzip  file_name where file_name is the name of the file that will be compressed. Example: gzip /home/etpclot/INDEX. After runing the command, the original file will be replaced with a new file called INDEX.gz.
c) Synopsis 2: gzip -d  file_name.gz where file_name is the name of the file that will be expanded. Example: gzip -d  /home/etpclot/INDEX.gz.After runing the command, the original file will be replaced with a new file called INDEX.
d)
Synopsis 3: gzip -l  file_name.gz where file_name is the name of a compressed file. Option -l lists the following fields:  size of the compressed file, size of the uncompressed file, compress ratio and uncompressed name.

12- tar

a) Description:
The tar command-line utility packs and creates a new single file, which will be marked by a .tar filename extension, from multiple files or directories hierarchies. The tar comand-line utility can extract (unpack) files from a .tar file as well..
b) Synopsis 1: tar cf  filename.tar  list_of_files where filename.tar is the name of the .tar file that will be created and list_of_files is a list of files (* can be used).
c) Synopsis 2: tar xf  filename.tar where filename.tar is the name of the packed file that will be unpacked.
d) Synopsis 3: tar cf  filename.tar  directory_name where filename.tar is the name of the .tar file that will be created and where directory_name is a directory that will be packed.
e) Synopsis 4: tar tf filename.tar shows the contents of filename.tar.
f
) Examples:
    tar  cf  test.tar  test00.c  test01.c  test02.c  asm.hex  TODO.txt --> Creating a
   a new file test.tar that packs test00.c  test01.c  test02.c  asm.hex and  TODO.txt
    tar  cf  test_c.tar  test*.c  -->
Creating a new file test.tar that packs .c files
 
   tar  xf  test.tar --> Unpacking test.tar
    tar  cf  home.tar  /home  --> Creating a new file home.tar that packs the content of /home
    tar  tf   test.tar 
--> Showing the contents of test.tar   

13- wget
a) Description: It is a command-line utility for non-interactive download of files from the Web.
b)
Synopsis: wget  URL where URL is the web address of any resource on internet or the LAN.
c) Exemple: wget http://www.collados.org/asix1/m01/asix1_m01_eng.pdf  --> Get and download
asix1_m01_eng.pdf
 

14-nano: a text editor
a) Description:  nano  is  a small, free and friendly plain text editor.
b)
ctrl-o: Saving the current file
c) ctrl-x: Saving the current file and exiting to the bash command-line interficie.

15- which
a) Description:  which  shows where the binary file (also called executable) of any command/program that has been installed on your file system
b) Synopis: which  command_name, where command_name is the name of a command-line utiliy (for instance mkdir)
c) Example: which  mkdir

16- mlocate and update
a) Description: The mlocate command-line utility finds (or locates) files by name.
b) Installation:
    * Run as a root user: aptitude install mlocate.
     * Afterwards, 
as a root user, run: updatedb (!!!!Very important --> If you do not run this command, mlocate does not work properly)
c) Example
:
    *  mlocate -b  '\resolv.conf' --> Shows directories where a file called resolv.conf is stored.
    * mlocate  -b '*.conf'  --> Shows directories where any file with the extension conf  are stored
    * mlocate -b 'resolv.*' -->
Shows directories where files with the basename resolv and extensions are stored.


PRACTICAL EXERCISE

VERY IMPORTANT: You can not answer any question as a root user. You can only answer the questions of this exercise as a standard user!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

1- Answer the following questions:
a) Where is the personal folder of your standard user in the linux directory tree?:
b)
Where is the personal folder of root user in the linux directory tree?:
c) How is called the main directory of the linux directory tree?:
d) What is the special character used to represent
the main directory of the linux directory tree?:
e) Which is the folder where each user can find his/her personal folder?:
f) Which are the folders where you can find the system administrator (or root) binaries (applications, command,...)?:
g) Which are the folders where you can find binaries used by standard users?:

2-
Show recursively and in a tree-like format the contents of:
    a) Your personal directory.
    b) /etc/apt 


3
-
Change to the  /usr/share/grub directory using a single command. List the directory contents
showing additional relevant information about the them (owner, permissions, size, creation date, etc...).

4-
Go back to your personal directory using a single command. List your hidden and unhidden files and folders, showing additional relevant information about the them (owner, permissions, size, creation date, etc...).

5-
Create 3 new directories in your personal folder: tasktemp and help. Show the contents of your personal folder.

6-
Change to /usr/share/info directory. Copy gnupg.info.gz and find.info.gz to ~/temp
List the ~/temp directory contents. 

7-
Move
~/temp/find.info.gz  to ~/task. Change to ~/task. Check
~/task/find.info.gz file size. Expand the file. Check  the new file size.

8-
Copy ~/temp/gnupg.info.gz to ~/help. Change the name of
~/help/gnupg.info.gz to ~/help/gnupginf.gzList the ~/help directory contents.

9-
Remove gnupg.info.gz from ~/temp. Copy ~/help to ~/temp.
List the ~/temp directory contents.

10-
Remove the directories ~/temp and ~/task.

11-
S
how a list (displaying relevant information) of:
    a) All files in /bin containing "dt".   
    b)
All files in /etc ending with ".conf".
    c)
All files in /usr/lib/NetworkManager starting with "nm-".
    d) A list of files in the /etc/ssh directory with no extension.
    e)
A list of files in the /boot/grub directory
whose name contains one or more extensions.
 

12- Copy /usr/share/info/nano.info.gz in your personal folder with the help of the special character ~. Change to your personal folder
with the help of the special character ~. Show the contents of your personal folder (hidden and not hidden) with the help of ~.

13- Run a command to check only  the size of
nano.info.gz. Extract the contents of nano.info.gz. Check the size of the extracted file nano.info. Check that nano.info.gz does not exist any longer.
 
14- Create a new directory called  ~/tmp. Copy all files in the /usr/bin directory that begin with the character "z" to ~/tmp. List the contents of ~/tmp.

15- Change to ~/tmp. Pack all files inside the ~/tmp directory in a new file called zpack.tar. Afterwards, compress zpack.tar. Finally, with the only exception of zpak.tar, remove all files in ~/tmp with the help of !.

16- Create a new directory called  ~/tmpl. Copy all files in the /usr/bin directory that begin with the characters "xd" to ~/tmpl.  Pack and compress the directory ~/tmpl  in a file called tmpl.tar.gz. Afterwards, remove ~/tmpl. Run a command to check the size only of the directory ~/tmpl.tar.gz.

17- Create a folder called ~/average. Change to ~/average. Download http://www.collados.org/asix1/m01/tu1/average.tar.gz with wget.

18-
Extract and unpack 
average.tar.gz. Remove average.tar. List the contents of ~/average. Show the contents of average.c and average_README using cat.

19- Open and display README with the help of the nano text editor. Change Author to your username. Change Date to 3-10-22. Save README. Exit the nano text editor.
Run a command to display the contents of README using cat.

20- Find:
    a) Directories where the binary file of gzip, gcc and geany have been stored. Please note that
gzip, gcc and geany are commands/programs.
    b) Directories where a file called  fstab (with no extension) is stored.
    c) Directories where  files with the extension .cnf are stored
    d)
Directories where  files with the basename hosts and an extension are stored