
namei Command in Linux
The namei command in Linux is used to follow a pathname until a terminal point is found, such as a file, directory, or device. This command-line utility is especially useful for resolving symbolic links and identifying the final destination of a given path.
When it encounters a symbolic link, it shows the link and continues to follow it, indenting the output to show the context. This is especially helpful for debugging "too many levels of symbolic links" errors.
The namei command produces output for each component of the path it follows. It uses specific characters to identify the type of file it encounters at each step −
- f − Pathname being resolved
- d − Directory
- l − Symbolic link (shows both the link and its target)
- s − Socket
- b − Block device
- c − Character device
- - − Regular file
- ? − Error (indicates a problem)
By understanding these characters, you can easily identify what type of file or directory is at each part of the path. In addition, when the system has too many symbolic links, namei provides an informative message to inform you that the limit has been exceeded.
Table of Contents
Here is a comprehensive guide to the options available with the namei command −
Syntax of namei Command
The following is the general syntax for the namei command −
namei [options] pathname
namei Command Options
The following is a list of options that allow you to customize the output of the namei command to get detailed information about the files and directories along a given path −
Options | Description |
---|---|
-l, --long | Use the long listing format. Combines the effects of -m, -o, and -v to provide detailed information including mode bits, owners, and vertical alignment. |
-m, --modes | Show the mode bits of each file type in the style of ls(1), for example rwxr-xr-x. Displays the permissions of each file or directory in the path. |
-n, --nosymlinks | Dont follow symbolic links. Stops at symbolic links instead of following them to their targets. |
-o, --owners | Show owner and group name of each file. Displays the user and group that owns each file or directory. |
-v, --vertical | Vertically align the modes and owners. Aligns the mode bits and owner/group information vertically for better readability. |
-x, --mountpoints | Show mount point directories with a "D" rather than a "d". Identifies mount point directories with a capital "D" to distinguish them from regular directories. |
-Z, --context | Show security context of the file or "?" if not available. Displays the security context (e.g., SELinux context) of each file if supported. |
-h, --help | Display help text and exit. Provides a summary of command options and usage. |
-V, --version | Print version and exit. Displays the version of the namei command and exits. |
Examples of namei Command in Linux
In this section, we will demonstrate how the namei command can be used with different options to obtain detailed and specific information about pathnames and their components.
Resolving pathnames specified as the Argument Parameters
With the namei command, you can resolve multiple pathnames specified as argument parameters. This will help you understand how it navigates through symbolic links and identifies the actual files and directories.
To get started, we will create the following directory structure along with the files −
sudo mkdir -p /real/projects/example sudo touch /real/projects/example/file1.txt sudo mkdir -p projects/example/dir1 sudo touch projects/example/dir1/file1.txt sudo ln -s /real/projects/example projects/example/link1

Now, we will run the namei command to resolve the pathnames specified as arguments −
sudo namei projects/example/link1 projects/example/dir1 projects/example/dir1/file1.txt
This command resolves each pathname, following symbolic links and displaying the final destination.

Explanation of the Output −
- link1 − The command resolves the symbolic link link1, showing its target directory /real/projects/example.
- dir1 − The command resolves the directory dir1 within projects/example.
- txt − The command resolves the regular file file1.txt within projects/example/dir1.
Displaying the Output in a long-listing Format
To illustrate how to use the namei command with the long listing format ("-l" option), let"s walk through this example step-by-step. Suppose we have the following directory structure −
/home/user/symlink-to-file -> /real/path/Tutorialspoint/to/actual-file
In this example, symlink-to-file is a symbolic link pointing to the actual file located at /real/path/Tutorialspoint/to/actual-file.
Now, we will run the namei command with the "-l" option to get a detailed listing of the path.
namei -l /home/user/symlink-to-file

This output confirms that the symbolic link symlink-to-file is correctly pointing to /real/path/Tutorialspoint/to/actual-file and provides detailed information about the permissions and ownership of each directory and the file along the path.
Show Mode Bits Only
The modes option shows the mode bits of each file type. It displays the file permissions and the file type (directories are represented by "d", regular files by "-", and symbolic links by "l") for each resolved pathname. Suppose we have a directory structure with a file located at −
/var/www/html/index.html
Well run the namei command with the "-m" flag to display the mode bits (permissions) of each component in the path −
sudo namei -m /var/www/html/index.html
This command shows the mode bits (permissions) for each component in the specified path, from the root directory to the file index.html.

Do Not Follow Symbolic Links
In some situations, you might want to see where a symbolic link is pointing without actually following it to its final destination. The --nosymlinks or the "-n" option in the namei command is designed for this purpose. It stops the namei command from following the symbolic link and instead shows you the link itself.
Suppose we have a symbolic link /etc/alternatives/editor that points to /bin/nano. Normally, namei would follow this link and show you the target path.
However, with the --nosymlinks or the "-n" flag, namei stops at the editor and shows you that it's a symbolic link pointing to /bin/nano.
sudo namei -n /etc/alternatives/editor

In this output, the namei command stops at the symbolic link editor and does not follow it to /bin/nano. This can help you identify and troubleshoot symbolic links without resolving them to their final destination.
Show Owner and Group Names
Knowing the owner and group name of files can be quite valuable. The --owners or the "-o" option in the namei command is specifically designed for this purpose. It allows us to quickly identify the ownership details of each resolved pathname.
To display the owner and group names of each component in the path, you can use the following command −
sudo namei -o /var/www/html/index.html
This command provides a detailed view of the ownership of each directory and file in the /var/www/html/index.html, helping you understand the permissions and access controls in place.

Show Mount Point Directories with "D"
You can use the " -x" option with namei command to display mount point directories with a "D" instead of the usual "d". This helps to easily identify mount points along the specified path.
To show mount point directories with a "D", you can use the following command −
namei -x /mnt/data/myfile.txt

This output shows that /mnt is a mount point, making it easy to distinguish from other directories. The namei command with the "-x" option is particularly useful for identifying and understanding the structure of mount points in your filesystem.
Display Security Context
You can use the "-Z" option with namei command to display the security context (e.g., SELinux context) of each file or directory along the specified path. This information can be useful for managing and troubleshooting security policies.
To show the security context of a file or directory, you can use the following command −
namei -Z /home/user/secure-file
This output shows the security context for each component in the specified path, helping you understand and manage the security policies applied to your files and directories.

Conclusion
The namei command offers a powerful way to explore and understand the structure of files and directories. By exploring these options, you can tailor the output to suit specific needs, whether you are troubleshooting, managing permissions, or simply exploring file structures.