Linux-Filesystem TIPS

I discussed about inode , sym/soft link, hard link etc in last blog post. In this article, I will look into other useful commands/methods like stat command, dev files, etc. Please note that inode contains information such as owner,mode/permission, number of hard links, size,time(except file name) etc for a file. So basically inode keeps the physical information for a file.

Are you getting confused with the file metadata using ls -il  command ?

Here is the solution to use stat command

sanjay@sanjay-VirtualBox:~/linux_day1$ stat myfile
File: 'myfile'
Size: 31 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 2253687 Links: 2
Access: (0664/-rw-rw-r--) Uid: ( 1000/ sanjay) Gid: ( 1000/ sanjay)
Access: 2018-09-27 11:34:52.005594304 +0530
Modify: 2018-09-27 11:34:52.005594304 +0530
Change: 2018-09-27 11:35:19.613691426 +0530
Birth: -

By default, 8 blocks will be allocated for regular file with block size 512 bytes.

Experiment: Just write 4096 bytes in a file using vi editor and check number of blocks, Then add extra character to observe the change.

There are many file systems based on disk partition supported in different operating systems e.g. ext2,ext3,ext4,NTFS,FAT32,extFAT etc . One can use mount command to find the same

sanjay@sanjay-VirtualBox:~/linux_day2$ mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs 
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)

/dev Files

As we observed that files take some space on hard-disk and follow a partition format type . How about /dev files ?

If I use df (disk free) command it gives me

sanjay@sanjay-VirtualBox:~/linux_day2$ df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 1998980 0 1998980 0% /dev
tmpfs 404424 6136 398288 2% /run
/dev/sda1 149003596 61338164 80073448 44% /

However if I use stat command I get 0 size.

sanjay@sanjay-VirtualBox:~/linux_day2$ stat /dev/sda1
File: '/dev/sda1'
Size: 0 Blocks: 0 IO Block: 4096 block special file
Device: 6h/6d Inode: 427 Links: 1 Device type: 8,1

This is because /dev/sda1 is not a regular file, It is a block special file and will be marked as b in ls command

sanjay@sanjay-VirtualBox:~/linux_day2$ ls -il /dev/sda1

427 brw-rw—- 1 root disk 8, 1 Sep 27 10:51 /dev/sda1

How many file types exist ?

  • Regular File      ‘-‘
  • Directory file    ‘d’
  • Symbolic link file ‘l’  :  Symbolic or Soft link
  • Block file             ‘b’  : Provides bufferred access to hardware
  • Character device file ‘c’ : Provides un-buffered direct access to hardware
  • Pipe Files ‘p’ : File to transfer information from one command/application/process to other for further process
  • Socket file ‘s’ : Files to transfer  information between applications

Is there any other kind of device files exist ?

Yes, That is called Pseudo-device.

  • /dev/null : null is a special device which discards the data written to it
  • /dev/zero : It provides endless stream of null(0x00) characters.
  • /dev/random or /dev/urandom used for random number generation. Can be used in Cryptography
  • / dev/full : To test the application behavior on disc full
    • sanjay@sanjay-VirtualBox:~/linux_day2$ echo “test” >> /dev/fullbash: echo: write error: No space left on device

Redirection:    There are three standard or default data streams associated with each command/program.

  • STDIN
  • STDOUT
  • STDERR

This discussed at Redirection

This article is discussed at Linux File System Video

Thanks for reading till end. I am trying to improve usability of my  site. Did you find this discussion helpful ? If so,  Please subscribe to YouTube channel Embedkari as well for additional embedded related stuff.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.