Announcement

Collapse
No announcement yet.

Create file with same ownership as the folder it is created in

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Create file with same ownership as the folder it is created in

    I have a script that needs to be run with sudo (or as root).

    In the script, I create a file with "touch", but since the script is running as root the new file is owned by root.

    I know how to change the ownership to a particular user if that user is known, but was wondering if there is a way to create a file that has the same permissions and ownership as the folder that it is in.

    In the script, the file is always being created in the user's home folder, so if this is possible it would mean that whichever user ran the script would own the file.

    Feathers
    samhobbs.co.uk

    #2
    Have you tried the followin?
    sudo -u user touch filename

    Comment


      #3
      Thanks for the info!

      How would I use this with a script?

      If I run the script with sudo -u username /path/to/bash/script, then would all commands in the script be executed as username?

      If I could use it for some of the commands, and not others, then that would be perfect.

      Feathers
      samhobbs.co.uk

      Comment


        #4
        So I thought I'd found a clever solution... apparently not! Here's a short version of my script, most stuff edited out to give you an easy working version:

        Code:
        #!/bin/bash
        
        # some variables
        FILES_FOLDER=~/personalised-hosts
        WHITELIST_FILE=$FILES_FOLDER/whitelist
        
        PERMISSION=$(ls -ld $FILES_FOLDER | awk '{print $3}')
        
        if [ ! -f $WHITELIST_FILE ]
        then
         echo "No whitelist file detected, creating a blank one here: $WHITELIST_FILE"
         touch $WHITELIST_FILE
        fi
        
        # Change ownership of files to match owner of $FILES_FOLDER
        chown -R $PERMISSION:$PERMISSION $FILES_FOLDER
        Before running the script, the files folder already exists, and is owned by me (username feathers-mcgraw).

        Code:
        feathers-mcgraw@62-West-Wallaby-Street:~$ ls -l
        total 12
        drwxr--r-- 2 feathers-mcgraw feathers-mcgraw 4096 Oct 31 20:40 bin
        drwxr-xr-x 2 feathers-mcgraw feathers-mcgraw 4096 Sep 19 06:08 Desktop
        -rw-rw-r-- 1 feathers-mcgraw feathers-mcgraw 2284 Oct 14 12:17 sam@samhobbs.co.uk_public_key.asc
        feathers-mcgraw@62-West-Wallaby-Street:~$ mkdir personalised-hosts
        feathers-mcgraw@62-West-Wallaby-Street:~$ ls -l
        total 16
        drwxr--r-- 2 feathers-mcgraw feathers-mcgraw 4096 Oct 31 20:40 bin
        drwxr-xr-x 2 feathers-mcgraw feathers-mcgraw 4096 Sep 19 06:08 Desktop
        drwxrwxr-x 2 feathers-mcgraw feathers-mcgraw 4096 Oct 31 20:48 personalised-hosts
        -rw-rw-r-- 1 feathers-mcgraw feathers-mcgraw 2284 Oct 14 12:17 sam@samhobbs.co.uk_public_key.asc
        I run the script, and afterwards:

        Code:
        feathers-mcgraw@62-West-Wallaby-Street:~$ ls -l
        total 16
        drwxr--r-- 2 feathers-mcgraw feathers-mcgraw 4096 Oct 31 20:40 bin
        drwxr-xr-x 2 feathers-mcgraw feathers-mcgraw 4096 Sep 19 06:08 Desktop
        drw-r--r-- 2 feathers-mcgraw feathers-mcgraw 4096 Oct 31 20:49 personalised-hosts
        -rw-rw-r-- 1 feathers-mcgraw feathers-mcgraw 2284 Oct 14 12:17 sam@samhobbs.co.uk_public_key.asc
        feathers-mcgraw@62-West-Wallaby-Street:~$ cd personalised-hosts
        bash: cd: personalised-hosts: Permission denied
        feathers-mcgraw@62-West-Wallaby-Street:~$ ls -l personalised-hosts
        ls: cannot access personalised-hosts/hosts-system: Permission denied
        ls: cannot access personalised-hosts/whitelist: Permission denied
        ls: cannot access personalised-hosts/hosts-sources: Permission denied
        ls: cannot access personalised-hosts/hosts-block-personal: Permission denied
        ls: cannot access personalised-hosts/hosts-block: Permission denied
        total 0
        -????????? ? ? ? ?            ? hosts-block
        -????????? ? ? ? ?            ? hosts-block-personal
        -????????? ? ? ? ?            ? hosts-sources
        -????????? ? ? ? ?            ? hosts-system
        -????????? ? ? ? ?            ? whitelist
        feathers-mcgraw@62-West-Wallaby-Street:~$ sudo ls -l personalised-hosts
        total 1848
        -rw-r--r-- 1 feathers-mcgraw feathers-mcgraw 939916 Oct 31 20:49 hosts-block
        -rw-r--r-- 1 feathers-mcgraw feathers-mcgraw 939916 Oct 31 20:49 hosts-block-personal
        -rw-r--r-- 1 feathers-mcgraw feathers-mcgraw    207 Oct 31 20:49 hosts-sources
        -rw-r--r-- 1 feathers-mcgraw feathers-mcgraw    237 Oct 31 20:49 hosts-system
        -rw-r--r-- 1 feathers-mcgraw feathers-mcgraw      0 Oct 31 20:49 whitelist
        Why is Kubuntu not letting me into the folder if I own it, and everything in it?!

        Feathers
        samhobbs.co.uk

        Comment


          #5
          Looks to me like you don't have the execute bit set on the directories. You can't parse a directory without the "x"

          Please Read Me

          Comment


            #6
            That was it! Thank you so much

            Pays to have all you knowledgeable old timers around
            samhobbs.co.uk

            Comment


              #7
              Originally posted by feathers mcgraw View Post
              ...old timers...
              excuse me?

              Please Read Me

              Comment


                #8
                Roflmao ;d

                Please Read Me

                Comment


                  #9
                  You're a Kubuntu veteran, and I'm a noob! Have only been using Linux since April
                  samhobbs.co.uk

                  Comment

                  Working...
                  X