Find the owner of a file in unix [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 y开发者_开发百科ears ago.
Improve this questionIs 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
).
ls -l | awk '{print $3, $4 }'
That'll do it
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" *
GNU find has the -printf option which will do this for you:
# if you want just the files in the directory, no recursion
find "$dir" -maxdepth 1 -type f -printf "%u %g\n"
# if you want all the files from here down
find "$dir" -type f -printf "%u %g\n"
# if you need the filename as well for disambiguation, stick a %f in there
find "$dir" -maxdepth 1 -type f -printf "%u %g %f\n"
Other systems might have this as gfind.
ls -l | cut -f3,4 -d" " | tail -n +2
精彩评论