how to view all details or metadata of a file in linux command line

When viewing a particular file in Linux, you might want to see all the relevant file metadata associated with it. The file metadata details includes information regarding its size, permissions, creation date, access date, inode number, uid/gid, file type etc.

There are mainly two different commands that you can use for this purpose, ls and stat. Both will print out almost the same information but in different format.

ls Command

The most useful of the two commands is ls, (at least in my opinion) which lists the file details. Using some command line options you can print out all the details and metadata information of the particular file.

bash$ ls -lisan <filename>

The various command line options above prints out various information as detailed below

l : This uses the long listing format while printing out. This is much more informative than the default format.
i : Prints out the inode number of the file
s : Prints the file size in blocks
a : Prints out all entries and does not ignore any files
n : Prints out the numeric user id and group id
h : Print the sizes in human readable format.

The above command will print out all relevant metadata information about the file, but usually is not very human readable in its format. As long as you know what information is printed in each column it should work just fine.

If you prefer a much more human readable format, then you can use the stat command instead.

stat Command

The basic stat command works without any command line arguments, other than the file name…

bash$ stat <filename>

As an example,

[root 17:03:19] ~ # stat world_bkp
File: ‘world_bkp’
Size: 2434 Blocks: 8 IO Block: 4096 regular file
Device: 804h/2052d Inode: 262149 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-02-13 05:06:57.883217273 -0600
Modify: 2014-02-13 05:06:57.893217273 -0600
Change: 2014-02-13 05:06:57.893217273 -0600
Birth: -

This prints out almost the same information as the ls command, but prints it out in a much more human readable format. There is also a basic description for each value in the format.

Both the ls and stat commands can be used for any file descriptor, which means both the file as well as directories. stat can be used on file systems as well.

bash$ stat /dev/sda3


bash$ stat -f /

The -f command line option specifies to print out the file system status instead of the file status. You can see the difference by running the command with and without the -f option.

If you are using another command such as find or locate to print out files, then you can pipe (|) the output of that command to either ls or stat to print out more meaningful information. A simple example is

bash$ locate world_bkp | xargs stat

The above command will print out the file details exactly as before, but is useful if you didn't know the exact location of the world_bkp file. Another example of piping to ls command is

bash$ locate world_bkp | xargs ls -lisan

Again, as with most Linux commands, you can input multiple files in the command line to print the details of multiple files or use the pipe to output details of multiple files.

Image Files

There are in fact a couple of more commands that shows you specific information of a file depending on the type of the file. If you want to see what the file type is, then the file command can help you out.

bash$ file <filename>

Also in the case of image files, the identify command that is part of the imagemagick package is a very good option. The identify command will print out the image specific properties such as the format, colorspace, channel information etc.

bash$ identify -verbose myimage.jpg