Technology Tales

Adventures & experiences in contemporary technology

Detecting file ownership in Korn shell scripts

17th October 2007

I recently was having a play with using a shell script to do some folder creation to help me set up a system for testing and I started to hit ownership issues that caused some shell script errors. At the time, I didn’t realise that there is a test that you can perform for ownership. The "-o" in the code below kicks in the test condition and avoids the error in question.

if [[ -o $dirname ]]
    cd test
    for i in 1 2 3 4 5 6 7 8 9 10
        if [[ -d study$i ]]
            mkdir study$i
    cd ~

Previously, I shared a way to test for directory (-d operator) and file (-f operator) existence that follows the above coding convention. However, there are a plethora of others and I have made a list of them here:

-e fileFile exists
-L fileFile is symbolic link
-r fileUser has read access to file
-s fileFile is non-empty
-w fileUser has write access to file
-x fileUser has execute access to file
-G fileUser’s effective group ID is the same as that of the file
file1 -nt file2File 1 is newer than file2
file1 -ot file2File 1 is older than file2
file1 -et file2File 1 was created at the same time as file2

It’s all useful stuff when you want to rid the command line output of errors in an above board sort of way. These are the kinds of things that often make life easier…

  • All the views that you find expressed on here in postings and articles are mine alone and not those of any organisation with which I have any association, through work or otherwise. As regards editorial policy, whatever appears here is entirely of my own choice and not that of any other person or organisation.

  • Please note that everything you find here is copyrighted material. The content may be available to read without charge and without advertising but it is not to be reproduced without attribution. As it happens, a number of the images are sourced from stock libraries like iStockPhoto so they certainly are not for abstraction.

  • With regards to any comments left on the site, I expect them to be civil in tone of voice and reserve the right to reject any that are either inappropriate or irrelevant. Comment review is subject to automated processing as well as manual inspection but whatever is said is the sole responsibility of the individual contributor.