在 Bash 脚本中更改文件所有权

在 Bash 脚本中更改文件所有权

我有这个 bash 脚本,它将前两个文件输出为用户所拥有,将后三个文件输出为 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
########################################################################

请问我如何通过脚本获取用户拥有的最后三个文件?

答案1

要更改文件的所有权,您可以使用chown,方式如下:

chown newuser myfile

或者,如果您也想更改组:

chown newuser:newgroup myfile

对于您来说,您还可以将最后 3 行更改为sudo -u boudiccas在之前添加obnam ...,这样应该可以解决问题。

相关内容