Results 1 to 7 of 7

Thread: test file ownership from bash?

  1. #1
    Join Date
    Sep 2006
    Location
    Clyde, NC, USA
    Beans
    57
    Distro
    Xubuntu 12.04 Precise Pangolin

    test file ownership from bash?

    I need to test file ownership and the group from bash. I haven't figured out a
    straight forward way to do this. Can anyone suggest something before I start
    working on something using ls and sed?
    Thanks,
    Mark
    Last edited by jmc1024; May 11th, 2007 at 02:16 AM. Reason: accidentally hit submit

  2. #2
    Join Date
    Jan 2007
    Location
    Baltimore, MD
    Beans
    841
    Distro
    Ubuntu

    Re: test file ownership from bash?

    use the comand:
    Code:
    ls -l
    this will display file permissions, owner and group information for all files in a directory.

    (or i did misinterpret your question?)

  3. #3
    psusi is offline Ubuntu addict and loving it
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Beans
    3,980
    Distro
    Ubuntu Development Release

    Re: test file ownership from bash?

    The test command, which can be abbreviated as [, can test the mode bits of a file. It has options to see if it is a normal file, or a symlink, or if you can write to it, and so on. Do you just want to see if you have access to it, or who actually owns it?

  4. #4
    Join Date
    Mar 2007
    Location
    somewhere cold
    Beans
    70
    Distro
    Kubuntu 8.10 Intrepid Ibex

    Re: test file ownership from bash?

    Quote Originally Posted by jmc1024 View Post
    I need to test file ownership and the group from bash. I haven't figured out a
    straight forward way to do this. Can anyone suggest something before I start
    working on something using ls and sed?
    Thanks,
    Mark
    if you're looking for a way to pull the group info on a file (or list of files) then I would suggest using awk instead of sed.

    ls -l filename | awk '{ print $3 }' <-- will give you the owner
    ls -l filename | awk '{ print $4 }' <-- will give you the group

    there are other ways to do this, but this should get you started.
    for beer in $(ls /home/fridge); do
    drink $beer
    done

  5. #5
    Join Date
    Sep 2006
    Location
    Clyde, NC, USA
    Beans
    57
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: test file ownership from bash?

    Yea, ls -l will display them, but then I've got to use sed to pull out the group and
    owner columns before I can test against a string. I was hoping someone knew
    of a command and/or flag that would save me using sed to slice and dice ls -l's
    output.
    Many Thanks,
    Mark

  6. #6
    Join Date
    Aug 2006
    Location
    Belgium
    Beans
    Hidden!
    Distro
    Xubuntu 8.04 Hardy Heron

    Re: test file ownership from bash?

    See man 1 stat:

    Code:
    edb@lapedb:~$ stat -c %U test.c
    edb
    You can use the format string to get these informations from the file:

    Code:
    The valid format sequences for files (without --file-system):
    
      %a   Access rights in octal
      %A   Access rights in human readable form
      %b   Number of blocks allocated (see %B)
      %B   The size in bytes of each block reported by %b
      %d   Device number in decimal
      %D   Device number in hex
      %f   Raw mode in hex
      %F   File type
      %g   Group ID of owner
      %G   Group name of owner
      %h   Number of hard links
      %i   Inode number
      %n   File name
      %N   Quoted file name with dereference if symbolic link
      %o   I/O block size
      %s   Total size, in bytes
      %t   Major device type in hex
      %T   Minor device type in hex
      %u   User ID of owner
      %U   User name of owner
      %x   Time of last access
      %X   Time of last access as seconds since Epoch
      %y   Time of last modification
      %Y   Time of last modification as seconds since Epoch
      %z   Time of last change
      %Z   Time of last change as seconds since Epoch
    
    Valid format sequences for file systems:
    
      %a   Free blocks available to non-superuser
      %b   Total data blocks in file system
      %c   Total file nodes in file system
      %d   Free file nodes in file system
      %f   Free blocks in file system
      %C - Security context in SELinux
      %i   File System ID in hex
      %l   Maximum length of filenames
      %n   File name
      %s   Block size (for faster transfers)
      %S   Fundamental block size (for block counts)
      %t   Type in hex
      %T   Type in human readable form

  7. #7
    Join Date
    Sep 2006
    Location
    Clyde, NC, USA
    Beans
    57
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: test file ownership from bash?

    Perfect! stat -c %U is exactly what I was looking for.
    Many Thanks,
    Mark

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •