How to Find Out File or Directory Owner and Group Names in Linux

This post will guide you how to find the group name of a given file or direcotry using stat command in your Linux operating system. How do I find File or directory Owner name and group name from the command line use a command under CentOS or RHEL Linux system.

Find Out File or Directory Owner and Group Names in Linux1

Stat Command


Stat command can be used to display file or file system status. Mandatory arguments to long options are mandatory for short options too.

The syntax of the Stat command is as follows:

stat [OPTION]... FILE...

Option:

-c --format=FORMAT use the specified FORMAT instead of the default; output a newline after each use of FORMAT

List File or Directory Owner and Group Name Using Ls Command


If you want to find the owner and group names for a given file or directory in your Linux terminal, and you can use the ls command with “-ld” options.

For example, you want to find owner and group name of a directory called /tmp, and type:

$ ls -ld /tmp

Outputs:

[devops@mydevops ~]$ ls -ld /tmp
drwxrwxrwt. 16 root root 4096 Oct 15 09:35 /tmp

From the above outputs, and you would see that the owner name of tmp direcotry is root. and the group name of tmp direcotry is also root.

List File or Directory Owner and Group Name Using Stat Command


You can also use another command called stat to find out owner and group name of a file, you just need to run stat command followed file name, type:

$ stat /tmp

or

$ stat -c "Owner is:%U Group name is: %G" /tmp

Outputs:

[devops@mydevops ~]$ stat /tmp
File: /tmp
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd00h/64768d Inode: 101027767 Links: 16
Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:tmp_t:s0
Access: 2019-05-10 20:33:06.000000000 -0400
Modify: 2019-10-15 09:35:17.152940027 -0400
Change: 2019-10-15 09:39:55.208097455 -0400
Birth: -

[devops@mydevops ~]$ stat -c "Owner is:%U Group name is: %G" /tmp
Owner is:root Group name is: root

if you want to get more help about stat command, and you can check its man page or help page with the following commands:

$ man stat
$ stat --help

Outputs:

[devops@mydevops ~]$ stat --help
Usage: stat [OPTION]... FILE...
Display file or file system status.

Mandatory arguments to long options are mandatory for short options too.
-L, --dereference follow links
-f, --file-system display file system status instead of file status
-c --format=FORMAT use the specified FORMAT instead of the default;
output a newline after each use of FORMAT
--printf=FORMAT like --format, but interpret backslash escapes,
and do not output a mandatory trailing newline;
if you want a newline, include \n in FORMAT
-t, --terse print the information in terse form
--help display this help and exit
--version output version information and exit

The valid format sequences for files (without --file-system):

%a access rights in octal (note '#' and '0' printf flags)
%A access rights in human readable form
%b number of blocks allocated (see %B)
%B the size in bytes of each block reported by %b
%C SELinux security context string
%d device number in decimal
%D device number in hex
%f raw mode in hex
%F file type
%g group ID of owner
%G group name of owner
%h number of hard links
%i inode number
%m mount point
%n file name
%N quoted file name with dereference if symbolic link
%o optimal I/O transfer size hint
%s total size, in bytes
%t major device type in hex, for character/block device special files
%T minor device type in hex, for character/block device special files
%u user ID of owner
%U user name of owner
%w time of file birth, human-readable; - if unknown
%W time of file birth, seconds since Epoch; 0 if unknown
%x time of last access, human-readable
%X time of last access, seconds since Epoch
%y time of last data modification, human-readable
%Y time of last data modification, seconds since Epoch
%z time of last status change, human-readable
%Z time of last status change, seconds since Epoch

Valid format sequences for file systems:

%a free blocks available to non-superuser
%b total data blocks in file system
%c total file nodes in file system
%d free file nodes in file system
%f free blocks in file system
%i file system ID in hex
%l maximum length of filenames
%n file name
%s block size (for faster transfers)
%S fundamental block size (for block counts)
%t file system type in hex
%T file system type in human readable form

--terse is equivalent to the following FORMAT:
%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C
--terse --file-system is equivalent to the following FORMAT:
%n %i %l %t %s %S %b %f %a %c %d

NOTE: your shell may have its own version of stat, which usually supersedes
the version described here. Please refer to your shell's documentation
for details about the options it supports.

Conclusion


You should know that how to get the owner and group names of a given file or direcotry using ls/stat commands in your CentOS/RHEL/Ubuntu Linux.

You might also like:

Sidebar



back to top