How to Get File Owner
Quick Answer: Get File Owner in Bash
To get the file owner, use stat -c %U file.txt to get the username or stat -c %u file.txt to get the numeric user ID. For owner and group together, use stat -c "%U:%G" file.txt. The stat command is the most reliable for scripting.
Quick Comparison: File Owner Checking Methods
| Method | Format | Best For | Speed | Reliability |
|---|---|---|---|---|
| stat -c %U | Username | Scripts | Fast | Very good |
| stat -c %u | Numeric UID | Numeric checks | Fast | Very good |
| ls -l | Both (visual) | Human reading | Fast | Good |
| Test operator -O | Boolean | Simple checks | Fastest | Good |
| id -u | Current user UID | Comparing users | Medium | Good |
Bottom line: Use stat -c %U for username; use -O test operator for “do I own this?” checks.
Getting File Ownership
Every file on a Unix/Linux system has an owner (user) and a group. Understanding how to check and work with ownership is important for permissions and security. You can use ls, stat, or special test operators in Bash.
Method 1: Get Owner with stat (Recommended)
The most reliable way to extract owner information for scripts:
#!/bin/bash
file="document.txt"
# Get owner using stat (username)
owner=$(stat -c %U "$file")
echo "Owner: $owner"
# Get owner UID (numeric)
owner_uid=$(stat -c %u "$file")
echo "Owner UID: $owner_uid"
# Get owner and group together
owner_group=$(stat -c "%U:%G" "$file")
echo "Owner:Group: $owner_group"
When to Use stat
Use stat when:
- You need the owner’s username for logging or display
- You’re comparing owners in scripts
- You need both owner and group information
- You want reliable, consistent output
Method 2: Checking Owner with ls
The traditional way to see ownership visually:
$ ls -l file.txt
-rw-r--r-- 1 john staff 1234 Feb 21 14:30 file.txt
# ^ ^^^^^^^^^^
# link count, owner (john), group (staff)
Get owner using ls
owner=$(ls -l “$file” | awk ‘{print $3}’) echo “Owner: $owner”
Get both owner and group
stat -c “%U:%G” “$file”
Output: john:staff
## Using stat for Complete Information
The `stat` command provides comprehensive ownership details:
```bash
#!/bin/bash
file="/home/user/documents/file.txt"
# Get all ownership information
stat "$file"
# Get specific format
echo "Owner: $(stat -c %U "$file")"
echo "Group: $(stat -c %G "$file")"
echo "UID: $(stat -c %u "$file")"
echo "GID: $(stat -c %g "$file")"
Checking Owner Using Test Operators
#!/bin/bash
file="/tmp/test.txt"
# Check if file is owned by current user
if [ -O "$file" ]; then
echo "You own this file"
else
echo "You do not own this file"
fi
# Check if file belongs to your group
if [ -G "$file" ]; then
echo "This file is in your group"
fi
Real-World Example: Ownership Audit
#!/bin/bash
audit_ownership() {
local dir="$1"
echo "Ownership Audit for: $dir"
echo "============================================"
for file in "$dir"/*; do
[ -e "$file" ] || continue
owner=$(stat -c %U "$file")
group=$(stat -c %G "$file")
type=$(stat -c %F "$file")
printf "%-30s %-12s %-12s %s\n" \
"$(basename "$file")" \
"$owner" "$group" "$type"
done
}
audit_ownership "/tmp"
Finding Files by Owner
#!/bin/bash
# Find all files owned by a specific user
find_by_owner() {
local owner="$1"
local dir="${2:-.}"
echo "Files owned by $owner in $dir:"
find "$dir" -user "$owner" -type f 2>/dev/null
}
# Find files NOT owned by a user
find_not_owned_by() {
local owner="$1"
local dir="${2:-.}"
echo "Files NOT owned by $owner:"
find "$dir" ! -user "$owner" -type f 2>/dev/null
}
find_by_owner "john"
find_not_owned_by "root"
Checking Root-Owned Files
#!/bin/bash
check_root_ownership() {
local file="$1"
local owner=$(stat -c %U "$file")
if [ "$owner" = "root" ]; then
echo "File is owned by root"
return 0
else
echo "File is owned by: $owner"
return 1
fi
}
check_root_ownership "/etc/passwd"
check_root_ownership "/tmp/file.txt"
Practical Example: Permission Report
#!/bin/bash
generate_permission_report() {
local dir="$1"
echo "System Report for: $dir"
echo "==========================================="
for file in "$dir"/*; do
[ -e "$file" ] || continue
owner=$(stat -c %U "$file")
group=$(stat -c %G "$file")
perms=$(stat -c %a "$file")
size=$(stat -c %s "$file")
printf "%s\n" "File: $(basename "$file")"
printf " Owner: %s, Group: %s\n" "$owner" "$group"
printf " Permissions: %s\n" "$perms"
printf " Size: %s bytes\n" "$size"
echo ""
done
}
generate_permission_report "/home"
Changing Ownership
When you need to change owner (usually requires root):
#!/bin/bash
# Change owner
chown newowner file.txt
# Change owner and group
chown newowner:newgroup file.txt
# Change only group
chown :newgroup file.txt
# Recursive change
chown -R owner:group directory/
Verifying Ownership Changes
#!/bin/bash
verify_ownership() {
local file="$1"
local expected_owner="$2"
local actual_owner=$(stat -c %U "$file")
if [ "$actual_owner" = "$expected_owner" ]; then
echo "✓ Ownership verified: $actual_owner"
return 0
else
echo "✗ Ownership mismatch"
echo " Expected: $expected_owner"
echo " Actual: $actual_owner"
return 1
fi
}
verify_ownership "/tmp/myfile.txt" "$USER"
Real-World Example: Secure Setup Script
#!/bin/bash
setup_secure_directories() {
local app_user="myapp"
local app_dir="/opt/myapp"
# Create user if needed
id "$app_user" &>/dev/null || useradd "$app_user"
# Create directories
mkdir -p "$app_dir"/{bin,lib,data,logs}
# Set ownership
chown -R "$app_user:$app_user" "$app_dir"
# Set permissions
chmod 755 "$app_dir"
chmod 700 "$app_dir/data"
chmod 700 "$app_dir/logs"
# Verify
echo "Verification:"
ls -l "$app_dir" | tail -n +2 | awk '{printf "%s owned by %s:%s\n", $9, $3, $4}'
}
setup_secure_directories
Finding Suspicious Ownership
#!/bin/bash
# Find files in home directory not owned by user
find_suspicious() {
local user="$1"
local home="/home/$user"
echo "Files in $home not owned by $user:"
find "$home" ! -user "$user" -type f 2>/dev/null
echo ""
echo "Suspicious files (owned by root in user home):"
find "$home" -user root -type f 2>/dev/null
}
find_suspicious "john"
Important Notes
-Otest checks if you own the file-Gtest checks if file’s group matches yoursstat -c %Ugets the owner namestat -c %ugets the numeric UID- Only root can change ownership with
chown - The
findcommand’s-useroption searches by owner
Quick Reference
# Get owner
stat -c %U file.txt
# Get owner and group
stat -c "%U:%G" file.txt
# Get numeric UID and GID
stat -c "%u:%g" file.txt
# Check if you own the file
[ -O file.txt ] && echo "You own it"
# Check if in file's group
[ -G file.txt ] && echo "You're in the group"
# Find files by owner
find . -user username
# Change owner
chown newowner file.txt
# Change owner and group
chown newowner:newgroup file.txt
Summary
Use stat -c %U to get file ownership, or use the -O test operator to check if you own a file. Understanding ownership is critical for managing permissions and securing your systems.