Hello Everyone, this aim of writing this article is to get clarity on files in Linux and different parameters associated with it including permissions, ownership, size etc. So let's get started!
One of the first commands we learn when working with Linux is ls which stands for list. Since Linux is generally operated without a GUI, a simple click on directories to list its content is not possible, this is where ls commands helps us.
Typical output of a ls command is
[dsharma@CentOS7 ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
A few basic points before we move further:
- dsharma - username
- CentOS7 - hostname/linux machine name
- ~ - this symbol shows that we are in our user home directory
To view information about these directories or files comprehensively, we use a flag/option with ls command called -l. This gives the output in a log listing format.
[dsharma@CentOS7 ~]$ ls -l
total 0
drwxr-xr-x. 2 dsharma dsharma 6 Mar 28 17:32 Desktop
drwxr-xr-x. 3 dsharma dsharma 94 Mar 30 11:42 Documents
drwxr-xr-x. 2 dsharma dsharma 6 Mar 28 17:32 Downloads
drwxr-xr-x. 2 dsharma dsharma 6 Mar 28 17:32 Music
drwxr-xr-x. 2 dsharma dsharma 6 Mar 28 17:32 Pictures
drwxr-xr-x. 2 dsharma dsharma 6 Mar 28 17:32 Public
drwxr-xr-x. 2 dsharma dsharma 6 Mar 28 17:32 Templates
drwxr-xr-x. 2 dsharma dsharma 6 Mar 28 17:32 Videos
Now we are shown a bunch of information which can seem too much at once! But don't worry let's break it down piece by piece.
Each row has 6 different columns or entries that tells us a lot about the file/directory.
drwxr-xr-x: The first column tells us whether we are dealing with a file or a directory and what the permissions associated with it are. the "d" character at the very beginning tells us that it is a directory. If it would have been a file, we would have seen "-".
Now there are 9 characters after letter "d" which are basically seen in 3 groups
In the above diagram the characters r,w and x represent the following with the respective numeric value:
r-read(4), w-write(2), x-execute(1)
- u stands for permissions given to user who is also the owner of file
- g stands for group, and the permissions given to that group
- o stands for others, and the permissions with them. Others are basically everyone except the user and group
Therefore as per the above diagram, the user can do all the operations on the file, the group can read and write only whereas others can read and execute, not write.
Each permission (r,w,x) has a standard numeric value associated with it and we can sum up those numbers to define the permissions for user, group and others. Hence in numeric terms, we can also say that user has permission 7 (4+2+1), the group has permission 6 (4+2) and others have permission 5 (4+1).
The second column is a number which tells us if we have a file or directory with us and is there some directory within that directory. If yes, how many?
- 1 -> we are working with a regular file
- 2 -> we are working with a directory
- 3 -> we are working with a directory which has 1 more directory inside it
- 4 -> we are working with a directory which has 2 more directory inside it and so on...
Note: This number only increases when a directory is created inside another directory and not when more and more regular files (.txt, .yaml) are added.
The column three & four are the user and group names. They represent the username of person who owns the file and the group to which the file belongs. Generally when we create a user, a group with the same username is also created and the user by default belongs to that group. However, groups can be modified, created, deleted as well.
drwxr-xr-x. 3 dsharma dsharma 94 Mar 30 11:42 Documents
The above description shows that Documents Directory belongs to user dsharma and to group dsharma.
The column five shows the size of the file or directory (in bytes). This is the amount of space the directory/file is occupying
Finally the column six shows the month, date and time when the last modification took place. This can be useful to check if some files are old and not needed or files/directories which have not been looked at for a long time.
All these columns give a brief overview about the file, their permissions and other properties.
Now we know how to look at the files and the important information to gather after writing the ls -l command. With this out of our way, we can move forward with some other aspects... But that we shall cover in another blog.
Thanks for reading it this far! Enjoy and keep practicing!