How to identify file type in Linux?

In this article we will see how to identify type of file in Linux or Solaris and understand the basic concept behind "how a linux system identifies a file"? 
 
	The most common command to identify the file type is "file". For example: this can be understood by following : 
 
	I have few files in my home directory and when i run "file" command followed by the file names then it shows the type of file it is: 
 
	  [root@saket ~]# file backup.sh test/ sakuu  
  
	backup.sh: Bourne-Again shell script text executable  
  
	test/:     directory  
  
	sakuu:     ASCII text  
  
	[root@saket ~]#   
  
	   
  
	Now another way is to simply do "ls -l" or long list.  
  
	   
  
	  
		[root@saket ~]# ls -lhd backup.sh test/ sakuu   
	  
		-rw-r--r-- 1 root root   20 Nov 15  2010 backup.sh  
	  
		-rw-r--r-- 1 root root   31 Feb 10  2011 sakuu  
	  
		drwxr-xr-x 3 root root 4.0K Jul  7 02:40 test/  
	  
		[root@saket ~]#   
	  
		Here directories show "d" as the first bit in permissions' column and in case of general files, it remains empty, for character/block files this shows "c" or "b". For example:  
	  
		   
	  
		  
			[root@saket ~]# ls -l /dev/hdc   
		  
			brw-rw---- 1 root disk 22, 0 Jul 12 03:36 /dev/hdc  
		  
			[root@saket ~]#   
		  
			   
		  
			In the above example:- "b" shows that this is a blobk file and as we know this is a hard disk.  
	  
  
 
	Now a very simple question "this is how our system shows the file type, but how does our system came to know the type of file", as we don't have any extension .txt .doc restriction in linux. 
 
	The answer is very simple:- Linux reads first and last 8 bytes of the file which is also known as file header and based on its header it displays the file type. For example:-  an image file in png format always have first 8 bytes "136 80 78 71 13 ...." like that and the last 8 bytes are also fixed. 
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments