[英]zip the files and folders inside a parent directory without including the parent directory + Amazon Linux
[英]linux files and folders are not inheriting parent directory permissions
我創建了目錄/share
並賦予chmod 2770
權限,並賦予chown root:stock /share
。
1)當我在/share
內部創建一個觸摸文件時,我看到該文件具有rw-rw-r--
而我沒有看到rwxrws---
2)當我在/share/data
創建目錄時,我看到的權限為drwxrwsr-x
,其中父目錄為drwxrws---
如何使父子文件和子目錄具有固有的父權限完全相同。
目錄中的setgid
位使新文件從目錄繼承group
,而不是其權限。
控制在文件創建時設置的位的標准方法是控制創建過程的umask (askubuntu),而不是文件系統。
創建文件或目錄時
新文件或目錄的所有者將是您的有效用戶ID( euid
)。 您可以根據/etc/sudoers*
的設置,使用su other_user
命令(將提示您輸入other_user
的密碼)或sudo su other_user
(允許或不允許您,可能要求您輸入密碼)來預先更改用戶ID。 /etc/sudoers*
)。 創建文件或目錄后,可以使用sudo chown other_user file_name
更改其所有者。
新文件或目錄的組將是您的有效組ID。 您可以預先使用newgrp other_group
命令更改組ID。 如果當前目錄將other_group
作為組,並且將其setgid
位置1,則有效的組ID將為other_group
。 創建文件或目錄后,可以使用chgrp other_group file_name
更改其組。 如果您是other_group
的成員,則newgrp
, chgrp
和setgid
可以使用。 如果您不這樣做,他們將不會:團體密碼機制理論上仍然存在,但幾十年前已被棄用,我從未見過有人使用它。 當然,如果要同時更改兩者,則始終可以使用sudo chgrp other_group file_name
,甚至可以使用sudo chown other_user:other_group file_name
。
新文件或目錄的讀寫權限取決於您的umask
,通常由登錄時配置文件設置。 最常用的umask值是022
(對於文件,將為您提供-rw-r--r--
和002
(將為您提供-rw-rw-r--
。 命令umask
將為您提供當前值。 您可以使用umask new_value
設置另一個值,該值將一直有效,直到您更改它或退出shell。 目錄也將默認設置所有執行權限,除非您在umask
具有奇數值,這將阻止相應的執行位。 例如,umask值027
將創建帶有-rw-r-----
文件和帶有drwxrwx---
目錄。 請參考文檔以獲取完整說明。 另外,如果父目錄具有setgid
位,則新目錄也將具有它。 默認情況下,無法設置setuid
和sticky
位,也無法設置文件的setgid
位。
之后,您始終可以使用chmod
命令設置所需的權限。
也就是說,沒有標准命令可以執行您想要的操作。 但是,您可以像下面這樣輕松地編寫bash函數並使用它們(將它們寫入文件mycreat_functions
並在需要時提供source mycreat_functions
)。 這將適用於手動創建的文件和目錄。 對於由程序,shell重定向等創建的文件,您仍然必須手動更正權限。
function mymkdir () {
local parentperms
for a in "$@"; do
mkdir "$a"
# This copies all permissions of the parent,
# exactly as they are
parentperms="$(stat -c%a $(dirname "$a"))"
chmod "$parentperms" "$a"
# if I’m root...
if [ $(id -u) = 0 ]; then
chown "$(stat -c%u:%g "$a")" "$a"
fi
done
}
function mytouch () {
local parentperms newperms
for a in "$@"; do
touch "$a"
# This inherits all permissions of the parent,
# but removes the excution and setgid bits, as is
# appropriate for files.
parentperms="$(stat -c%a $(dirname "$a"))"
newperms="$(printf %o $((8#$parentperms & 8#5666)))"
chmod "$newperms" "$a"
# if I’m root...
if [ $(id -u) = 0 ]; then
chown "$(stat -c%u:%g "$a")" "$a"
fi
done
}
注意:所有者,組和權限存儲在一個inode中,那里還有關於如何檢索文件內容的其他信息; 目錄條目將索引節點與文件名相關聯,而ls -i
顯示列出文件的索引節點號 。 復制文件時,將創建一個新的目錄條目並分配一個新的inode,因此此處提到的所有內容均適用。 移動文件時,您將在新位置創建一個新目錄條目,但將其指向舊的inode,以便有效地保持所有者,組和權限不變。 如果希望它們根據新目錄條目的父項進行更改,則必須沿着上面的mytouch
和mymkdir
行創建一個mymv
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.