1. Introduction to Unix and OS
Module 2
More File Attributes
Dr. Girisha G S
Dept. of CSE
SoE,DSU, Bengaluru
2. Agenda
• File System and inodes
• Hard links – ln command
• umask command
• find command
3. File Systems and inodes
File system:
- Each physical drive can be divided into several partitions
- Each partition can contain one file system
- Each file system has its own inode table
- All attributes of a file except its name and contents are available in a table – inode
(index node), accessed by the inode number
- Each file is assigned an inode number which is unique within the file system
- The inode contains the following attributes of a file:
4. - To know inode number of a file, issue the ls command with –i option:
$ ls -il myfile.txt
9059 -rw-r--r-- 1 kumar metal 51813 Jan 31 11:15 myfile
Where, 9059 is the inode number
Hard Links
- A Unix file may have multiple names
- A hard link is a reference to an existing file, not a copy of a file
- A hard link is used to provide alternate means of referencing a file
-rw-rw-r- - 1 guest other 72 sep 12 09:59 emp.lst
- Link count corresponds to number of files that reference the original file.
This count is normally 1 when the file is created
- To see the hard link count of a file, use the ls -l command, the second
column really is the number of hard links:
Link count
$ ls –l emp.lst
5. ln – creating hard links
- A file is linked with the ln command which takes two filenames as
arguments
Syntax: ln [options] existing_filename new_filename
Example: link emp.lst with employee
$ ln emp.lst employee
- The –i option to ls shows that they have the same inode number, meaning
that they are actually one and the same file:
$ ls –li emp.lst employee
29518 -rwxr-xr-x 2 kumar metal 915 may 4 09:58 emp.lst
29518 -rwxr-xr-x 2 kumar metal 915 may 4 09:58 employee
- The link count, which is normally one for unlinked files, is shown to be two
6. umask
- Umask value is used to set the default permission of a file and
directory while creating
- umask command is used to see the default mask for the file or
directory
- The default permission set for the file and directory upon their
creation is:
rw-rw-rw- (octal 666) for regular files
rwxrwxrwx (octal 777) for directories
- The default value is transformed by subtracting umask value from it to remove
one or more permission
- The computation that takes place during their permission
assignment is shown below
666 777
-022 -022
644 755
- To display the current umask value, use the umask command
$ umask
022
rw-r- -r- - (644) for ordinary files
rwxr-xr-x (755) for directories
7. - find command recursively examines a directory tree to look for files matching some
criteria, and then takes some action on the selected files.
find : locating files
Syntax:
find path_list selecton_criteria action
- It recursively examines all files specified in path_list
- It then matches each file for one or more selection-criteria
- Finally It takes some action on those selected files
Working of find command
Options for matching criteria
-inum n Selects file having inode no n
-name flname The name of the file for search
-type x What type of file to search for
f- ordinary file
d- directory file
-perm p Find files based permission p
8. -size n Find files based on size
+n – match files larger than n bytes
-n - match files smaller than n bytes
-user usr Find files owned by user usr
options for action component
-print Prints selected file on standard output
-exec cmd Execute command cmd on a file
-ls Execute ls command on selected files
-mtime x Find files which are based on modification time
+x – modified morethan x days ago
-x – modified less than x days ago
Example 1: Search for files in whole file system with a.out
$ find / -name a.out -print
9. Example 2: Find and report all files with extension .c in the current
working directory
$ find . -name “*.c” -print
Exercises:
1. Find and report all files begin with an upper case letter
2. Use find command to locate from your current directory the
following
i) all files with extension .html or .HTML
ii) files modified yesterday
3. Use find command to locate the following
i) all symbolic link files with permission 666 in the parent directory
ii) all files with number of links equal to 5
iii) display the content of the file name example.c in the home
directory
Hinweis der Redaktion
A file is a collection of data items stored on disk. A UNIX file system is a collection of files and directories stored on disk in a hierarchical structure. All other files and directories on the system, regardless of what physical storage device they may reside on, are located below the root The root directory is represented by the / (forward slash) character.Each file system is
stored in a separate whole disk partition.
An Inode number points to an Inode. An Inode is a data structure that stores the following information about a file :
Please note that, neither the name of the file nor the inode number is stored in the inode
Unix divided physical disks into logical disks called partitions. Each partition is a standalone file system
"ls -i" lists the inode of a file
The "-i" option lists the inode number before the filename
A separate link exists to the same file contents
When you execute the command ls -l in UNIX, you get detailed information about files: permissions, file size, date of last modification, etc. A while ago, I got a question from one of my students who wondered what the second column meant. According to the documentation, it’s the “number of hard links,” but what does that actually mean? the number of hard links.
file with a link count of only 1 has no (other) hard links. A file with more than 1 link has other name(s)
After the above command, user may refer to the same file either with emp.lst or employee
T On a Linux system, each file and directory is assigned access rights for the owner of the file, the members of a group of related users, and everybodyo display the current umask value, use the umask command
The find command is used to locate files on a unix system. Find will search ant set directories you specify for files that match the suppied criteria.
search for files in a directory hierarchy. the search can be based on different criteria, and the matching files can be run through defined actions
To find all the files whose name is a.out in a current working directory.:
Path_list: directory to start searching
Criteria: criteria for the actual search
Action: action you want to take on the files found
-name flname: searches for a certain filename flname
-type x: searches for files of type x where x is
-user usr : searches for files owned by user usr