Linux/C2/File-Attributes/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: The Linux File Attributes

Author: Shahid Ali Farooqui

Keywords: File permissions, ownership of a file, group, inode, soft and hard links.

Prerequisite: Please create empty files named as example1, example2, example3, example4,

example5, and testchown. Please also create empty directories named as test_chown and

directory1

Resources for the tutorial:File:Resources for file attributes.zip

Visual Cue
Narration
Display slide 1 Welcome to this spoken tutorial on Linux File Attributes.
Switch to terminal The prerequisite for this tutorial is to create empty files named as example1, example2, example3, example4, example5, and testchown. Please create empty directories named as test_chown and directory1 also
Display slide 2 A file attribute is metadata that describes or is associated with a computer file. File attribute is the characteristics that describe a file, such as owner, file type, access permissions, etc.
Display slide 3 chown command is used to change the ownership of the file or directory. This is an admin command, root user only can change the owner of a file or directory.
The syntax of chown command is chown space [options] space ownername space filename or directoryname
We may give following options with chown command.

-R : To change the permission on files that are in the subdirectories of the directory that you are currently in.

-c : Change the permission for each file.

-f : Prevents chown from displaying error messages.

Display slide 4 Now we will look at some examples
Switch to terminal So go to the terminal. We will move to the directory where we have created empty files and folders. For that cd space Desktop slash file_attr and press enter
Now type the command

$ ls space -l spacetestchown and press Enter.


-rw-r--r-- 1 shahid shahid 0 2010-12-14 16:28 testchown

Here we can see the owner of the file 'testchown' is shahid
To change the owner of the file, type the command


$ sudo space chown space anusha space testchown

press Enter

Enter the sudo password and press Enter again. Press enter
Now type


$ ls space -l spacetestchown


and press Enter


-r--r--r-- 2 anusha shahid 0 2010-12-14 02:05 testchown


Here we can see the new owner of the file is anusha.

Now we will see how to change owner of the proposal software directory

Type the command


$ ls -l


press Enter


drwxr-xr-x 2 shahid shahid 4096 2010-12-14 16:31 test_chown


Here we can see the owner of the directory 'test_chown' is shahid

To change the owner of the directory, type the command


$ sudo space chown space minus capital R space anusha spacetest_chown which is directory name


and press Enter

Enter the sudo password, if required and press Enter again.

I will clear the screen by pressing Clt+L for our convenience. Now type


$ ls space -l


press Enter


drwxr-xr-x 2 anusha shahid 4096 2010-12-14 16:31 test_chown


Here we can see the new owner of the directory is anusha.

Display slide 5 chmod command is used to change the access mode or permissions of one or more files.
Syntax of the chmod command is


chmod space [options] space mode space filename space chmod space [options] space filename


We may give the following options with chmod command.

-c : Print information about files that are changed.

-f : Do not notify user of files that chmod cannot change.

Display slide 6 There are following types of access or permissions

r : Read

w : Write

x : Execute

s : Set user (or group) ID

Switch to terminal Alternatively, we may specify permissions by a three-digit octal number. The first digit stands for owner permission, the second stands for group permission, and the third stands for other's permission.
Permissions are calculated by adding the following octal values:

4 : Read

2 : Write

1 : Execute

Display slide 7 Now we will look at some examples of chmod

Move to terminal and enter the command to add execute-by-user permission to file example1 Before that i will again clear the screen by pressing Clt+l.

Now lets type


$ chmod space u+x space example1


press Enter

Now type


$ ls space -l space example1


press Enter to see the changes


-rwxr--r-- 1 shahid shahid 0 2010-12-14 02:05 example1


here you can see to assign the read/write/execute permission by owner, read/execute permission by group, and execute-only permission by others to file example1

Now type the command


$ chmod space 751 space example1


press Enter

Now type


$ ls space -l space example1

and press Enter


-rwxr-x--x 1 shahid shahid 0 2010-12-14 02:05 example1


Here we can see that above command has assigned the read/write/execute permission by owner, read/execute permission by group, and execute-only permission by others to file example1.

Display slide 8 To assign read-only permission to file example1 for everyone type the command


$ chmod space =r space example1


and press Enter

Now type the command


$ ls space -l space example1


press Enter


-r--r--r-- 1 shahid shahid 0 2010-12-14 02:05 example1


Here we can see that the read only permission has been assigned to file example1 for everyone

To change the permission recursively and assign the read and execute access for everyone and also write access for the owner of the directory directory1 type the command


$ chmod space minus capital R space 755 space directory1


press Enter

Now type


$ ls space -l


press Enter to see the changes


drwxr-xr-x 2 shahid shahid 4096 2010-12-14 02:14 directory1

Display

slide 9

To give the user execute permission on file example2

Type the command


$ chmod space u+x space example2


press Enter

Now type the command


$ ls space -l space example2


and press Enter


-rwxr--r-- 1 shahid shahid 0 2010-12-14 02:17 example2


Here we can see that execute permission has been assigned to user on example2

To add the write permissions to the group for file example3

type the command


$ chmod space g+w space example3


press Enter

and now type


$ ls space -l space example3


press Enter


-rw-rw-r-- 1 shahid shahid 6 2010-12-14 00:31 example3


Here we can see the write permission has been added to group

To remove the write permissions for all type the command


$ chmod space a-w space example3


press Enter

Now type


$ ls space -l space example3


press Enter

-r--r--r-- 1 shahid shahid 6 2010-12-14 00:31 example3


Here we can see the write permission for all has been removed

Display slide 10 Chgrp

chgrp command is used to change the group of one or more files to newgroup. Newgroup is either a group ID number or a group name located in /etc/group. Only the owner of a file or a privileged user may change the group.

The syntax for the chgrp command is


chgrp space [options] space newgroup space files space chgrp space [options]

Switch to terminal Lets go to terminal.

Now we will look at some examples of chgrp command.

type the command

$ ls space -l space example4


press Enter


-rw-r--r-- 1 shahid shahid 0 2010-12-14 00:48 example4


Here we can see the group permission is for user shahid

To change the group permission , type the command


$ sudo space chgrp space rohit space example4


press Enter


Enter the sudo password if required.

Now type the command


$ ls space -l space example4


press Enter


-rw-r--r-- 1 shahid rohit 0 2010-12-14 00:48 example4


Here we can see that the group has changed from shahid to rohit.

Display slide 11 The inode number is a unique integer assigned to the device. Inode stores basic information about a regular file or a directory. All the files are hard links to inodes. Whenever a program refers to a file by name, the system actually uses the filename to search for the corresponding inode.
We can use ls space -i command to see the inode number of a file.

type the command


$ ls space -i space example5


press Enter


Number written before the file is the inode number of the file.

Display slide 12 Inodes are associated with precisely one directory entry at a time.

Hard links are to associate multiple directory entries with a single inode.

ln is the command to make link

The syntax of ln command to create the hard link is


ln space {source} space {link}


Where, source is an existing file and link is the file to create.

Display slide 13 Now we will look at some examples of hard links


Let me clear the screen again. Now type the command

$ ln space example1 space exampleln


press Enter

Switch to terminal To display the inode number of both the files, type the command


$ ls space -i space example1 space exampleln


press Enter


Here we can see that inode number of both the files are same, file exampleln is the hard link for file example1

Display slide 14 Soft link (symbolic link) is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path.
The syntax of ln command to create soft links is


ln space -s space {target-filename} space {symbolic-filename}

Display slide 15 Now we will look at some examples of soft links
To create the soft link, type the command


$ ln space -s space example1 space examplesoft


press Enter

Switch to terminal Now, to display the inode number and list of both the files, type the command


$ ls space -li space example1 space examplesoft


press Enter


Here we can see that inode number of both the files are different and examplesoft is a softlink of example1.

Display slide 16 So in this tutorial we have learnt about the Linux Files Attributes like changing permission, ownership and group of a file.

We also learnt about the inode, soft and hard links of a file. This brings me to the end of this tutorial. Spoken Tutorials are a part of the Talk to a Teacher project, supported by the National Mission on Education through ICT.

More information on the same is available at the following link http://spoken-tutorial.org /NMEICT-Intro . This is Anusha Kadambala and Shahid from IIT Bombay signing off . Thanks for joining.

Contributors and Content Editors

Chandrika, PoojaMoolya