timmy / 更改目錄權限與擁有者
0 likes
0 forks
1 files
Last active 7 months ago
使用 chown 和 chmod 將資料夾及其內容的擁有者設為 nobody,並設定適當的讀寫執行權限。
| 1 | # 將該資料夾及裡面的所有檔案/子目錄的 owner & group 改成 65534:65534 |
| 2 | # 65534 就是「nobody」的代號,用來指定權限最低的帳號,降低風險。 |
| 3 | sudo chown -R 65534:65534 /path/to/directory |
| 4 | |
| 5 | # 把該資料夾及其所有內容的權限設成 775 |
| 6 | # 表示 owner 與同群組成員可讀寫執行,其他人只能讀與執行 |
| 7 | sudo chmod -R 775 /path/to/directory |
| 1 | mkdir archive |
| 2 | |
| 3 | find . -maxdepth 1 -type f -exec mv {} archive/ \; |
timmy / 使用 shutil 進行檔案與目錄操作
0 likes
0 forks
1 files
Last active 9 months ago
shutil 模組提供高級的檔案與目錄管理功能,包括複製、移動、壓縮與刪除,適用於備份、部署與自動化檔案管理。
| 1 | import shutil |
| 2 | import os |
| 3 | |
| 4 | # 定義來源與目標 |
| 5 | source_file = "example.txt" |
| 6 | destination_dir = "backup/" |
| 7 | destination_file = os.path.join(destination_dir, source_file) |
| 8 | |
| 9 | # 確保目標目錄存在 |
| 10 | os.makedirs(destination_dir, exist_ok=True) |
Newer
Older