Find the proprietor of a directory site or documents, yet just return that and also absolutely nothing else

I am seeking a command that will return the proprietor of a directory site and also just that - - such as a regex analyzing the ls -lat command or something comparable? I intend to make use of the cause an additional manuscript.

2019-05-18 22:11:02
Source Share
Answers: 3

One can additionally do this with GNU locate:

find $directoryname -maxdepth 0 -printf '%u\n'

This isn't mobile beyond the GNU system, yet I would certainly be stunned to locate a Linux circulation where it does not function.

2019-05-21 07:57:59

stat from GNU coreutils can do this:

stat -c '%U' /path/of/file/or/directory

Unfortunately, there are a variety of variations of stat, and also there is not a great deal of uniformity in their syntax. As an example, on FreeBSD, it would certainly be

stat -f '%Su' /path/of/file/or/directory

If transportability is a problem, you are possibly much better off making use of Gilles's suggestion of incorporating ls and also awk. It needs to start 2 procedures as opposed to one, yet it has the benefit of making use of just POSIX - typical capability:

ls -ld /path/of/file/or/directory | awk '{print $3}'
2019-05-21 06:41:38

Parsing the output of , yet getting the first couple of areas is an exemption, it in fact works with all "typical" unices (it does not work with systems such as some Windows executions that permit rooms in customer names).

ls -ld /path/to/directory | awk 'NR==1 {print $3}'

Another alternative is to make use of a stat command, yet the trouble with stat from the covering is that there are numerous commands with various syntax, so stat in a shell script is unportable (also throughout Linux installments).

Keep in mind that screening whether an offered customer is the proprietor is a various suggestion.

if [ -n "$(find . -user "$username" -print -prune -o -prune)" ]; then
  echo "The current directory is owned by $username."
if [ -n "$(find . -user "$(id -u)" -print -prune -o -prune)" ]; then
  echo "The current directory is owned by the current user."
2019-05-21 06:03:17