How do I check who owns a file in Linux

The chown command is used to change the ownership of a file, but it does not provide information on the current ownership. You can use the ls or stat commands to get this information. Here’s how you can do it.

Using ls to check ownership

1. First, open your terminal.

2. Then, use the cd command to change to the directory containing the file:

cd /path/to/directory

3. List the file with ls

ls -la filename

4. Replace filename with the name of your file.

  • This command will display relevant information, including hidden files, permissions, ownership, size, and timestamps.

4. Here’s an example output:

-rw-r--r-- 1 user group 1024 Mar 6 12:00 filename
  • File Type: The first character represents the type (- for regular files, d for directories, etc.).
  • Permissions: The next nine characters represent permissions for the owner, group, and others.
  • Owner: The owner (user) of the file.
  • Group: The group (group) of the file.

Using stat to get more information about ownership

1. Run the stat command:

stat filename

2. Interpret the output from stat.

Here’s an example output:

File: filename
Size: 1024       Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d   Inode: 1234567     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   user)   Gid: ( 1000/   group)
Access: 2024-03-06 12:00:00.000000000 +0000
Modify: 2024-03-06 12:00:00.000000000 +0000
Change: 2024-03-06 12:00:00.000000000 +0000
Birth: -
  • Uid: Shows the user ID of the owner, along with the username.
  • Gid: Shows the group ID and name.

Other commands you may find useful

  • ls -lh: This variant adds human-readable file sizes to your output.
  • getfacl: If ACLs are in use, this command can provide more information about ownership and permissions:
getfacl filename

Changing ownership with chown

If you need to change the ownership of a file, use the chown command with superuser privileges:

sudo chown newuser:newgroup filename

Replace newuser and newgroup with the desired owner and group.

Summary

Using ls -la, stat, or other relevant commands, you can easily find out who owns a file and what permissions it has in Linux. Whether you want to view or change it, these tools are useful for managing files in your system.