Types of Linux File
Types of Linux File
In Linux system, everything is a file and if it is not a file, it is a process. A file doesn't include only text
files, images and compiled programs but also include partitions, hardware device drivers and
directories. Linux consider everything as as file.
Linux Files
In above example, we have two files named as 'Demo.txt' and 'demo.txt'. Although, they both share
the same name but still they are two different files.
Types of Files:
Regular files (-): It contain programs, executable files and text files.
Special files
Command Description
rm To remove a file.
cp To copy a file.
All the three owners (user owner, group, others) in the Linux system have three types of permissions
defined. Nine characters denotes the three types of permissions.
Read (r) : The read permission allows you to open and read the content of a file. But you can't do any
editing or modification in the file.
Write (w) : The write permission allows you to edit, remove or rename a file. For instance, if a file is
present in a directory, and write permission is set on the file but not on the directory, then you can
edit the content of the file but can't remove, or rename it.
Execute (x): In Unix type system, you can't run or execute a program unless execute permission is
set.But in Windows, there is no such permission available.
Look at the above snapshot, there are ten characters (-rw-rw-r--) before the user owner. We'll
describe these ten characters here.
When you are the Group then the group permission applies to you. Other permissions are not
relevant to you.
When you are the Other, then the other permission applies to you. User and group permissions are
not relevant to you.
Permission Example
Now we'll show some examples how permissions can be seen for a file or directory.
Look at the above snapshot, different directories and files have different permissions.
Now, from remaining nine letters, first triplet represents the permission for user owner. Second
triplet represents the permission for group owner. Third triplet represents the permission for other .
You can change the permissions with chmod command accordingly to your need. Below are some
examples to change the permissions for different groups.
Syntax:
For example, to set r octal will be 4, to set w octal will be 2, to set x octal will be 1.
Octal Table:
000 0 ---
001 1 --x
010 2 -w-
011 3 -wx
100 4 r--
101 5 r-x
110 6 rw-
111 7 rwx
777 = rwxrwxrwx
765 = rwxrw-r-x
654 = rw-r-xr--
umask
While creating a file or directory, by default a set of permissions are applied. These default
permissions are viewed by umask command.
For safety reasons all Unix systems doesn't provide execution permission to newly created files.
mkdir -m
Syntax:
Example:
cp -p
The 'cp -p' command preserves the permissions and time stamps from source files.
Syntax:
cp -p <sourceFile> <destinationFile>
Files can be protected in a directory from getting removed by other users who do not own it by
preventing it with sticky bit. It is displayed at the same location as the x permission for others.
Example:
chmod +t new1
To make sure all the files in the directories are owned by the group owner of directory, setgid can be
used. It is displayed on the same location as x permission for group. It is represented by a s (x is also
there) or a S (no x is there).
With the help of these two permissions, an executable file is accessed with the permissions of
the file owner instead of the executing owner. It means that if a program has root user and setuid
permission is set on it, then a user will run that program as root. This can be dangerous as well as
good for the security.
A Linux filesystem has many hard links and symbolic links. A link is a connectivity between the
filename and the actual data byte in the disk space. More than one filename can link to the same
data.
Hard Links
Soft Links
1) Hard Links
They are the low-level links. It links more than one filename with the same Inode and it represents
the physical location of a file.
When hard link is created for a file, it directly points to the Inode of the original file in the disk space,
which means no new Inode is created. Directories are not created using hard links and they can not
cross filesystem boundaries. When the source file is removed or moved, then hard links are not
affected.
Soft links are very common. It represents a virtual or abstract location of the file. It is just like the
shortcuts created in Windows. A soft link doesn't contain any information or content of the linked
file, instead it has a pointer to the location of the linked file. In other words, a new file is created
with new Inode, having a pointer to the Inode location of the original file.
It is used to create link between directories and can cross filesystem boundaries. When the source
file is removed or moved, then soft links are not updated.
Linux Inodes
An Inode number is a uniquely existing number for all the files in Linux and all Unix type systems.
When a file is created on a system, a file name and Inode number is assigned to it.
Generally, to access a file, a user uses the file name but internally file name is first mapped with
respective Inode number stored in a table.
Note: Inode doesn't contain the file name. Reason for this is to maintain hard-links for the files.
When all the other information is separated from the file name then only we can have various file
names pointing to the same Inode.
Inode Contents
User ID of file
Group ID of file
Device ID
File size
Date of creation
Permission
Example:
ls -ld new1
Inode Table
The Inode table contains all the Inodes and is created when file system is created. The df -
i command can be used to check how many inodes are free and left unused in the filesystem.
Inode Number
Each Inode has a unique number and Inode number can be seen with the help of ls -li command.
What is a Directory
A directory is a table which contains all its files Inode number and connect it to the file system.
Example:
ls -ali new1
Hard links for any file can be created with command ln. One extra hard link file will be created in the
respective directory
The original file and hard linked file both contain the same Inode number and hence, they have the
same permissions and same owners. Content will also be the same for both the files. In short, both
the files are equal now, but if original file will be removed then hard link file will not be affected.
A hard link can be find with find command by specifying the Inode number. Inode number is always
unique to its partition.
Example:
Symbolic Links
Symbolic links are also called soft links. Command ln -s is used to create soft link. It doesn't link to
Inodes but create a name to mapping. It create its own Inode number.
Example:
ln -s xyz symlink_to_xyz
Removing Links