如何將文件和資料夾設定為其預設權限?

如何將文件和資料夾設定為其預設權限?

我正在使用 Debian,並且想要在新電腦上重新安裝個人和設定檔(使用者的整個 /home 資料夾)的備份。

由於某些原因我無法理解,我整個備份的每個檔案都具有相同的權限:777 / -rwxrwxrwx。這對於某些設定檔來說過於寬鬆。例如,在重新安裝備份後第一次使用 ssh,我必須執行「chmod 600 ~/.ssh/config」才能啟動 ssh。

我真的不知道為什麼會這樣。我使用rsync -av應該保留權限進行備份,並且我的外部硬碟使用的是 ext4 檔案系統。

不管怎樣,我只有這個備份,不可能再做一個(電腦壞了)。我正在尋找一種方法來自動將文件和資料夾設定為預設值(例如我猜 .ssh/config 檔案是 600)。

有沒有辦法自動實現這一點?

答案1

幹得好;

ORIG_DIR="/bla/bla"
BKUP_DIR="/bla/dee"
:~$ find $ORIG_DIR | while read aline; do perm=$(stat "$aline" | grep "Access: (" | sed 's/Access: (//;s/\/.*//'); chmod -v $perm "$BKUP_DIR/$aline"; done

我舉了一個例子;

:~$ mkdir ooh
:~$ mkdir noo
:~$ touch ooh/mog1 ooh/mog2 ooh/mog3
:~$ rsync -av ooh noo
sending incremental file list
ooh/
ooh/mog1
ooh/mog2
ooh/mog3

sent 245 bytes  received 77 bytes  644.00 bytes/sec
total size is 0  speedup is 0.00
:~$ ls -la noo/ooh/
total 32
drwxr-xr-x 2 mike mike 4096 Oct 28 23:09 .
drwxr-xr-x 3 mike mike 4096 Oct 28 23:10 ..
-rw-r--r-- 1 mike mike    0 Oct 28 23:09 mog1
-rw-r--r-- 1 mike mike    0 Oct 28 23:09 mog2
-rw-r--r-- 1 mike mike    0 Oct 28 23:09 mog3
:~$ chmod 700 ooh/mog1
:~$ chmod 600 ooh/mog2
:~$ chmod 555 ooh/mog3
:~$ find ooh
ooh
ooh/mog1
ooh/mog3
ooh/mog2
:~$ find ooh | while read aline; do perm=$(stat "$aline" | grep "Access: (" | sed 's/Access: (//;s/\/.*//'); chmod -v $perm "noo/$aline"; done
mode of 'noo/ooh' retained as 0755 (rwxr-xr-x)
mode of 'noo/ooh/mog1' changed from 0644 (rw-r--r--) to 0700 (rwx------)
mode of 'noo/ooh/mog3' changed from 0644 (rw-r--r--) to 0555 (r-xr-xr-x)
mode of 'noo/ooh/mog2' changed from 0644 (rw-r--r--) to 0600 (rw-------)

相關內容