In Linux, every file and folder belongs to a user and a group. In this blog post, we will discuss three different ways to find the owner of a file or folder in Linux. Let’s get started!
The following Linux commands can be used to find file owner.
- ls -l howtouselinux # file owner is on the 3rd column
- stat howtouselinux # file owner is after uid
- stat -c “%U %G” howtouselinux
Table of Contents
Understanding the ownership of files in Linux
Linux file ownership is one of the most important concepts to understand when administrating a Linux system. Every file and folder on a Linux system has an owner and a group.
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!The owner is the user who created the file or folder, and the group is the group to which the owner belongs.
- User Owner (UID): Every file and directory is assigned a user owner, identified by a unique user ID (UID). The UID corresponds to a user account on the system.
- Group Owner (GID): Each file and directory is also associated with a group owner, identified by a unique group ID (GID). The GID corresponds to a group on the system.
File ownership is closely tied to file permissions. The owner, group members, and other users are assigned specific permissions (read, write, execute) for the file, which determine who can access and modify it.
File ownership is important for security. It allows you to control who can access your files.
For example, you can make sure that only you can access your personal files by setting the owner to yourself. You can also make sure that only members of a certain group can access a file by setting the group to that group.
Procedure to Find file owner in Linux
- Open the terminal.
- Type ls -l and press Enter. This will show a list of all the files in the current directory with their permissions, owner, and group.
- Type stat filename and press Enter. This will show detailed information about the file including the owner.
- Type find / -type f -user username and press Enter. This will find all the files on the system that are owned by the specified user.
- Type find / -type f -group groupname and press Enter. This will find all the files on the system that are owned by the specified group.
Find file owner with ls command in Linux
The most simple method to find file owner in Linux is using “ls -l” command. Open the terminal then type ls -l filename in the prompt. The 3rd column is the file owner.
The ls command should be available on any Linux system. It will list the file type, file permission, group, file size etc.
This command will show you all of the following information about a file or folder, including its owner.
- File type
- File permissions
- Hard links
- Ownership
- Group
- Size
- Date and time
To use this method, simply type “ls -l filename”. Replace “filename” with the name of the file or folder you are looking for.
For example, if I want to find the owner of a file called “testfile”, I would type “ls -l testfile”.
In the following example, the owner of testfile is howtouselinux.
% ls -l testfile
-rw-r--r-- 1 howtouselinux staff 0 3 3 21:04 testfile
check file owner with stat command in Linux
Another way to find the owner of a file or folder is by using the “stat” command. This command will show you even more information about a file or folder than ls -l.
stat is a command-line utility that displays detailed information about given files. stat command accepts one or more FILEs and it also includes a number of options that control the command behavior.
To use this method, simply type “stat filename”. For example, if I wanted to find the owner of a file called “testfile”, I would type stat testfile.
$ stat testfile
File: `testfile'
Size: 32 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 429064 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 1000/ howtouselinux) Gid: ( 1000/ staff)
Access: 2012-07-27 17:49:05.682143441 +0300
Modify: 2012-07-01 03:58:02.848540175 +0300
Change: 2012-08-01 21:12:57.129819212 +0300
From the output, we can see that the owner of this file is howtouselinux. The uid of this user is 1000.
get file owner with stat command + two options in Linux
The third way to find the owner of a file or folder is by using the “stat” command with %U %G options. It will only display the owner and group of this file.
The -c option in the stat command allows you to specify a custom format for the output. This option is followed by a format string that defines the desired output format.
stat -c "%U %G" testfile
howtouselinux staff
“%U %G”: The format string enclosed in quotes. It contains two format specifiers:
%U: Represents the user name of the file’s owner.
%G: Represents the group name of the file’s group owner.
The output of the command will be the user name followed by a space and then the group name of the “testfile”.
For example, if the file’s owner is “john” and the group owner is “users”, the command will output:
john users
How to change the owner of a file or folder in Linux?
You can use the chown command to change the owner of a file or folder in Linux.
To use this command, simply type “chown username filename”. Replace “username” with the name of the user you want to be the owner, and replace “filename” with the name of the file or folder you want to change.
For example, if I wanted howtouselinux to be the owner of a file called “testfile”, I would type “chown howtouselinux testfile”.
You can also use chown to change both the owner and group of a file or folder. To do this, simply type “chown username:groupname filename”.
Find all files owned by a certain user with Linux Find command
If you want to find all files owned by a certain user, use the find command.
To do this, simply type “find / -user username”. Replace “username” with the name of the user who owns the file or folder you are looking for.
For example, if I wanted to find the files whose owner is howtouselinux, I would type “find / -user howtouselinux”.
Linux commands for file owner and permission
Here are the Linux commands for file owner.
- You can use the ls command to list the owner of a file. For example, if you want to list the owner of the file foo.txt, you would type: ls -l foo.txt
- You can use the chown command to change the owner of a file. For example, if you want to change the owner of the file foo.txt to the user bar, you would type: chown bar foo.txt
- You can use the ls command to list the group of a file. For example, if you want to list the group of the file foo.txt, you would type: ls -l foo.txt
- You can use the chgrp command to change the group of a file. For example, if you want to change the group of the file foo.txt to the group bar, you would type: chgrp bar foo.txt
Here are the Linux commands for file permission.
- You can use the ls command to list the permissions of a file. For example, if you want to list the permissions of the file foo.txt, you would type: ls -l foo.txt
- You can use the chmod command to change the permissions of a file. For example, if you want to make the file foo.txt readable and writable by everyone, you would type: chmod 666 foo.txt
- You can use the ls command to list the modification time of a file. For example, if you want to list the modification time of the file foo.txt, you would type: ls -l foo.txt
- You can use the touch command to change the modification time of a file. For example, if you want to change the modification time of the file foo.txt to the current time, you would type: touch foo.txt
And that’s it! These are three different ways to find out who owns a file or folder in Linux. Do you have any other tips or tricks? Share them with us in the comments below! And don’t forget to check out our other blog posts for more Linux tips and tricks. Thanks for reading!