Usp Unit-2
Usp Unit-2
Syllabus: The File system –The Basics of Files-What’s in a File-Directories and File Names-
Permissions-I Nodes-The Directory Hierarchy, File Attributes and Permissions-The File
Command: knowing the File Type-The Chmod Command: Changing File Permissions-The
Chown Command: Changing the Owner of a File-The Chgrp Command: Changing the Group of
a File.
1
may have files with the same names. For example, we can create demo.txt directory in
/tmp.
2. WHAT’S IN A FILE
Depending on content of the file and behavior of the permissions granted to the files, Unix files
are classified into the following categories.
Regular files / Ordinary files
Directory files
Device files
Regular Files
Regular files hold data and executable programs. Executable programs are the commands (ls)
that you enter on the prompt. The data can be anything and there is no specific format enforced
in the way the data is stored. The regular files can be visualized as the leaves in the UNIX tree.
Directory Files
Directories are files that contain other files and sub-directories. Directories are used to organize
the data by keeping closely related files in the same place. The directories are just like the folders
in windows operating system. The kernel alone can write the directory file. When a file is added
to or deleted from this directory, the kernel makes an entry. A directory file can be visualized as
the branch of the UNIX tree.
Device Files
Device or special files are used for device I/O on UNIX and Linux systems. They appear in a file
system just like an ordinary file or a directory. On UNIX systems there are two flavors of special
files for each device, character special files and block special files. Linux systems only provide
one special file for each device.
When a character special file is used for device I/O, data is transferred one character at a time.
This type of access is called raw device access.
When a block special file is used for device I/O, data is transferred in large fixed-size blocks.
This type of access is called block device access.
2
3. DIRECTORIES AND FILE NAMES
All file names are case sensitive. So filename jsp.txt Jsp.txt JSP.txt all are three different files.
We can use upper and lowercase letters, numbers, “.” (period/dot), “-“ (hyphen) and “_”
(underscore) symbols. We can use other special characters such as blank space, but they are hard
to use and it is better to avoid them.
In short, filenames may contain any character except / (root directory), which is reserved as the
separator between files and directories in a pathname. You cannot use the null character. Most
UNIX limit filename to 255 characters (255 bytes). However, most versions of UNIX system
consider filenames upto 14 characters only.
A filename must be unique inside its directory. For example, consider that a directory jsp
contains a file name called demo.txt. Inside /home/jsp directory you cannot create a demo.txt file
and demo.txt directory name as that name already exists. However, other directory may have
files with the same names. For example, we can create demo.txt directory in /home/priya.
COMMANDS RELATED TO FILES
Possible operations that can be done on UNIX files are
Create
Remove/Delete
Display
Copy
Move/Rename
List
Creating a file
Files can be created using two commands
cat command
touch command
cat command : We can create a new file with the name file1.txt using the following cat
command and you can type the text you want to insert in the file. To save the file, type
‘Ctrl-d’ at the end.
syntax: cat > filename
Type some content Here
Ctrl D
3
Example:
$ cat > file1.txt
This is my new file in Linux.
The cat command is very useful.
Thanks
^d
touch command : The touch command is the easiest way to create new empty files.
syntax : touch filename(s)
for example, the following command would create three new, empty files named
file1, file2 and file3:
touch file1 file2 file3
Removing a file
rm command is used to delete or remove file
Syntx: rm filenme
Ex: rm it3
File called it3 will be removed nd if we do ls the file nme will not be in list.
rm commnd can also be used to remove multiple files as
rm file file2 file3
Display the contents of file
Cat commnd is used to display the contents of a file.
Syntx: cat filenme
Ex: cat it3
Displays the contents of file called it3.
Copy one file to another file
cp command is used to copy the contents of a file into another file.
Syntx: cp file1 file2
file1 is source file and file2 is destination file. Contents of file1 are copied into file2. After
copying both the files can be listed using the ls command.
Ex: cp it3 thirdit
Contents of file called it3 are copied into another file called thirdit.
4
Move/Rename file
A file can be moved to another file using mv command. It is like renaming.
Syntax: mv file1 file2
Contents of file1 are moved to file2. After the execution of this command, only file2 is listed
using ls because file1 is no more present. File1 is removed once its contents are moved to file2.
Ex: mv it3 thirdit
Contents of it3 file are moved into the file called thirdit and the file it3 is deleted from the file
system. It is also considered as it3 is renamed as thirdit.
Listing the file name
ls command is used to list or display the file names available in the current directory.
Syntx: ls [-options]
Ex: ls
Displays the file names and sub-directory names in the current directory.
COMMANDS RELATED TO DIRECTORIES
A directory is a file that holds the file names and sub-directory names. Every directory has .
and .. as the initial contents. Their significance is as shown below
. Current directory
.. Parent of current directory
Path names are used to denote the way to a particular file or sub-directory. There are two types
of paths Absolute Path & Relative Path
Absolute Path is the path that starts from the root directory as
/home/students/it/it3/16FE1A1255/fact.sh
Relative Path is the path that starts from present working directory or current directory as
16FE1A1255/fact.sh
Possible operations that can be done on UNIX directory files are
Create
Remove/Delete
Change directory
Copy
Move/Rename
List
5
Creating a directory
The mkdir command in UNIX allows users to create directories. The mkdir command can create
multiple directories at once and also set permissions when creating the directory.
Syntax: mkdir directoryname
Ex: mkdir ITSTU
We can also use mkdir command to create more than one directory in a single line as
Syntax: mkdir dir1 dir2 dir3 ..
Ex: mkdir IT2 IT3 IT4
Inside a sub directory we can create sub directories using mkdir command. For this, we need to
first move into sub directory and there create a sub directory. Suppose if we are in STUDENT
directory, we can create a sub directory as
mkdir ITSTU
Using cd ITSTU, we can move to sub directory called ITSTU. There we can create another sub
directory with a name IT3. This creation of sub directories can be done in a single mkdir
command.
Syntax: mkdir dir1/dir2
Ex: mkdir ITSTU/IT3
Removing a directory
The rmdir command removes empty directoriesfrom your filesystem. The rmdir command
removes each directory specified on the command line, if they are empty. That is, each directory
removed must contain no files or directories, or it cannot be removed by rmdir. To remove both
a parent directory and a subdirectory of that parent, the subdirectory must be specified first, so
the parent directory is empty when rmdir tries to remove it. rmdir is functionally equivalent to
the command rm -d.
Syntax: rmdir dirname
Ex: rmdir IT3
More than one directory can also be removed usin rmdir at a time.
Syntax: rmdir dir1 dir2 dir3 ..
Ex: rmdir IT2 IT3 IT4
6
Changing to other directory
The cd command, which stands for "change directory", changes the shell's current working
directory. cd is among the commands you will use most often on the command line. It changes
your working directory. Use it to move around within the hierarchy of your file system. Cd is a
navigation command.
The current directory, regardless of which directory it is, is represented by a single dot (".").
So, running this command:
Syntax: cd .
It would change us into the current directory. In other words, it would do nothing.
To move to a sub directory , use the following syntax.
Syntax: cd subdirname
Ex: cd ITSTU
It is same as that of cd ./ITSTU
The parent directory of the current directory — in other words, the directory one level up from
the current directory, which contains the directory we're in now — is represented by two dots
("..").
So, If we were in the directory /home/username/documents, and we executed the command:
Syntax: cd ..
7
Copy file to another directory
cp command is used for copying a directory into another directory
Syntax: cp source_dir1 dest_dir2
Ex: cp VIGNAN LARA
All files and sub directories present in VIGNAN are copied into LARA directory.After executing
this command both source and destination directory are present in the file system.
Copying the files and sub directories of source directory recursively into destination directory is
known as recursive copying. This is done by using following option of cp command.
Syntax: cp –r source_dir1 dest_dir2
Ex: cp –r VIGNAN LARA
When the destination directory has any content (files and sub directories), those will be deleted
and files present in source directory are copied into that directory. The destination directory
contents are replaced by source directory contents. This is done automatically. If we are required
to prompt a message if the destination directory contents to be replaced or nor, the option is –i.
Syntax: cp –i source_dir1 dest_dir2
Ex: cp –i VIGNAN LARA
Various files can be copied into a directory at a time using cp command. Here we need to
mention all the file names that are going to be copied and the directory name.
Syntax: cp file1 file2 file3 dirname
Ex: cp it2 it3 it4 ITSTU
The directory name can also have absolute path or relative path of sub directories also.
Move/Rename directory
mv command is used to move a directory in to another directory.
Syntax: mv source_dir1 dest_dir2
Ex: mv VIGNAN LARA
All files and sub directories present in VIGNAN are moved into LARA directory. After
executing this command source directory is removed and only destination directory is present in
the file system.
Moving the files and sub directories of source directory recursively into destination directory is
known as recursive moving. This is done by using following –r option of mv command.
Syntax: mv –r source_dir1 dest_dir2
8
Ex: mv –r VIGNAN LARA
When the destination directory has any content (files and sub directories), those will be deleted
and files present in source directory are moved into that directory. The destination directory
contents are replaced by source directory contents. This is done automatically. If we are required
to prompt a message if the destination directory contents to be replaced or nor, the option is –i.
Syntax: mv –i source_dir1 dest_dir2
Ex: mv –i VIGNAN LARA
Various files can be moved into a directory at a time using mv command. Here we need to
mention all the file names that are going to be moved and the directory name.
Syntax: mv file1 file2 file3 dirname
Ex: mv it2 it3 it4 ITSTU
The directory name can also have absolute path or relative path of sub directories also.
Listing the directory contents
ls command is used to list the file names and sub directory names held by the given directory.
Syntax: ls [optons]
Ex: ls
4. PERMISSIONS
Every file and directory on your Unix/Linux system is assigned 3 types of owner, given below.
User
A user is the owner of the file. By default, the person who created a file becomes its owner.
Hence, a user is also sometimes called an owner.
Group
A user- group can contain multiple users. All users belonging to a group will have the same
access permissions to the file. Suppose you have a project where a number of people require
access to a file. Instead of manually assigning permissions to each user, you could add all users
to a group, and assign group permission to file such that only this group members and no one
else can read or modify the files.
Other
Any other user who has access to a file is other. This person has neither created the file, nor he
belongs to a user group who could own the file. Practically, it means everybody else. Hence,
when you set the permission for others, it is also referred as set permissions for the world.
9
Every file and directory in your UNIX/Linux system has following 3 permissions defined for all
the 3 owners discussed above
Read: This permission give you the authority to open and read a file. Read permission on a
directory gives you the ability to lists its content.
Write: The write permission gives you the authority to modify the contents of a file. The write
permission on a directory gives you the authority to add, remove and rename files stored in the
directory. Consider a scenario where you have to write permission on file but do not have write
permission on the directory where the file is stored. You will be able to modify the file contents.
But you will not be able to rename, move or remove the file from the directory.
Execute: In Windows, an executable program usually has an extension ".exe" and which you can
easily run. In Unix/Linux, you cannot run a program unless the execute permission is set. If the
execute permission is not set, you might still be able to see/modify the program code(provided
read & write permissions are set), but not run it.
The ls -l command is used to check the permissions on the file or directory.The following is the
example
Here, we have highlighted '-rw-rw-r--'and this weird looking code is the one that tells us about
the permissions given to the owner, user group and the world.
Here, the first '-' implies that we have selected a file.p>
10
An inode is a data structure on a traditional Unix-style file system. An inode stores basic
information about a regular file, directory, or other file system object.
A Unix file is "stored" in two different parts of the disk - the data blocks and the inodes. The data
blocks contain the "contents" of the file. The information about the file is stored elsewhere - in
the inode.
Inode consists of
File type
File permissions
UID of owner
GID of group owner
No.of links
Size in bytes
Date & time of last modification
Date & time of creation of file
Date & time of last modification of inode
An array of pointers thatpoint to the block where file content is stored
Both the inodes and data blocks are stored in a "filesystem" which is how a disk partition is
organized. But these inodes are strange and confusing. ls –i list the Inode number of a file.
[vhemapriya@lara ~]$ ls -i
4457563 adsr16 4457351 dij.cpp 4456753 merge.cpp 4457272 t.c
4457570 avl.cpp 4457183 dp.c 4457064 quick1.cpp 4457278 unix
4457439 bst.cpp 4456766 DS 4457048 quick.cpp 4456955 while.c
The "-i" option lists the inode number before the filename.
stat command is used to display file statistics that also displays inode number of a file
[vhemapriya@lara ~]$ stat mft.c
File: ‘mft.c’
Size: 923 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 12847546 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 9554/vhemapriya) Gid: ( 9554/vhemapriya)
Context: unconfined_u:object_r:user_home_t:s0
11
Access: 2019-07-22 10:28:58.565795866 +0530
Modify: 2019-07-22 10:28:56.759795808 +0530
Change: 2019-07-22 10:28:56.790795809 +0530
6. DIRECTORY HIERARCHY
When it comes to the management and access of a large number of flies in Unix, all related files
are grouped into a single group. For example, all binary files are grouped together, all temporary
files are grouped together, and all device flies are grouped together. Therefore there are number
of groups of files.
Each group constitutes a directory or a sub-directory and is referred to by an appropriate name.
All these grouped files, that is, directories and sub directories are arranged in the form of an
inverted tree like hierarchical structure as shown in following figure. This inverted tree like
organization of all the flies is called the file system. The figure shows a typical Unix file system
with only a few of the possible directories and files that exist in it, but practically there will be
many more directories and files.
1 /
This is the root directory which should contain only the directories needed at the top
level of the file structure
12
2 /bin
This is where the executable files are located. These files are available to all users
3 /dev
These are device drivers
4 /etc
Supervisor directory commands, configuration files, disk configuration files, valid user
lists, groups, Ethernet, hosts, where to send critical messages
5 /lib
Contains shared library files and sometimes other kernel-related files
6 /boot
Contains files for booting the system
7 /home
Contains the home directory for users and other accounts
8 /mnt
Used to mount other temporary file systems, such as cdrom and floppy for the CD-
ROM drive and floppy diskette drive, respectively
9 /proc
Contains all processes marked as a file by process number or other information that is
dynamic to the system
10 /tmp
Holds temporary files used between system boots
11 /usr
Originally the directory holding user home directories, its use has changed, and it now
holds executables, libraries, and shared resources that are not system critical: X11, KDE,
PERL, LaTeX etc. It is now used for miscellaneous purposes, and can be used by many
users. Includes administrative commands, shared files, library files, and others
12 /var
13
Typically contains variable-length files such as log and print files and any other type of
file that may contain a variable amount of data
13 /sbin
Contains binary (executable) files, usually for system administration. For
example, fdisk and ifconfig utlities
14 /kernel
Contains kernel files
14
Other
Any other user who has access to a file. This person has neither created the file, nor he belongs
to a user group who could own the file. Practically, it means everybody else. Hence, when you
set the permission for others, it is also referred as set permissions for the world.
Every file and directory in your UNIX/Linux system has following 3 permissions defined for all
the 3 owners discussed above
Read: This permission give you the authority to open and read a file. Read permission on a
directory gives you the ability to lists its content.
Write: The write permission gives you the authority to modify the contents of a file. The write
permission on a directory gives you the authority to add, remove and rename files stored in the
directory. Consider a scenario where you have to write permission on file but do not have write
permission on the directory where the file is stored. You will be able to modify the file contents.
But you will not be able to rename, move or remove the file from the directory.
Execute: In Windows, an executable program usually has an extension ".exe" and which you can
easily run. In Unix/Linux, you cannot run a program unless the execute permission is set. If the
execute permission is not set, you might still be able to see/modify the program code(provided
read & write permissions are set), but not run it.
The ls -l command is used to check the permissions on the file or directory.The following is the
example
Here, we have highlighted '-rw-rw-r--'and this weird looking code is the one that tells us about
the permissions given to the owner, user group and the world.
Here, the first '-' implies that we have selected a file.p>
15
r = read permission
w = write permission
x = execute permission
- = no permission
Let us look at it this way.
The first part of the code is 'rw-'. This suggests that the owner 'Home' can:
Read the file
Write or edit the file
He cannot execute the file since the execute bit is set to '-'
The second part is 'rw-'. It for the user group 'Home' and group-members can:
Read the file
Write or edit the file
The third part is for the world which means any user. It says 'r--'. This means the user can only:
Read the file
The table below gives numbers for all for permissions types.
Number Permission Type Symbol
0 No Permission ---
1 Execute --x
2 Write -w-
16
3 Execute + Write -wx
4 Read r--
5 Read + Execute r-x
6 Read +Write rw-
7 Read + Write +Execute rwx
Type of user
Type of operation
Type of permission
Type of user
Users of Unix system are represented as
User Description
17
g (group) users who are members of the file's group
o (others) users who are neither the file's owner nor members of the file's group
Type of operation
The operation is represented with an operator is used to specify how the modes of a file should
be adjusted. The following operators are accepted:
Operator Description
= The modes specified are to be made the exact modes for the specified classes
Type of permission
There are three types of permissions in UNIX given for the users.
Permission Description
r Permission to read
18
adds execute permission to group with out altering the existing permissions.
Ex2 :
chmod o-w itfile
and removes write permission to others with out altering the existing permissions.
These two exmples can be used in a single command as
chmod g+x o-w itfile
Absolute Permission Assignment
With this type, existing permissions will be erased and the mentioned permissions are assigned.
For this assignment = and octal numbers are used.
Ex1 :
chmod a=x itfile
Now ifile will have only execute permission for all users because all other exissting perissions
are erased.
Ex2 :
Chmod 635 itfile
File called itfile will have the permisions as followed after this commnd
6 is assigned to owner – only read and write permissions
3 is assigned to group – only write and execute permissions
5 is assignred to others - only read and execute permissions
This assignment is done by erasing the existing permissions.
10. THE CHOWN COMMAND CHANGING THE OWNER OF A FILE
chown - To change owner, change the user and/or group ownership of each given File to a new
Owner. Chown can also change the ownership of a file to match the user/group of an existing
reference file.
Synatx: chown NewOwner FileName
Following are the examples of how the owner/group can be specified:
If only an OWNER (a user name or numeric user id) is given, that user is made the owner of
each given file, and the files' group is not changed. If the OWNER is followed by a colon or dot
and a GROUP (a group name or numeric group id), with no spaces between them, the group
ownership of the files is changed as well (to GROUP).
EXAMPLES
19
Change the owner of file.
$ chown user1 sample.txt
Change the group of file.
$ chown :mygroup file.txt
Change both the owner and group of file in single command.
$ chown user1:mygroup file.txt
20