3.11 chown(更改文件或目录的拥有者或所属组)

本文深入探讨了Linux系统中用于更改文件或目录拥有者或所属组的chown命令,包括其使用方法、参数以及相关命令。通过实例演示了如何批量修改文件属性,提供实用技巧和注意事项。

3.11 chown(更改文件或目录的拥有者或所属组)

2011-07-21 23:23 华清远见嵌入式培训中心/贺丹丹 电子工业出版社  我要评论(0) 字号: T |  T
一键收藏,随时查看,分享好友!

《Linux指令从初学到精通》第3章文件管理,本章介绍了许多常用命令,如cp、ln、chmod、chown、diff、tar、mv等,因为这些都与文件管理相关,在日常的使用中经常用到,因此本章是本书的重点章节。本节为大家介绍chown(更改文件或目录的拥有者或所属组)。

AD: 2013云计算架构师峰会超低价抢票中

3.11 chown(更改文件或目录的拥有者或所属组)

(1)频度等级:☆☆

(2)功能说明:

UNIX/Linux 是多人多作业系统,所有的文件皆有拥有者。利用 chown 可以将文件的拥有者加以改变。

(3)语法:

   
  1. chown [-cfhRv][--dereference][--help][--version]
    [拥有者.
    <所属群组>][文件或目录..] 或  
  2. chown [-chfRv][--dereference][--help][--version]
    [.所属群组][文件或目录...] 或  
  3. chown [-cfhRv][--dereference][--help][--reference
    =<参考文件或目录>][--version][文件或目录...] 

(4)所属包:

   
  1. chgrp 

(5)使用技巧:

批量修改同目录下的文件的所有者和组信息,可使用参数“-R”。如果被修改文件命名有一定的规则,则可使用通配符。

命令指定的用户名和组信息可以使用用户ID 和组ID来代替。

(6)相关命令:

   
  1. chgrp 

(7)参数:

chown主要参数如表3-13 所示。

表3-13 chown参数表

 

(8)补充说明:

在UNIX 系统家族里,文件或目录权限的掌控由拥有者及所属群组来管理。用户可以使用chown指令去变更文件与目录的拥有者或所属群组,设置方式采用用户名称或用户识别码,设置群组则用群组名称或群组识别码。

(9)典型实例:

例1:改变文件所有者。

例如修改文件testfile的文件所有者为root,可使用如下命令:

   
  1. chown-v root testfile #修改文件所有者为root,-v参数为显示详细过程 

为了使读者更加清楚,先通过ls命令查看原始属性,testfile的原始属性如下:

   
  1. cmd@cmd-desktop:~$ls-l testfile #查看原有属性  
  2. -rw-r--r-- 1 cmd cmd 598 2009-09-01 15:05 
    testfile #所有者为cmd,所属组为cmd 

使用chown命令修改所属组,再次查看属性,结果如下:

   
  1. cmd@cmd-desktop:~$chown-v root testfile  
  2. changed ownership of ‘testfile’ to ‘root’  
  3. cmd@cmd-desktop:~$ls-l testfile #查看修改后的属性  
  4. -rw-r-r-- 1 root cmd 598 2009-09-01 15:07 testfile #文件所有者改为root 

例2:同时修改文件所有者和所属组。如果想同时修改文件所有者和所属组,可使用如下命令:

   
  1. chown-v root:root testfile #修改文件所有者为
    root,所属组为root,-v参数为显示详细过程 

使用chown命令修改所属组和所有者,查看属性,结果如下:

   
  1. cmd@cmd-desktop:~$chown-v root testfile  
  2. changed ownership of ‘testfile’ to root:root  
  3. cmd@cmd-desktop:~$ls-l testfile #查看修改后的属性  
  4. -rw-r-r-- 1 root root 598 2009-09-01 15:10 
    testfile #文件所有者改为root,所属组改为root 
Fetching 15 files: 0%| | 0/15 [00:00<?, ?it/s] Traceback (most recent call last): File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1116, in mkdir os.mkdir(self, mode) FileNotFoundError: [Errno 2] No such file or directory: &#39;/Volume/ExtremePro/models/astrobert/.cache/huggingface&#39; During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1116, in mkdir os.mkdir(self, mode) FileNotFoundError: [Errno 2] No such file or directory: &#39;/Volume/ExtremePro/models/astrobert/.cache&#39; During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1116, in mkdir os.mkdir(self, mode) FileNotFoundError: [Errno 2] No such file or directory: &#39;/Volume/ExtremePro/models/astrobert&#39; During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1116, in mkdir os.mkdir(self, mode) FileNotFoundError: [Errno 2] No such file or directory: &#39;/Volume/ExtremePro/models&#39; During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1116, in mkdir os.mkdir(self, mode) FileNotFoundError: [Errno 2] No such file or directory: &#39;/Volume/ExtremePro&#39; During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/bin/hf", line 7, in <module> sys.exit(main()) ^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/cli/hf.py", line 59, in main service.run() File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/cli/download.py", line 136, in run print(self._download()) # Print path to downloaded files ^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/cli/download.py", line 169, in _download return snapshot_download( ^^^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/_snapshot_download.py", line 332, in snapshot_download thread_map( File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/tqdm/contrib/concurrent.py", line 69, in thread_map return _executor_map(ThreadPoolExecutor, fn, *iterables, **tqdm_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/tqdm/contrib/concurrent.py", line 51, in _executor_map return list(tqdm_class(ex.map(fn, *iterables, chunksize=chunksize), **kwargs)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/tqdm/std.py", line 1181, in __iter__ for obj in iterable: File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/concurrent/futures/_base.py", line 619, in result_iterator yield _result_or_cancel(fs.pop()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/concurrent/futures/_base.py", line 317, in _result_or_cancel return fut.result(timeout) ^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/concurrent/futures/_base.py", line 449, in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result raise self._exception File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/_snapshot_download.py", line 306, in _inner_hf_hub_download return hf_hub_download( ^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/file_download.py", line 987, in hf_hub_download return _hf_hub_download_to_local_dir( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/file_download.py", line 1215, in _hf_hub_download_to_local_dir paths = get_local_download_paths(local_dir=local_dir, filename=filename) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/_local_folder.py", line 218, in get_local_download_paths metadata_path = _huggingface_dir(local_dir) / "download" / f"{sanitized_filename}.metadata" ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/zhangfengyue/Downloads/ai_astro_exhibit/.venv/lib/python3.11/site-packages/huggingface_hub/_local_folder.py", line 425, in _huggingface_dir path.mkdir(exist_ok=True, parents=True) File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1120, in mkdir self.parent.mkdir(parents=True, exist_ok=True) File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1120, in mkdir self.parent.mkdir(parents=True, exist_ok=True) File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1120, in mkdir self.parent.mkdir(parents=True, exist_ok=True) [Previous line repeated 2 more times] File "/opt/homebrew/Cellar/python@3.11/3.11.14_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1116, in mkdir os.mkdir(self, mode) OSError: [Errno 30] Read-only file system: &#39;/Volume&#39;
最新发布
11-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值