How to Get List of all Owners of Files in a Linux Directory

This guide is intended to help you get a list of owners of all files in a directory on Linux. The commands shown here are universal and are expected to work on any Linux Distribution. My Lab though is based on Ubuntu 18.04,16.04 and CentOS 7.

Working with files, directories, and user permissions is a daily basis task when administering Linux systems. Almost everything on Linux is a file, and knowing how to check and change the ownership of a file/directory comes in handy.

Read also :

List owners of files in a directory using stat command

stat is a command line tool used to display file or file system status on Linux/Unix systems. On Ubuntu and all Debian based systems, stat command is provided by the coreutils package. If for any reason this package is not installed, you can install it using the command:

$ sudo apt-get -y install coreutils

On CentOS, the command is also provided by the coreutils package which can be installed using yum if missing:

$ sudo yum -y install coreutils

Once confirmed that the command exists, we'll use below options to get owners of files on a directory:

-c : To specify the format
%U : Format for printing username of the file owner

So command syntax is:

 stat -c %U  <file>

For all files use wildcard * mark. See examples below:

$ cd ~
$ stat -c %U *

The problem with this output is that there is no mapping between the file and owner. We'll need to use bash concepts to map file and owner. So we'll run a command like below:

$ cd /dir
$ files=`ls`
$ for file in $files; do owner=`stat -c %C $file`; echo $file $owner; done

The output for above will be in the format <file> <owner> which should be easy to interpret.

Development jmutai
Documents root dan
examples.desktop benard

For  other stat valid format sequences for files, check its man page:

$ man stat

List owners of files in a directory using find command

A find is a Linux command line tool often used to search for files in a directory. With some regex and print command, you can list the owners of files in a directory

The exact command syntax to use is:

# find /dir -printf '%u\n'

This will do a recursive search and listing by default. You can do further filtering like print unique usernames to avoid duplicates.  For this, pipe the output to sort command, with option -u

# find /dir -printf '%u\n' | sort -t: -u

To include file group ownership, add a %g option in the print function.

# find /dir -printf '%u:%g\n' | sort -t: -u

The example below will print all unique usernames owning files in the /var/ directory

See next example for usernames and groups:


Using ls command

You can also use ls command to check the owner of files and directories in Linux. The only problem is how to handle recursive ownership.

# ls -lh /dir 
# ls -lhR /dir

The ab0ve three commands should help you identify owners of files and directories in your Linux machines. If you have any other commands/script you feel we can update in this list, please don't hesitate to share.

Josphat Mutai 2:07 am

About Josphat Mutai

Red Hat Certified Engineer with more than 2 years experience managing critical Cloud Infrastructures and Services. Passionate about Technology and loves testing and reviewing gadgets and Open-source tools.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.