Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the owner of a file in unix [closed]

Tags:

unix

Is there a way to get just the file's owner and group, separated by space in unix shell?

I'm trying to write a script to find the owner of all the files in a directory and print it (in a specific format, can't use ls -la).

like image 456
Dogbert Avatar asked Sep 07 '11 09:09

Dogbert


People also ask

Who is the owner of a file Linux?

User: A user is the one who created the file. By default, whosoever, creates the file becomes the owner of the file. A user can create, delete, or modify the file. Group: A group can contain multiple users.

How do I find out who owns a directory?

Run ls with the -l flag to show the owner and group-owner of files and directories in the current directory (or in a specific named directory).

How will you figure out the author of each file?

To see the Author property for a document or workbook, click File > Info, and then look for Author under Related People on the right.


2 Answers

ls -l | awk '{print $3, $4 }'

That'll do it

like image 86
CPJ Avatar answered Sep 27 '22 21:09

CPJ


Use the stat command, if available on your version of UNIX:

    $ stat -c "%U %G" /etc/passwd
    root root

or, to do this operation for all files in a directory and print the name of each file too:

    $ stat -c "%n %U %G" *
like image 35
Simon C Avatar answered Sep 27 '22 21:09

Simon C