Lab No - 04 - File Operation and Permission.
Lab No - 04 - File Operation and Permission.
Objectives:
Answer:
File Operations
A file is an abstract data type. For defining a file properly, we need to consider the operations that
can be performed on files. The operating system can provide system calls to create, write, read,
reposition, delete, and truncate files.
There are six basic file operations within an Operating system. These are:
Creating a file: There are two steps necessary for creating a file. First, space in the file
system must be found for the file. We discuss how to allocate space for the file. Second, an
entry for the new file must be made in the directory.
Writing a file: To write to a file, you make a system call specify about both the name of the
file along with the information to be written to the file.
Reading a file: To read from a file, you use a system call which specifies the name of the file
and where within memory the next block of the file should be placed.
Repositioning inside a file: The directory is then searched for the suitable entry, and the
'current-file-position' pointer is relocating to a given value. Relocating within a file need not
require any actual I/O. This file operation is also termed as 'file seek.'
Deleting a file: For deleting a file, you have to search the directory for the specific file.
Deleting that file or directory release all file space so that other files can re-use that space.
Truncating a file: The user may wish for erasing the contents of a file but keep the
attributes same. Rather than deleting the file and then recreate it, this utility allows all
attributes to remain unchanged — except the file length — and let the user add or edit the
file content.
File permissions
Every file and directory in your UNIX/Linux system has following 3 permissions such as -
1) Read
2) Write
3) Execute permission
Read (r): this gives permission to merely open a file or folder and view its contents.
Write (w): this gives permission to overwrite, append-to or delete a file or folder.
Execute (x): this gives permission to "run" a file. For example to run a script or a program.
1. the ls -command: It shows the contents of a particular directory – both files and directories.
2. ls -R command: We can all also find child files and directories by providing the recursive option.
We will provide the -R option which will list files and folders recursively.
3. ls -a command:All operating systems have hidden files to hide them from use. It is not a
security-related feature. It related to operation and reliability. We can see that files and folders
like .cpan, .npm, .config etc. are listed with the help of -a option.
7. pwd command: We can get the current working directory with pwd command.
Implementation of File permission
ls -l command
One can view the permissions by checking the file or directory permissions i your favorite GUI File
Manager or by reviewing the output of the “ls -l” command while in the terminal and while
working in the directory which contains the file or folder.
u – Owner
g – Group
o – Others
a – All users
The potential Assignment Operators are + (plus) and – (minus); these are used to tell the system
whether to add or remove the specific permissions.
r – Read
w – Write
x – Execute
So for an example, lets say I have a file named file1 that currently has the permissions set to
_rw_rw_rw, which means that the owner, group and all users have read and write permission.
Now we want to remove the read and write permissions from the all users group.
To make this modification you would invoke the command: chmod a-rw file1
To add the permissions above you would invoke the command: chmod a+rw file1