M01: Introduction to Operating Systems
TU1: Installing, configuring and exploting a computer system
ASIX1
Practical Exercise 5: Running basic command-line utilities. Working with bash
10-11-12

Practical Exercise 5: Running basic command-line utilities. Working with bash


GENERAL CONDITIONS
1- Deadline: 24-11-2012.
2- Send your report as a PDF file attached to an e-mail with the following specifications:
     a) E-mail address:
cf(at)collados.org or jordi.binefa(at)fje.edu depending who is your teacher
     b) File Name:
        b.1)
ASIX1 (Catalan): asix1_surname_name_m01tu01pr5.pdf
        b.2)
DAW1 (English): daw1_surname_name_m01tu01pr5.pdf
    
c) Subject:
       c.1)
ASIX1 (Catalan): asix1_surname_name_m01tu01pr5
       c.2) DAW1 (English): daw1_surname_name_m01tu01pr5
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
6- Page numbering on footer bar

BASIC UTILITIES: DOCUMENTATION
1- passwd
The passwd command-line utility
changes passwords for user and group accounts.  While an administrator may change the password for any account or group, a  normal user is only allowed to change the password for their own account.
If an old password is present, the user is first promted for it and the password is compared agaisnt the stored one. After the user is authenticated, the  user  is  then prompted for a replacement password. Care must be taken to not include special control characters or characters, which are not available on all keyboards.  If the password is accepted, passwd will prompt again and compare the second entry against the first.  Both entries  are  require to match in order for the password to be changed.

2- date
The date utility shall write the date and time to standard output (screen). If an operand beginning with '+' is specified, the output format of date shall  be  controlled  by  the  conversion  specifications. Particularly interesting is running: 

date +"20%y%m%d%H%M%S"

where:  %y = year, %m = month, %d =day, %H = hour, %M = minute and %S = second.

3- uname
The uname
command-line utility prints certain system information. Some options can be used to modify the behaviour of the command uname. Some useful options are:
-a: print all information -->  uname  -a.
-r: print the kernel version  --> uname  -r.

4- man
a) An interface to the on-line reference manuals. If you want to know how a command works, you have to run the command:  man  command_name. For instance, if you want to know how the ifconfig command works you have to run: man  ifconfig, or
if you want to know how the uname command works you have to run: man  uname.
b) Some manuals have two or more sections. If you are asked about what section you want to read, like for instance after running man uname:
man  uname
Man: find all matching manual pages (set MAN_POSIXLY_CORRECT to avoid this)
 * uname (1)
   uname (2)
   uname (1p)
   uname (3p)
Man: Quina pągina de manual voleu?
Man:
You have to choose the number between brackets and press enter.  If you do not select any option, the default option (option after *  symbol) will be chosen.
c)
Once your finished viewing a manual,  you can exit or close the manual by pressing the q key
 
5- 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 (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.
        ls -a --> Lists hidden files
6- 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 .

7- 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:
    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 home directory which is "/home/username". The "~" indicates the users home directory.
    cd -  --> Changes to the previous directory. The "-" indicates the previous directory

8- pwd
a) Description: The pwd command-line utility shows the name of the current working directory.
b) Synopsis:  pwd

9- Special character * (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 folder that begin with the string "prov" then you can run the command: ls  -ls  prov*.
b) Example:
etpclot@etpclot:~>ls  -ls
amemo    memo              memoalx.0620    memosally     user.memo
mem        memo.0612     memoalz.keep    sallymemo
memalx    memoa            memorandum     typescript
etpclot@etpclot:~>ls  -ls  memo*      
memo      memo.0612    memoa    memoalx.0620    memoalx.keep    memorandum    memosally
(it shows all files in the working directory that begin with the string memo)
etpclot@etpclot:~>ls  -ls  *mo           
amemo    memo              sallymemo        user.memo
(it shows all files in the working directory that end with the string mo)                 
etpclot@etpclot:~>ls  -ls  *alx*     

memalx    memoalx.0620    memoalx.keep
(it shows all files in the working directory that contain the string alx)                 

10- cp
a) Description: The cp command-line utility copies one o more source 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 ~.

11- 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

12- 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 ~.

13- 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.
b) Note 1 : you can use .,..,- and ~.

14-
whereis
a) Description: The whereis command-line utility shows where the binary, source and manual page files are for a command.
b) Synopis: whereis  command_name, where command_name is the name of a command-line utiliy (for instance mkdir)
c) Example: whereis  mkdir.
d) Some useful options are:
    -b: Searchs only for binaries.
    -m: Searchs only for manual sections.
    -s: Searchs only for sources.

15- 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 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.

16- 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) Examples:
    tar  cf  test.tar  test00.c  test01.c  test02.c  asm.hex  TODO.txt
    tar  cf  test_c.tar  test*.c
    tar  xf  test.tar
    tar  cf  home.tar  /home
   
17- nano: a text editor
a) Description: The nano command_line utility  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.


PRACTICAL EXERCISE
1- Change to the  /usr/share/info directory step by step going through the tree of files and directories
2-
Change to the  /usr/share/info directory using a single command.
3- Change to your home folder using only a single command.
4- List your hidden files, showing extra information (owner, permissions, etc...).
5- Create a new directory called job in your home folder.  Create another directory in your home folder called tmp.
6- Change to /usr/share/info directory. Copy grep.info.gz and find.info.gz to ~/tmp.
7- Could you copy grep.info.gz to /etc. What is happening?.
8- Move
find.info.gz to ~/job. Check  the allocated size of this file. Expand the file. 
Check  the new allocated size of this file.
9- Copy  grep.info.gz in ~/tmp to your home folder and changing its name to test-HOWTO.gz.
10- Remove grep.info.gz from ~/tmp.
11- Remove the directories ~/tmp and ~/job.
12-
S
how all files in the /dev directory that contain the string "sd" and "tty".   
13- Copy /usr/share/info/grep.info.gz to ~, changing the name to INDEX.gz.Extract INDEx.gz. Display the contents of the file INDEX.Whereis INDEX.gz?
14- Create a new directory called  ~/temporal. Copy
all files in the /bin directory that begin with the character "g" to ~/temporal. Pack all files in the ~/temporal directory in a new file called g_pack.tar.
15- Remove all files in ~/temporal but g_pack.tar.
16- Compress g_pack.tar.
17-
Create a new directory called ~/temporalv1. Move ~/temporal/g_pack.tar.gz to ~/temporalv1.
18- Extract ~/temporalv1/g_pack.tar.gz. Unpack ~/temporalv1/g_pack.tar. Remove ~/temporalv1/g_pack.tar. List all files in ~/temporalv1.
19- Change to your home directory. Pack ~/temporalv1 in a new file called dir_temporalv1.tar. Compress dir_temporalv1.tar. Remove ~/temporalv1.
20- List all files in your home folder. Extract and unpack  
dir_temporalv1.tar.gz. Check if ~/temporalv1 exists another time. Check all files in the restored ~/temporalv1 directory.
21- Create a new directory called ~/bin. Move ~/temporal to ~/bin changing its name to tmpv1.
22- Where are the binary code of gzip and tar commands?.
23-Create a text  in your home folder using nano. Write the next script:
# Script scr01.sh
#
#!/bin/bash
clear
echo "Hello $USER"
ls -ls ~
exit 0
Save the script  with the following specifications: a) File Name: script01.sh. Run the command chmod 777 scrip01.sh. Run script01.sh using the command ./script01.sh.