Files types in linux

How to Find Out File Types in Linux

In Linux, everything is considered as a file. In UNIX, seven standard file types are regular, directory, symbolic link, FIFO special, block special, character special, and socket. In Linux/UNIX, we have to deal with different file types to manage them efficiently.

In Linux/UNIX, Files are mainly categorized into 3 parts:

The easiest way to find out file type in any operating system is by looking at its extension such as .txt, .sh, .py, etc. If the file doesn’t have an extension then in Linux we can use file utility. In this article, we will demonstrate file command examples to determine a file type in Linux.

To find out file types we can use the file command.

Syntax: file [OPTION…] [FILE…]

You can run the following command to verify the version of the file utility:

We can test a file type by typing the following command:

We can pass a list of files in one file and we can specify using the -f option as shown below:

cat file.txt file -f file.txt

Using the -s option we can read the block or character special file.

Using -b option will not prepend filenames to output lines

Using -F option will use string as separator instead of “:”.

Using -L option will follow symlinks (default if POSIXLY_CORRECT is set):

We can use the –extension option to print a slash-separated list of valid extensions for the file type found.

For more information and usage options, you can use the following command:

We can also use ls command to determine a type of file.

The following table shows the types of files in Linux and what will be output using ls and file command

File Type Command to create the File Located in The file type using “ls -l” is denoted using FILE command output
Regular FIle touch Any directory/Folder PNG Image data, ASCII Text, RAR archive data, etc
Directory File mkdir It is a directory d Directory
Block Files fdisk /dev b Block special
Character Files mknod /dev c Character special
Pipe Files mkfifo /dev p FIFO
Symbol Link Files ln /dev l Symbol link to
Socket Files socket() system call /dev s Socket

Types of File and Explanation

Regular Files

Regular files are ordinary files on a system that contains programs, texts, or data. It is used to store information such as text, or images. These files are located in a directory/folder. Regular files contain all readable files such as text files, Docx files, programming files, etc, Binary files, image files such as JPG, PNG, SVG, etc, compressed files such as ZIP, RAR, etc.

Or we can use the “file *” command to find out the file type

Directory Files

The sole job of directory files is to store the other regular files, directory files, and special files and their related information. This type of file will be denoted in blue color with links greater than or equal to 2. A directory file contains an entry for every file and sub-directory that it houses. If we have 10 files in a directory, we will have 10 entries in the directory file. We can navigate between directories using the cd command

We can find out directory file by using the following command:

We can also use the file * command

Special Files

1. Block Files:

Block files act as a direct interface to block devices hence they are also called block devices. A block device is any device that performs data Input and Output operations in units of blocks. These files are hardware files and most of them are present in /dev.

We can find out block file by using the following command:

We can use the file command also:

2. Character device files:

A character file is a hardware file that reads/writes data in character by character in a file. These files provide a serial stream of input or output and provide direct access to hardware devices. The terminal, serial ports, etc are examples of this type of file.

We can find out character device files by:

We can use the file command to find out the type of file:

3. Pipe Files:

The other name of pipe is a “named” pipe, which is sometimes called a FIFO. FIFO stands for “First In, First Out” and refers to the property that the order of bytes going in is the same coming out. The “name” of a named pipe is actually a file name within the file system. This file sends data from one process to another so that the receiving process reads the data first-in-first-out manner.

We can find out pipe file by using the following command:

We can use the file command to find out file type:

A symbol link file is a type of file in Linux which points to another file or a folder on your device. Symbol link files are also called Symlink and are similar to shortcuts in Windows.

We can find out Symbol link file by using the following command:

We can use the file command to find out file type:

5. Socket Files:

A socket is a special file that is used to pass information between applications and enables the communication between two processes. We can create a socket file using the socket() system call. A socket file is located in /dev of the root folder or you can use the find / -type s command to find socket files.

We can find out Symbol link file by using the following command:

We can use the file command to find out file type:

Источник

File types In Linux/Unix explained in detail.

The Linux Juggernaut

«How many types of files are there in Linux/Unix and what are they?» This is a common question to every person who starts to learn Linux. O.K, why is it that much important to know file types?

Answer: This is because Linux considers every thing as a file. When ever you start working on Linux/Unix box you have to deal with different file types to effectively manage them.

How many types of file are there in Linux/Unix?

By default Unix have only 3 types of files. They are..

  1. Regular files
  2. Directory files
  3. Special files(This category is having 5 sub types in it.)

So in practical we have total 7 types(1+1+5) of files in Linux/Unix. And in Solaris we have 8 types. And you can see the file type indication at leftmost part of “ls -l” command.

Here are those files type.

Regular file(-) 
Directory files(d) 
Special files 

For your information there is one more file type called door file(D) which is present in Sun Solaris as mention earlier. A door is a special file for inter-process communication between a client and server (so total 8 types in Unix machines). We will learn about different types of files as below sequence for every file type.

Definition and information of the file type 
How to create particular file type
How to list/see particular file type

Regular file type Explained in Linux

These are the files which are indicated with «-» in ls -l command output at the starting of the line. And these files are.

1. Readable file or 
2. A binary file or
3. Image files or
4. Compressed files etc.

How to create regular files in Linux/Unix?
Ans: Use touch/vi command and redirection operators etc.

How can we list regular files?

ls -l | grep ^- 

Example listing of regular files :

-rw-r--r-- 1 krishna krishna 20986522 2010-01-31 13:48 test.wmv 
-rw-r--r-- 1 krishna krishna 173448 2010-01-30 21:20 Transformers-Teaser-Wallpaper-310.jpg
-r-xr-xr-x 1 root root 135168 2009-12-12 19:14 VIDEO_TS.VOB
-rw-r--r-- 1 krishna krishna 2113536 2009-12-01 13:32 Aditya 365 – Janavule.mp3
-rwxrwxrwx 1 root root 168 2010-02-14 14:12 xyz.sh

Directory file type explained in Linux/Unix

These type of files contains regular files/folders/special files stored on a physical device. And this type of files will be in blue in color with link greater than or equal 2.

How can we list them in my present working directory? 
ls -l | grep ^d

Example listing of directories.

drwxr-xr-x 2 surendra surendra 4096 2010-01-19 18:37 bin 
drwxr-xr-x 5 surendra surendra 4096 2010-02-15 18:46 Desktop
drwxr-xr-x 2 surendra surendra 4096 2010-01-18 14:36 Documents
drwxr-xr-x 2 surendra surendra 4096 2010-02-13 17:45 Downloads

How to create them?
Ans : Use mkdir command

Block file type in Linux

These files are hardware files most of them are present in /dev.

How to create them?
Ans : Use fdisk command or create virtual partition.

How can we list them in my present working directory?

ls -l | grep ^b

Example listing of Block files(for you to see these file, they are located in /dev).

brw-rw---- 1 root disk 8, 1 2010-02-15 09:35 sda1
brw-rw---- 1 root disk 8, 2 2010-02-15 09:35 sda2
brw-rw---- 1 root disk 8, 5 2010-02-15 09:35 sda5 

Character device files in Linux

Provides a serial stream of input or output.Your terminals are classic example for this type of files.

How can we list character files in my present working directory?

ls -l | grep ^c

Example listing of character files(located in /dev)

crw-rw-rw- 1 root tty 5, 0 2010-02-15 16:52 tty 
crw--w---- 1 root root 4, 0 2010-02-15 09:35 tty0
crw------- 1 root root 4, 1 2010-02-15 09:35 tty1

Pipe files in Linux/Unix

The other name of pipe is a “named” pipe, which is sometimes called a FIFO. FIFO stands for “First In, First Out” and refers to the property that the order of bytes going in is the same coming out. The “name” of a named pipe is actually a file name within the file system.

How to create them?
Ans: Use mkfifo command.

How can we list character files in my present working directory?

ls -l | grep ^p

Example listing of pipe files

prw-r----- 1 root root 0 2010-02-15 09:35 /dev/.initramfs/usplash_outfifo 
prw-r----- 1 root root 0 2010-02-15 09:35 /dev/.initramfs/usplash_fifo
prw------- 1 syslog syslog 0 2010-02-15 15:38 /var/run/rsyslog/kmsg

These are linked files to other files. They are either Directory/Regular File. The inode number for this file and it’s parent files are same. There are two types of link files available in Linux/Unix ie soft and hard link.

How to create them?
Ans : use ln command

How can we list linked files in my present working directory?

ls -l | grep ^l

Example listing of linked files 
lrwxrwxrwx 1 root root 24 2010-02-15 09:35 sndstat -> /proc/asound/oss/sndstat
lrwxrwxrwx 1 root root 15 2010-02-15 09:35 stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 2010-02-15 09:35 stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 2010-02-15 09:35 stdout -> /proc/self/fd/1

Socket files in Linux

A socket file is used to pass information between applications for communication purpose

How to create them?
Ans : You can create a socket file using socket() system call available under

int sockfd = socket(AF_INET, SOCK_STREAM, 0);

You can refer to this socket file using the sockfd. This is same as the file descriptor, and you can use read(), write() system calls to read and write from the socket.

How can we list Socket files in my present working directory?

ls -l | grep ^s

Example listing of socket files.

srw-rw-rw- 1 root root 0 2010-02-15 09:35 /dev/log

srwxrwxrwx 1 root root 0 2010-02-15 10:07 /var/run/cups/cups.sock 
srwxrwxrwx 1 root root 0 2010-02-15 09:35 /var/run/samba/winbindd_privileged/pipe
srwxrwxrwx 1 mysql mysql 0 2010-02-15 09:35 /var/run/mysqld/mysqld.sock

A tip for you guys. How to find your desired type of file ?

Ans : Use find command with -type option. For example if you want to find socket file, just use below command. find / -type s If you want to find linked file then how? Find / -type l

Surendra Anne

Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.

Latest posts by Surendra Anne (see all)

  • Docker: How to copy files to/from docker container — June 30, 2020
  • Anisble: ERROR! unexpected parameter type in action: Fix — June 29, 2020
  • FREE: JOIN OUR DEVOPS TELEGRAM GROUPS — August 2, 2019
  • Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) — August 27, 2018
  • How to use ohai/chef-shell to get node attributes — July 19, 2018

Источник

Читайте также:  Linux логирование удаления файлов
Оцените статью
Adblock
detector