Of course. In Linux, you can show user and group IDs (UIDs and GIDs) instead of their names using the -n option with ls.
The Short Answer
Use the -n flag in combination with -l (long listing format):
bash
ls -ln
Detailed Explanation with Examples
The -n flag tells ls to display the numeric UID and GID instead of resolving them to their corresponding user and group names from files like /etc/passwd and /etc/group.
Standard ls -l (with names)
First, let's look at the standard output of ls -l. The third and fourth columns show the user and group names.
bash
$ touch myfile.txt
$ ls -l myfile.txt
-rw-r--r-- 1 alice developers 0 Nov 21 10:30 myfile.txt
# ^ ^
# | |
# Username Groupname
In this example, the user is alice and the group is developers.
Here, you can see that the username alice has been replaced by her User ID (UID) 1001, and the group name developers has been replaced by its Group ID (GID) 1005.
Combining with Other Common Flags
The -n flag works perfectly with other ls options you might use:
ls -lnh: Numeric IDs and human-readable file sizes (e.g., 4.0K, 1.2M).
ls -lna: Numeric IDs for all files, including hidden dotfiles.
ls -lnt: Numeric IDs, sorted by modification time (newest first).
Scripting: Numeric IDs are more reliable and consistent for scripts. Usernames can change or contain special characters, whereas UIDs are stable integers.
Troubleshooting Name Services: If your system uses a network-based name service (like LDAP or NIS) and it's slow or unavailable, a regular ls -l can hang while trying to look up names. Using ls -n bypasses this lookup, making the command fast and responsive.
Permissions on Different Systems: When moving files between systems (e.g., in a tar archive), the numeric UID/GID is what's preserved. The same UID might map to a different username on another machine. ls -n allows you to see the actual ownership ID that the filesystem stores.
Alternative for Scripting: stat
For scripting purposes, the stat command is often a much better choice than parsing the output of ls. It's designed to provide file metadata in a clean, customizable format.
To get just the UID and GID:
bash
# Get the numeric User ID (UID)
$ stat -c '%u' myfile.txt
1001
# Get the numeric Group ID (GID)
$ stat -c '%g' myfile.txt
1005
# Get both in a custom format
$ stat -c 'File: %n, UID: %u, GID: %g' myfile.txt
File: myfile.txt, UID: 1001, GID: 1005
This is more robust because you don't need to use tools like awk or cut to extract columns from ls output, which can be brittle.