Linux namei Command Tutorial for Beginners (5 Examples)

On the Linux command line, you work with several types of files, for example, directories, symbolic links, and stuff like that. Sometimes, the requirement is to know more about individual elements in a path – what type of file it is, who is its owner, and more. Thankfully, there’s an inbuilt Linux command line utility – dubbed namei – that lets you fetch this information.

In this tutorial, we will discuss the basics of namei using some easy to understand examples. But before we start with that, it’s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux namei command

The namei command in Linux follows a pathname until a terminal point is found. Following is its syntax:

namei [options] pathname…

And here’s what the man page says about this tool:

namei  interprets  its  arguments as pathnames to any type of Unix file
       (symlinks, files, directories, and so forth).  namei then follows  each
       pathname  until  an  endpoint  is  found (a file, a directory, a device
       node, etc).  If it finds a symbolic link, it shows the link, and starts
       following it, indenting the output to show the context.

       This  program is useful for finding "too many levels of symbolic links"
       problems.

Following are some Q&A-styled examples that should give you a good idea on how the namei command works.

Q1. How to use namei?

Basic usage is fairly simple, all you have to do is to execute ‘namei’ followed by a command line path.

For example:

namei -v /home/himanshu/Downloads/HTF-review/Nodejs-Docker/1.png

And here’s the output this command produced:

f: /home/himanshu/Downloads/HTF-review/Nodejs-Docker/1.png
 d /
 d home
 d himanshu
 d Downloads
 d HTF-review
 d Nodejs-Docker
 – 1.png

The tool’s man page describes in detail how to interpret the output.

For each line of output, namei uses the following characters to identify the file type found:

          f: = the pathname currently being resolved
           d = directory
           l = symbolic link (both the link and its contents are output)
           s = socket
           b = block device
           c = character device
           p = FIFO (named pipe)
           - = regular file
           ? = an error of some kind

So you can see the namei command broke down all the elements in the path we supplied to it, informing us about their type.

Q2. How to vertically align namei output?

This you can do by using the -v command line option. For example:

namei -v /home/himanshu/Downloads/HTF-review/Nodejs-Docker/1.png

And here’s the output:

f: /home/himanshu/Downloads/HTF-review/Nodejs-Docker/1.png
d /
d home
d himanshu
d Downloads
d HTF-review
d Nodejs-Docker
- 1.png

If you compare this with the output shown in the previous section, you’ll see there’s a vertical alignment this time around.

Q3. How to make namei show owner and group information?

This can be done using the -o command line option. For example:

namei -o /home/himanshu/Downloads/HTF-review/Nodejs-Docker/1.png

Here’s the output:

f: /home/himanshu/Downloads/HTF-review/Nodejs-Docker/1.png
 d root     root     /
 d root     root     home
 d himanshu himanshu himanshu
 d himanshu himanshu Downloads
 d himanshu himanshu HTF-review
 d himanshu himanshu Nodejs-Docker
 - himanshu himanshu 1.png

So you can see that ownership information for each file/directory is displayed in the output.

Q4. How to make namei use long listing output format?

This can be done using the -l command line option.

namei -l /home/himanshu/Downloads/HTF-review/Nodejs-Docker/1.png

Here’s the output:

f: /home/himanshu/Downloads/HTF-review/Nodejs-Docker/1.png
drwxr-xr-x root     root     /
drwxr-xr-x root     root     home
drwxr-xr-x himanshu himanshu himanshu
drwxr-xr-x himanshu himanshu Downloads
drwxr-xr-x himanshu himanshu HTF-review
drwxr-xr-x himanshu himanshu Nodejs-Docker
-rw-rw-r-- himanshu himanshu 1.png

So you can see that an ls command like output is produced by the namei command.

As already explained in the beginning, the namei command follows a symbolic link by default. For example, on my system, ‘link1’ is a symbolic link to a file ‘file1’, so I passed ‘link1’ path as input to namei in the following way:

namei /home/himanshu/link1

Then the following output was produced:

f: /home/himanshu/link1
 d /
 d home
 d himanshu
 l link1 -> file1
   - file1

So you can see the namei command clearly showed the kind of file ‘file1’ is. However, if you want, you can force the tool to not follow symbolic links, something which you can do by using the -n command line option.

namei -n /home/himanshu/link1

Here’s the output in this case:

f: /home/himanshu/link1
 d /
 d home
 d himanshu
 l link1 -> file1

So you can see the tool didn’t follow symbolic link in this case.

Conclusion

The namei command is particularly useful in case of nested symbolic link elements in path. Here, in this tutorial, we have discussed majority of the command line options this tool offers. Once you’re done practicing these, head to the tool’s man page to know more about it.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu’s work has also been featured in some of world’s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

linux namei command tutorial for beginners 5
linux namei command tutorial for beginners 5 examples 1
linux namei command tutorial for beginners 5 examples 2
linux namei command tutorial for beginners 5 examples 3

نوشته های مشابه