s there a way to determine the owner of a file by using a script?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I will know the file name, such as File002.doc. What I need to get is the owner xxx02. I could use cut to (ls -l testp | cut -b 17-21) in this example, but that would assume a 5 character owner name. Maybe there is another command that will get the file's owner? Any other ideas on determining the owner of a file by using a script?
I will know the file name, such as File002.doc. What I need to get is the owner xxx02. I could use cut to (ls -l testp | cut -b 17-21) in this example, but that would assume a 5 character owner name.
This could be solved by cutting by field number (option: -f4). For this, you need to specify spaces as seperator (option: -d' '). Example:
Code:
ls -l File002.doc | cut -d' ' -f4
But because "cut" sees each space as a seperator, it may not be very reliable. A solution using "sed", "awk", "python", "perl" would better, but slower.
Another way by setting shell arguments:
Code:
set -- $(ls -l File002.doc)
# Now $1, $2, $3,.. are set to the different fields in
# the output of "ls":
echo "Owner: $3"
echo "Group: $4"
echo "Size: $5"
But probably the most reliable, shortest and fastest way is to use "stat" instead of "ls":
Wow...Not just one but, 3 great ways to get the job done. Thanks Hko and pljvaldez. stat will work best for my current script, but I see uses for the other options too. Excellent.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.