• No results found

Lab No. 04: Command Line File processing

Objective:

To familiarize students with the directory level and file level security, compressing and archiving tech-niques.

Scope:

On the completion of this lab, students will be able to:

• Understand file types used in LINUX system.

• View and modify file access permission.

• Change the ownership of files and directories

• Create backup for critical files using archives Useful Concepts:

Access Permissions

• The ability of a user to access a file or directory.

• Three general types of permissions are read permission, write permission and execute permission.

File owner

• The user of the system to whom the file belongs.

Archive

• A collection of contents (files or directories) for backup purpose.

Compression

• A technique used for reducing the size of a file or a directory without effecting contents.

Exercise-1:- This exercise shows which types of files are used in linux and how to know bout file types

Linux considers each device as file. There are many types of file that are used in Linux. Use ls –l com-mand to find the type of file. For example:

javidali@JLaptop:/dev$ ls -l

In this example, first character in each row, I.e. the first colom represent the file type. The following table shows file types, representation symbols with short description.

Symbol File type Description

- Regular files text file, Postscript, graphics files, Hypertext (HTML), video file etc

d Directories Files that act as containers to other files

b Block files Files that are used for block (parallel)devices. Data from such devices are read or written to in blocks.

c Character files Files that are used for character (serial) devices. Data from such de-vices are read or written to character by character

l Symbolic links Shortcut to other files or directories s socket files Files that are used in communication

Regular files other than text files can be explored by using an appropriate program, as indicated in the table below. Follow the command with the name of the file to view it or omit the file name and select the file from the menu once the program is running.

File Type Application Name Command for the application

Postscript gv (based on GhostView) gv

Portable Document Format (PDF) Adobe Acrobat Reader acroread

Image files XV xv

Image files The Gimp gimp

LaTeX ouput (DVI) xdvi xdvi

Hypertext (HTML) Netscape Netscape

MP3 and MPEG gtv gtv

Here the first character in the first column (-) indicates that the file is a normal file. The next 9 characters indicate the access permissions for the file. The next set of 9 characters is divided into 3 groups of 3 cha-racters. Purpose of these characters is as under:

• (-) represents no permission

• (r) represents 'read' permission

• (w) represents 'write' permission

• (x) represents 'execute' permission

The three group represents user (owner of the file), group(to which the owner belongs) and others (any other user of the system) respectively.

Three characters in each group are for 'read', 'write' and 'execute' permission respectively.

In our example, the owner has 'read' and 'write' permission for the file and everyone else has only read permission.

For a normal file, read, write and execute permissions are obvious.

For a directory, read and write permissions mean that to read the contents of the directory and create new entries in the directory. Execute permission means that one can search in the directory but not read from or write to the directory.

You can use the chmod command to change the access permissions of a file or a directory. To specify permissions for a file with chmod, any of the following two methods can be used.

• use a symbolic mode

• use an octal mode

Symbolic mode use combination of the following symbols:

Symbol Meaning

- Remove (take away) a permission

r Read permission

w Write permission

x Execute permission

For example,if the current working directory has the these files:

javidali@JLaptop:~$ ls -l

-rwxr-xr-x 1 root root 955 2009-04-21 08:07 arithmatics.sh -rw-r--r-- 1 javidali javidali 130 2009-04-19 05:29 boidata.txt drwxr-xr-x 2 javidali javidali 4096 2009-04-08 19:56 Desktop drwxr-xr-x 2 javidali javidali 4096 2009-03-12 14:30 Documents

-rwx--- 1 javidali javidali 512000 2009-03-24 18:00 install_v10.70.2.3.tar -rw-r--r-- 1 javidali javidali 750 2009-04-28 08:42 lsresults.txt

-rwxrwxrwx 1 root root 207 2009-04-10 15:55 myscript.sh -rw-rw-rw- 1 root root 208 2009-04-10 15:55 myscript.sh~

-rw-r--r-- 1 student cs 0 2009-04-28 08:26 newfile.txt javidali@JLaptop:~$

Then gives write permission to the group and read permission to others for all files in the current directo-ry, type the command

javidali@JLaptop:~$ chmod g+w,o+r * Now look at The permissions again:

javidali@JLaptop:~$ ls -l

-rwxr-xr-x 1 root root 955 2009-04-21 08:07 arithmatics.sh -rw-r--r-- 1 javidali javidali 130 2009-04-19 05:29 boidata.txt drwxr-xr-x 2 javidali javidali 4096 2009-04-08 19:56 Desktop drwxr-xr-x 2 javidali javidali 4096 2009-03-12 14:30 Documents

-rwx-w-r-- 1 javidali javidali 512000 2009-03-24 18:00 install_v10.70.2.3.tar -rw-rw-r-- 1 javidali javidali 750 2009-04-28 08:42 lsresults.txt

-rwxrwxrwx 1 root root 207 2009-04-10 15:55 myscript.sh -rw-rw-rw- 1 root root 208 2009-04-10 15:55 myscript.sh~

-rw-rw-r-- 1 student cs 0 2009-04-28 08:26 newfile.txt javidali@JLaptop:~$

Then take away the read and write permission from the owner of the file myscript.sh, group of the owner and all other users for the files newfile.txt, type the command

javidali@JLaptop:~$ chmod a-rw myscript.sh The result is:

javidali@JLaptop:~$ ls –l myscript.sh

---x--x--x 1 root root 207 2009-04-10 15:55 myscript.sh

Often your home directory has important documents and needs to restrict access to it. If you want to takes all the three access permissions away from everyone but the user, use the command:

javidali@JLaptop:~$ chmod go-rwx 

Octal mode use the same symbols for user,group and other but for access permissions its uses octal num-bers.

Permission Octal Number Equivalent symbol

Read 4 r--

Write 2 -w-

execute 1 --x

If two or more permissions are to be set, the octal numbers are added together. For example, r-x is equiva-lent to 4+1=5 and rw-r--r-- is equivaequiva-lent to 644. now for example to give read,write and execute permis-sion to user, write permispermis-sion to the group and read permispermis-sion to others for the file mysscript.sh , type the command

javidali@JLaptop:~$ chmod 724 myscript.sh Here is the result:

-rwx-w-r-- 1 root root 207 2009-04-10 15:55 myscript.sh To secure your home drive from everyone but the user, use the command:

javidali@JLaptop:~$ chmod 700 

Exercise-3:- This exercise shows how to view and change the own ership of the file

ls -l command is can be used to view the owner of a file. For example to know that who is the owner of the file newfile.txt, issue the following command.

student@JLaptop:~$ ls –l newfile.txt

---x--x--x 1 student cs 207 2009-04-10 15:55 newfile.txt

Here column next to the number '1' represents that 'student' is the owner of this file , and next column in-dicates that group of this owner is 'cs'.

Use chown command to change the owner of a file. For example to change the owner of the file 'newfile.txt' from student to root, issue the following command.

javidali@JLaptop:~$ chown root newfile.txt The result is:

javidali@JLaptop:~$ ls –l newfile.txt

---x--x--x 1 root cs 207 2009-04-10 15:55 newfile.txt

Note that you have to change the group of the owner as well, for example, to change the owner and group of the file 'newfile.txt' both to root, try the following command.

javidali@JLaptop:~$ sudo chown root:root newfile.txt The effect will be like:

javidali@JLaptop:~$ ls –l newfile.txt

---x--x--x 1 root root 207 2009-04-10 15:55 newfile.txt

Exercise-4:- This exercise describes the ways to compress/uncompress, files and making archives of files for backup purposes.

Among many available utilities for archiving and compression purposes are the tar and gzip utilities. tar is an archiving utility while gzip is used for compressing the file size. Compressed files can be restored to their original form using gzip -d or gunzip or zcat. Now for example to backup the 'mydata' directory inside home directory of user 'student', we will first archive the directory and then compress the archive file. Steps are:

javidali@JLaptop:~$ sudo tar -cvf backup.tar /home/student/mydata/

/home/student/mydata/.synaptic/ have to compress the archive. Here is the command;

javidali@JLaptop:~$ gzip backup.tar Output of this command is a file named backup.tar.gz To uncompress the file again, issue the command

javidali@JLaptop:~$ gzip -d backup.tar.gz Or

javidali@JLaptop:~$ gunzip backup.tar.gz

Or you can use tar command to uncompress and extract contents of the file. Example is:

javidali@JLaptop:~$ tar -xz backup.tar.gz

Where -z switch issues gzip command from within the tar command and -x switch extract the contents.

To list contents of an archive use -t switch with tar command:

javidali@JLaptop:~$ tar -t backup.tar

And to extract the contents of the archive, issue the following command.

javidali@JLaptop:~$ tar -xvf backup.tar

Exercises for lab:

Exercise 1:- Change the access permission of to the home directory of your login user to match drwxr-xr-x using both symbolic mode & octal mode.

Exercise 2:- Create a file and modify its access permission so that the file can be executed by user, group and others besides its current permission setting.

Exercise 3:- Create and archive and add all subdirectories of your home directory to that archive.

Home Work:

1) Search the manual page of the utilities gzip and tar and write a procedure to add files and directo-ries to and existing archive.

2) Write a note on the advantages of file level security in operating systems.

3) Why do we often compress files before adding to an archive?