Linux Commands

Linux Command: Namei Usage

Linux is a diverse platform to play with a lot of commands in its shell at one time and does other work as well. These commands are of diverse use and purpose. There comes a moment while working in Linux that you have to find out and know more about some specific file, its owner, its path, and content between some folders. One of those unique and great commands is the “namei” command of the Linux system. The namei command is used so far to know more about the specific directory i.e., its path, location, and a lot more things. Therefore, today we will be discussing the namei command in our Ubuntu 20.04 Linux shell. So, let’s have a new start.

You need to open up your system first and then open up the terminal console as well. Use the “Ctrl+Alt+T” for doing so. After the opening of the terminal, we are ready to utilize our command for specific purposes. You have to know that the namei command uses many flags in it for those reasons. If you want to find out more about the namei command, just write “namei” in the shell and you will see the commands info.

Simple Command:

The namei command can be used within the shell without the use of any flag in it to simply list the path values. For this, you need to define the path to the particular destination in the shell. You can see from the below image that the path has been divided into chunks. The “d” part shows all the directories of the particular path while the “-“ part shows the destination file.

01: Vertically Align the Path

Lets say, you want to vertically align the path of a particular file that has been found on your system’s home folder and you don’t know how to do that. So, you need the namei command here for your help. You have to use it with the “-v” flag for vertical alignment of its path from start to end. You have to give the path to this file in the command as well. After using this command by providing a path to the “new.txt” file, we have got the below result.

At the first line, the “f:” shows the path to the file is specified in front of it. “d” stands for directory i.e., means all the names in front of “d” are directories. The “-“ sign indicates that the name in front of it is your required file i.e. regular file. The “f” sign indicates that this path can only be directed towards some regular file.

$ namei –v /home/aqsayasin/new.txt

Let’s vertically align another file using the same namei command. So, we have updated the path given in the command this time i.e. different from the previous one. We have added a path to the file “read.cc” file found in the Downloads folder of our system. The “-v” flag is used to vertically align the path to this file in the shell. You will see that it will display the original path at the first line of output in front of “f”. After that, it will vertically align the list of all the directories. In last, it will show you the name of a file in front of the “-“ sign.

$ namei –v /home/aqsayasin/Downloads/read.cc

02: Find Owner and Group

The namei command is very useful to find out the real owner and group of the particular address given in it. That owner and group must be some Linux user that may or may not currently be logged in. So, we are using the name- command here with the “-o” flag in the shell to find out the owner and group of each part of the path given in the below instruction. Firstly, it will display the overall address in the output in front of “f” stating that it is directing towards some regular file. Now, this path contains a total of three directories and 1 regular file. The owner and group of the first two directories are the same i.e., “root” The owner of the next directory is user “aqsayasin” and it also lies in the “aqsayasin” group. The regular file also belongs to the user “aqsayasin” i.e., owner and group is “aqsayasin” here.

$ namei –o /home/aqsayasin/new.txt

Let’s find out the owner of another file “read.cc” in Ubuntu 20.04 system using the same namei command. We have utilized the “-o” flag with the file path. The output shows that the first two directories belong to the “root” user and group. The last 2 directories and the regular file “read.cc” belong to the currently logged-in user i.e., aqsayasin and you can make changes to these directories and files as well.

03: Long List Information

If you want to make your system, list all the other information regarding the specific path, you can also do that with the namei command in the shell. In other words, you can list out all the read, write, and execution rights of a certain path in the shell with the help of a namei command. For this purpose, you have to utilize the “-l” flag for listing the information within the namei command along with the path mentioned in it. Upon the execution of the namei command with the ”-l” command, we have got the result shown below in the image. The output of this command shows the context like “drwxr-xr-x” along with its owner and group information. The character “d” shows that the specific location of a directory. The first three “rwx” character means that the owner has all the rights to read, write and execute on this file. The “r_x” means the group and other users of this system have rights of reading and executing but not writing. These privileges are for all the three mentioned directories. While the file got only the read and write privileges for its owner and group i.e., “rw-rw-“, and other users can only read it. i.e., “i—“.

We have used the namei list command for another path to the file “read.cc”. The output for this command shows the same output as it did for an above path to the file “new.txt”. The directories contain the same privileges, owner, and group information. The file also contains the same owner, group, and privileges as the “new.txt” file got before.

If a user only wants to list the privileges assigned to owners, groups, and other users, they can also do that with the namei command. You only have to update the flag in the command. So, replace “-l” with the “-m” flag to do so. In the output, you can see that it displayed only the information regarding the privileges.

Conclusion:

Our guide contains a brief description of using the namei command in the shell of Ubuntu 20.04 system to achieve certain purposes like displaying the directories in vertical order, owners of files and directories, the groups to which these files and folders belong, the privileges assigned to those owners, groups, and users, etc. All the commands are quite easy to implement in any Linux distribution.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.