changing ownership of files within a bash script

7,427

To change ownership of a file, you can use chown, this way :

chown newuser myfile

or, if you want to change group as well :

chown newuser:newgroup myfile

In your case, you can also change your 3 last lines to add sudo -u boudiccas before obnam ..., it should do the trick.

Share:
7,427

Related videos on Youtube

boudiccas
Author by

boudiccas

Updated on September 18, 2022

Comments

  • boudiccas
    boudiccas over 1 year

    I have this bash script which outputs the first two files as owned by user, and the next three as owned by root:

    ##################################################
    # Variables
    NUM="6"     #number of backup files to keep
    LOGFILE="/home/boudiccas/cron/obnam-ls.txt"
    ####################################################
    
    # Change into new directory
    cd /home/boudiccas/cron/obnam/
    
    # Generate new file and save it
    sudo -u boudiccas obnam ls>"obnamhome-ls-$(date +%Y-%m-%d).txt"
    
    sudo -u boudiccas obnam --config=/etc/obnam-back4.conf ls>"obnamback4-ls-$(date +%Y-%m-%d).txt"
    
    obnam --config=/etc/obnametc.conf ls>"obnametc-ls-$(date +%Y-%m-%d).txt"
    
    obnam --config=/etc/obnamusr.conf ls>"obnamusr-ls-$(date +%Y-%m-%d).txt"
    
    obnam --config=/etc/obnamvar.conf ls>"obnamvar-ls-$(date +%Y-%m-%d).txt"
    
    # Delete old backups!
    find /home/boudiccas/cron/obnam -type f -mtime +$NUM -name 'obnamhome-ls-*.txt' -exec rm -v {} + >>$LOGFILE 2>&1
    
    find /home/boudiccas/cron/obnam -type f -mtime +$NUM -name 'obnamback4-ls-*.txt' -exec rm -v {} + >>$LOGFILE 2>&1
    
    find /home/boudiccas/cron/obnam -type f -mtime +$NUM -name 'obnametc-ls-*.txt' -exec rm -v {} + >>$LOGFILE 2>&1
    
    find /home/boudiccas/cron/obnam -type f -mtime +$NUM -name 'obnamusr-ls-*.txt' -exec rm -v {} + >>$LOGFILE 2>&1
    
    find /home/boudiccas/cron/obnam -type f -mtime +$NUM -name 'obnamvar-ls-*.txt' -exec rm -v {} + >>$LOGFILE 2>&1
    ########################################################################
    

    How can i get the last three files to be owned by user through the script please?

  • Levans
    Levans almost 11 years
    @user205787 and what is preventing from adding some chown commands in your script ?
  • boudiccas
    boudiccas almost 11 years
    You were right Levans, chowning did all that I wanted, nice and simply and easily. Thanks.