Como copiar permissões e owner no Linux

A dica de hoje é bem simples e rápida, para quem trabalha com ambientes linux, sempre estamos utilizando o chown para alterar o owner e o chmod para mudar as permissões de diretorios e arquivos. Hoje irei mostrar alguns exemplos de como clonar permissões ou owner de arquivos e diretórios usando um arquivo como referência RFILE.

Sintaxe do chmod:

chmod [OPTIONS] USER[:GROUP] FILE(s)

Sintaxe do chown:

chown [owner/group owner] [file name]

Sintaxe para copiar permissões e owner utilizando o RFILE:

# chmod ----reference= file reference file destination
chmod --reference=RRFILE FILE
chown --reference=RRFILE FILE

Copiando o owner oracle do arquivo c-1542762205-20240425-00 para o arquivo c-1542762205-20240419-00:

[root@orcl_hml backupset]# ls -lat
-rw-r-----  1 oracle asmadmin 14483456 Apr 25 14:57 c-1542762205-20240425-00 #<=== owner oracle
-rwxrwxrwx  1 root   root     14483456 Apr 19 08:49 c-1542762205-20240419-00 #<=== owner root
[root@orcl_hml backupset]# chown --reference=c-1542762205-20240425-00 c-1542762205-20240419-00

Verificando os arquivos:

[root@orcl_hml backupset]# ls -lat
-rw-r-----  1 oracle asmadmin 14483456 Apr 25 14:57 c-1542762205-20240425-00 #<=== owner oracle
-rwxrwxrwx  1 oracle asmadmin 14483456 Apr 19 08:49 c-1542762205-20240419-00 #<=== owner oracle

Copiando as permissões do arquivo c-1542762205-20240425-00 para o arquivo c-1542762205-20240419-00:

[root@orcl_hml backupset]# chmod --reference=c-1542762205-20240425-00 c-1542762205-20240419-00

[root@orcl_hml backupset]# ls -lat
-rw-r-----  1 oracle asmadmin 14483456 Apr 25 14:57 c-1542762205-20240425-00
-rw-r-----  1 oracle asmadmin 14483456 Apr 19 08:49 c-1542762205-20240419-00

Copiando todas as permissões e owner do arquivo file.txt para todos os demais arquivos:

[root@orcl_hml backupset]# ls -lat
total 72
drwxrwxr-x. 2 oracle oinstall 4096 Jun 18 16:34 .
-rwxrwxr-x  1 oracle oinstall    6 Jun 18 16:34 file.txt 	 #<============== template file
-rw-r--r--  1 root   root       13 Jun 18 16:34 file10.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file11.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file12.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file13.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file14.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file15.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file1.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file2.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file3.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file4.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file5.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file6.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file7.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file8.txt
-rw-r--r--  1 root   root       13 Jun 18 16:34 file9.txt

Copiando as permissões:

[root@orcl_hml backupset]# find /backups/backupset/ -type f -exec chmod --reference=/backups/backupset/file.txt {} \;

Copiando o woner:

[root@orcl_hml backupset]# find /backups/backupset/ -type f -exec chown --reference=/backups/backupset/file.txt {} \;

Verificando o owner e permissões após a mudança:

[root@orcl_hml backupset]# ls -lat
total 72
drwxrwxr-x. 2 oracle oinstall 4096 Jun 18 16:34 .
-rwxrwxr-x  1 oracle oinstall    6 Jun 18 16:34 file.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file10.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file11.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file12.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file13.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file14.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file15.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file1.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file2.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file3.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file4.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file5.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file6.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file7.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file8.txt
-rwxrwxr-x  1 oracle oinstall   13 Jun 18 16:34 file9.txt
drwxrwxr-x  7 oracle oinstall 4096 Feb 20 18:18 ..

Com essa dica simples, pode ser que em algum momento possa te ajudar.

search previous next tag category expand menu location phone mail time cart zoom edit close