0% found this document useful (0 votes)
75 views

Types of Linux File

Everything in Linux is treated as a file, including directories, devices, and pipes. There are different types of files like regular files, directories, block files, character devices, pipes, symbolic links, and sockets. Files are case sensitive and permissions control how users can interact with files through commands like chmod which set permissions using octal or symbolic modes. Inodes store metadata about files and are used to map file names to locations on disk, allowing hard links where multiple names can refer to the same file.

Uploaded by

gamehint812
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Types of Linux File

Everything in Linux is treated as a file, including directories, devices, and pipes. There are different types of files like regular files, directories, block files, character devices, pipes, symbolic links, and sockets. Files are case sensitive and permissions control how users can interact with files through commands like chmod which set permissions using octal or symbolic modes. Inodes store metadata about files and are used to map file names to locations on disk, allowing hard links where multiple names can refer to the same file.

Uploaded by

gamehint812
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Linux Files

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.

Files are always case sensitive. Let's understand it through an example.

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.

Directory files (d): It is shown in blue color. It contain list of files.

Special files

Block file (b)

Character device file (c)

Named pipe file (p)

Symbolic link file (l)

Socket file (s)

Linux File Commands

Command Description

file Determines file type.

touch Used to create a file.

rm To remove a file.

cp To copy a file.

mv To rename or to move a file.

rename To rename file.


File Permissions

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.

Permissions are listed below:

permission on a file on a directory

r (read) read file content (cat) read directory content (ls)

w (write) change file content (vi) create file in directory (touch)

x (execute) execute the file enter the directory (cd)

Look at the above snapshot, there are ten characters (-rw-rw-r--) before the user owner. We'll
describe these ten characters here.

File permissions for (-rw-rw-r--)

position characters ownership

1 - denotes file type

2-4 rw- permission for user

5-7 rw- permission for group

8-10 r-- permission for other


When you are the User owner, then the user owner permission applies to you. Other permissions
are not relevant to you.

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.

First letter (-) or d represents the files and directories respectively.

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 .

Setting Permissions With chmod

You can change the permissions with chmod command accordingly to your need. Below are some
examples to change the permissions for different groups.

To add permissions to a group.

Syntax:

chmod <groupName>+<permissionName> <fileName>

Setting Octal Permissions

Octal permissions can also be set for the groups.

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:

binary octal permissions

000 0 ---

001 1 --x
010 2 -w-

011 3 -wx

100 4 r--

101 5 r-x

110 6 rw-

111 7 rwx

From this we can conclude that,

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

The 'mkdir -m' command can be used to set the mode.

Syntax:

mkdir -m <mode> <fileName>

Example:

mkdir -m 777 new1

mkdir -m 000 new2

cp -p

The 'cp -p' command preserves the permissions and time stamps from source files.

Syntax:
cp -p <sourceFile> <destinationFile>

Advanced File Permission

Sticky Bit on Directory

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.

It is represented by a t (x is also there) or a T (no x is there).

Example:

chmod +t new1

permission for file new1 is changed to t at place of x permission for others.

setgid Bit on Directory

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).

Group permission is changed into s at the place of x permission for group.

setgid and setuid on Regular Files

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.

Linux File Links

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.

There are two types of links in Linux OS:

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.

2) Soft Links (Symbolic Links)

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

An Inode is a data structure containing metadata about the files.

Following contents are stored in the Inode from a file:

User ID of file

Group ID of file

Device ID

File size

Date of creation

Permission

Owner of the file

File protection flag

Link counter to determine number of hard links

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

Creating Hard Links

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.

Finding Hard Links

A hard link can be find with find command by specifying the Inode number. Inode number is always
unique to its partition.

Example:

find / -inum 662786 2> /dev/null

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

With rm command links can be removed.

You might also like