前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >文件管理类命令常用使用方法及其相关示例演示

文件管理类命令常用使用方法及其相关示例演示

作者头像
小小科
发布2018-05-04 15:51:37
8300
发布2018-05-04 15:51:37
举报
文章被收录于专栏:北京马哥教育北京马哥教育

cat

使用语法:cat [-AbeEnstTuv][--help][--version] FILENAME

命令功能:查看文件内容或将多文件连接后传输到基本输出(也可以通过重定向将多文件合并为一个文件)

常用选项及示例

-n或–number 由1开始对所有输出的内容以行为单位进行编号

1.[root - gwx-st ~]#>cat test1 -n 2.1 1111 3.2 4.3 22222 5.4 333333

-b 或–number-nonblank 功能与-n相似,但是会忽略文件内的空白行

1.[root - gwx-st ~]#>cat test2 -b 2.1 1111 3.2 3333

将两个文件合并为一个文件

1.[root - gwx-st ~]#>cat test1 test2 -n > test3 2.[root - gwx-st ~]#>cat test3 3.1 1111 4.2 5.3 22222 6.4 333333 7.5 1111 8.6 9.7 3333

chattr

使用语法:chattr [-RV][-v<版本编号>][+/-/=<属性>][文件或目录...] 命令功能:改变文件隐藏属性

隐藏属性模式

1.a:让文件或目录仅供附加用途。 2.b:不更新文件或目录的最后存取时间。 3.c:将文件或目录压缩后存放。 4.d:将文件或目录排除误操作之外。 5.i:不得任意更动文件或目录。 6.s:保密性删除文件或目录。 7.S:即时更新文件或目录。 8.u:预防意外删除。

常用选项及示例

-R 递归处理,将指定目录下的所有文件及子目录一并处理。 -V 显示指令执行过程。 +<属性> 开启文件或目录的该项属性。

1.[root - gwx-st ~]#>chattr -RV +i test 2.chattr 1.42.9 (28-Dec-2013) 3.Flags of test set as ----i----------- 4.Flags of test/test1 set as ----i----------- 5.Flags of test/test2 set as ----i----------- 6.[root - gwx-st ~]#>lsattr test 7.----i----------- test/test1 8.----i----------- test/test2

-<属性> 关闭文件或目录的该项属性。

1.[root - gwx-st ~]#>chattr -RV -i test 2.chattr 1.42.9 (28-Dec-2013) 3.Flags of test set as ---------------- 4.Flags of test/test1 set as ---------------- 5.Flags of test/test2 set as ---------------- 6.[root - gwx-st ~]#>lsattr test 7.---------------- test/test1 8.---------------- test/test2

=<属性> 指定文件或目录的该项属性。

1.[root - gwx-st ~]#>chattr -RV =idS test 2.chattr 1.42.9 (28-Dec-2013) 3.Flags of test set as --S-i-d--------- 4.Flags of test/test1 set as --S-i-d--------- 5.Flags of test/test2 set as --S-i-d--------- 6.[root - gwx-st ~]#>lsattr test 7.--S-i-d--------- test/test1 8.--S-i-d--------- test/test2 9.

chmod

使用语法:chmod [-cfRv][--help][--version][<权限范围>+/-/=<权限设置...>][文件或目录...]

或chmod[-cfRv][--help][--version][数字代号][文件或目录...]

或 chmod [-cfRv][--help][--reference=<参考文件或目录>][--version][文件或目录...]

命令功能:变更文件或目录的权限。

补充说明:

1. 文件或目录权限的控制分别以读取,写入,执行3种一般权限来区分,可以使用 chmod指令去变更文件与目录的权限,设置方式采用文字或数字代号皆可。符号连接的权限无法变更,如果您对符号连接修改权限,其改变会作用在被连接的原始文件。 2. 权限范围的表示法如下: 3. u:User,即文件或目录的拥有者。 4. g:Group,即文件或目录的所属群组。 5. o:Other,除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围。 6. a:All,即全部的用户,包含拥有者,所属群组以及其他用户。 7. 有关权限代号的部分,列表于下: 8. r:读取权限,数字代号为"4"。 9. w:写入权限,数字代号为"2"。 10. x:执行或切换权限,数字代号为"1"。 11. -:不具任何权限,数字代号为"0"。

常用选项及示例

-v或–verbose  显示指令执行过程。 -c或–changes  效果类似”-v”参数,但仅回报更改的部分。 -f或–quiet或–silent  静默模式,不显示错误信息。 -R或–recursive  递归处理,将指定目录下的所有文件及子目录一并处理。

<权限范围>+<权限设置>  开启权限范围的文件或目录的该项权限设置。

1.[root - gwx-st ~]#>chmod -Rv +w test 2.mode of 'test' retained as 0755 (rwxr-xr-x) 3.mode of 'test/test1' retained as 0644 (rw-r--r--) 4.mode of 'test/test2' retained as 0644 (rw-r--r--)

<权限范围>-<权限设置>  关闭权限范围的文件或目录的该项权限设置。

1.[root - gwx-st ~]#>chmod -Rv a-w test 2.mode of 'test' changed from 0755 (rwxr-xr-x) to 0555 (r-xr-xr-x) 3.mode of 'test/test1' changed from 0644 (rw-r--r--) to 0444 (r--r--r--) 4.mode of 'test/test2' changed from 0644 (rw-r--r--) to 0444 (r--r--r--)

<权限范围>=<权限设置>  指定权限范围的文件或目录的该项权限设置。

1.[root - gwx-st ~]#>chmod -Rv a=w test 2.mode of 'test' changed from 0555 (r-xr-xr-x) to 0222 (-w--w--w-) 3.mode of 'test/test1' changed from 0444 (r--r--r--) to 0222 (-w--w--w-) 4.mode of 'test/test2' changed from 0444 (r--r--r--) to 0222 (-w--w--w-)

–reference=<参考文件或目录>  把指定文件或目录的权限全部设 成和参考文件或目录的权限相同

1.[root - gwx-st ~]#>ll -d test 2.d-w--w--w-. 2 root root 30 Nov 4 14:37 test/ 3.[root - gwx-st ~]#>ll sh -d 4.drwxr-xr-x. 2 root root 4.0K Nov 2 09:03 sh/ 5.[root - gwx-st ~]#>chmod --reference sh test 6.[root - gwx-st ~]#>ll -d sh test 7.drwxr-xr-x. 2 root root 4.0K Nov 2 09:03 sh/ 8.drwxr-xr-x. 2 root root 30 Nov 4 14:37 test/

chown

使用语法:chown [-cfhRv][--dereference][--help][--version][拥有者.<所属群组>][文件或目录..]

或chown[-chfRv][--dereference][--help][--version][.所属群组][文件或目录... ...]

或chown[-cfhRv][--dereference][--help][--reference=<参考文件或目录>][--version][文件或目录...]

命令功能:变更文件或目录的拥有者或所属群组

常用选项及示例

-f或–quite或–silent  不显示错误信息。 -R或–recursive  递归处理,将指定目录下的所有文件及子目录一并处理。 -v或–version  显示指令执行过程。 -c或–changes  效果类似”-v”参数,但仅回报更改的部分。

1.[root - gwx-st ~]#>ll -d test 2.d-w--w--w-. 2 root root 30 Nov 4 14:37 test/ 3.[root - gwx-st ~]#>chown -Rc gwx.gwx test 4.changed ownership of 'test/test1' from root:root to gwx:gwx 5.changed ownership of 'test/test2' from root:root to gwx:gwx 6.changed ownership of 'test' from root:root to gwx:gwx 7.[root - gwx-st ~]#>ll -d test 8.d-w--w--w-. 2 gwx gwx 30 Nov 4 14:37 test/

-h或–no-dereference  之对符号连接的文件作修改,而不更动其他任何相关文件。

1.[root - gwx-st ~]#>ll test1 2.lrwxrwxrwx. 1 root root 4 Nov 4 16:02 test1 -> test/ 3.[root - gwx-st ~]#>ll -d test 4.drwxr-xr-x. 2 root root 30 Nov 4 16:04 test/ 5.[root - gwx-st ~]#>chown -hc gwx.gwx test1 6.changed ownership of 'test1' from root:root to gwx:gwx 7.[root - gwx-st ~]#>ll test1 8.lrwxrwxrwx. 1 gwx gwx 4 Nov 4 16:02 test1 -> test/ 9.[root - gwx-st ~]#>ll -d test 10.drwxr-xr-x. 2 root root 30 Nov 4 16:04 test/

–reference=<参考文件或目录>  把指定文件或目录的拥有者与所属群组全部设成和参考文件或目录的拥有者与所属群组相同。

1.[root - gwx-st ~]#>ll -d test 2.drwxr-xr-x. 2 root root 30 Nov 4 16:04 test/ 3.[root - gwx-st ~]#>ll /home/gwx/sh/back-etc.sh 4.-rwxr-xr--. 1 gwx gwx 746 Oct 26 16:04 /home/gwx/sh/back-etc.sh* 5.[root - gwx-st ~]#>chown --reference=/home/gwx/sh/back-etc.sh test 6.[root - gwx-st ~]#>ll -d test 7.drwxr-xr-x. 2 gwx gwx 30 Nov 4 16:04 test/

cp

使用语法:cp [-abdfilpPrRsuvx][源文件或目录][目标文件或目录][目的目录]

命令功能:复制文件或目录

常用选项及示例

-a 或–archive 此参数的效果和同时指定”-dpR”参数相同。 -b 或–backup 删除,覆盖目标文件之前的备份,备份文件会在字尾加上一个备份字符串。 -v 或–verbose 显示指令执行过程。 -i 或–interactive 覆盖既有文件之前先询问用户。 -f 或–force 强行复制文件或目录,不论目标文件或目录是否已存在。 -r 递归处理,将指定目录下的文件与子目录一并处理。 -R 或–recursive 递归处理,将指定目录下的所有文件与子目录一并处理。 -p 或–preserve 保留源文件或目录的属性。 -P 或–parents 保留源文件或目录的路径。

1.[root - gwx-st ~]#>ll -d test* 2.drwxr-xr-x. 2 gwx gwx 30 Nov 4 16:04 test/ 3.[root - gwx-st ~]#>cp -aiv test test1 4.'test' -> 'test1' 5.'test/test1' -> 'test1/test1' 6.'test/test2' -> 'test1/test2' 7.[root - gwx-st ~]#>ll -d test* 8.drwxr-xr-x. 2 gwx gwx 30 Nov 4 16:04 test/ 9.drwxr-xr-x. 2 gwx gwx 30 Nov 4 16:04 test1/

-s 或–symbolic-link 对源文件建立符号连接,而非复制文件。

1.[root - gwx-st ~]#>ll sh.tar.xz 2.-rw-r--r--. 1 root root 2.7K Oct 23 16:59 sh.tar.xz 3.[root - gwx-st ~]#>cp -sv sh.tar.xz sh.tar.xz.ln 4.'sh.tar.xz' -> 'sh.tar.xz.ln' 5.[root - gwx-st ~]#>ll sh.tar.xz.ln 6.lrwxrwxrwx. 1 root root 9 Nov 4 16:40 sh.tar.xz.ln -> sh.tar.xz 7.

-d 或–no-dereference 当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录。

1.[root - gwx-st ~]#>ll sh.tar.xz.ln 2.lrwxrwxrwx. 1 root root 9 Nov 4 16:40 sh.tar.xz.ln -> sh.tar.xz 3.[root - gwx-st ~]#>cp -d sh.tar.xz.ln sh1.tar.xz 4.[root - gwx-st ~]#>ll sh1.tar.xz 5.lrwxrwxrwx. 1 root root 9 Nov 4 16:41 sh1.tar.xz -> sh.tar.xz 6.

-l 或–link 对源文件建立硬连接,而非复制文件。

1.[root - gwx-st ~]#>cp -l sh.tar.xz sh2.tar.xz 2.[root - gwx-st ~]#>ll -i sh*.tar.xz 3.101983257 lrwxrwxrwx. 1 root root 9 Nov 4 16:41 sh1.tar.xz -> sh.tar.xz 4.119955255 -rw-r--r--. 2 root root 2.7K Oct 23 16:59 sh2.tar.xz 5.119955255 -rw-r--r--. 2 root root 2.7K Oct 23 16:59 sh.tar.xz 6.

ln

使用语法:ln [-bdfinsv][源文件或目录][目标文件或目录] 或 ln [-bdfinsv][源文件或目录...][目的目录] 命令功能:连接文件或目录

常用选项及示例

-f或–force  强行建立文件或目录的连接,不论文件或目录是否存在。

-i或–interactive  覆盖既有文件之前先询问用户。 -n或–no-dereference  把符号连接的目的目录视为一般文件。 -s或–symbolic  对源文件建立符号连接,而非硬连接。

1.[root - gwx-st ~]#>ll nginx-1.11.5.tar.gz 2.-rw-r--r--. 1 root root 935K Oct 29 14:02 nginx-1.11.5.tar.gz 3.[root - gwx-st ~]#>ln -s nginx-1.11.5.tar.gz nginx 4.[root - gwx-st ~]#>ln nginx-1.11.5.tar.gz nginx1 5.[root - gwx-st ~]#>ll -i nginx* 6.101983269 lrwxrwxrwx. 1 root root 19 Nov 4 17:06 nginx -> nginx-1.11.5.tar.gz 7.131383020 -rw-r--r--. 2 root root 935K Oct 29 14:02 nginx1 8.131383020 -rw-r--r--. 2 root root 935K Oct 29 14:02 nginx-1.11.5.tar.gz.11.5.tar.gz nginx1

locate

使用语法:locate [-d <数据库文件>][范本样式...]

命令功能:基于本地文件数据库查找文件

常用选项及示例

-d<数据库文件>或–database=<数据库文件> 设置locate指令使用的数据库。locate指令预设的数据库位于/var/lib/slocate目录里,文件名为slocate.db,您可使用这个参数另行指定。使用updatedb更新本地文件数据库。

1.[root - gwx-st ~]#>locate nginx-1.11.5.tar.gz 2./root/nginx-1.11.5.tar.gz

lsattr

使用语法:lsattr [-adlRvV][文件或目录...] 命令功能:显示文件隐藏属性

常用选项及示例

-a  显示所有文件和目录,包括以”.”为名称开头字符的额外内建,现行目录”.”与上层目录”..”。

1.[root - gwx-st ~/test]#>lsattr -a 2.---------------- ./. 3.---------------- ./.. 4.---------------- ./test1 5.---------------- ./test2 6.---------------- ./test.ln

-d  显示,目录名称,而非其内容。

1.[root - gwx-st ~/test]#>lsattr -d 2.---------------- .

-R  递归处理,将指定目录下的所有文件及子目录一并处理。

1.[root - gwx-st ~]#>lsattr -R test 2.---------------- test/test1 3.---------------- test/test2 4.---------------- test/test.ln

mv

使用语法:mv [-bfiuv][源文件或目录][目标文件或目录] 命令功能:移动或更名现有的文件或目录。不同不经下执行为移动,同一路径下执行为更名。

常用选项及示例

-b或–backup  若需覆盖文件,则覆盖前先行备份。 -f或–force  若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文 件或目录。 -i或–interactive  覆盖前先行询问用户。 -v或–verbose  执行时显示详细的信息。

1.移动 2.[root - gwx-st ~/test]#>ll 3.total 12K 4.-rw-r--r--. 2 root root 16 Nov 4 16:03 test1 5.-rw-r--r--. 1 root root 17 Nov 4 16:04 test2 6.-rw-r--r--. 2 root root 16 Nov 4 16:03 test.ln 7.[root - gwx-st ~/test]#>mv ../passwd . 8.[root - gwx-st ~/test]#>ll 9.total 16K 10.-rw-r--r--. 1 root root 3.0K Oct 31 08:39 passwd 11.-rw-r--r--. 2 root root 16 Nov 4 16:03 test1 12.-rw-r--r--. 1 root root 17 Nov 4 16:04 test2 13.-rw-r--r--. 2 root root 16 Nov 4 16:03 test.ln

1.更名 2.[root - gwx-st ~/test]#>ll 3.total 16K 4.-rw-r--r--. 1 root root 3.0K Oct 31 08:39 passwd 5.-rw-r--r--. 2 root root 16 Nov 4 16:03 test1 6.-rw-r--r--. 1 root root 17 Nov 4 16:04 test2 7.-rw-r--r--. 2 root root 16 Nov 4 16:03 test.ln 8.[root - gwx-st ~/test]#>mv test.ln test.mv 9.[root - gwx-st ~/test]#>ll 10.total 16K 11.-rw-r--r--. 1 root root 3.0K Oct 31 08:39 passwd 12.-rw-r--r--. 2 root root 16 Nov 4 16:03 test1 13.-rw-r--r--. 1 root root 17 Nov 4 16:04 test2 14.-rw-r--r--. 2 root root 16 Nov 4 16:03 test.mv 15.[root - gwx-st ~/test]#> 16.

rm

使用语法:rm [-dfirv][文件或目录...] 命令功能:删除文件或目录

常用选项及示例

-d或–directory  直接把欲删除的目录的硬连接数据删成0,删除该目录。 -f或–force  强制删除文件或目录。 -i或–interactive  删除既有文件或目录之前先询问用户。 -r或-R或–recursive  递归处理,将指定目录下的所有文件及子目录一并处理。 -v或–verbose  显示指令执行过程。

1.[root - gwx-st ~]#>ll -d test* 2.drwxr-xr-x. 2 root root 57 Nov 4 17:21 test/ 3.drwxr-xr-x. 2 gwx gwx 30 Nov 4 16:04 test1/ 4.[root - gwx-st ~]#>rm -dirv test1 5.rm: descend into directory 'test1'? y 6.rm: remove regular file 'test1/test1'? y 7.removed 'test1/test1' 8.rm: remove regular file 'test1/test2'? y 9.removed 'test1/test2' 10.rm: remove directory 'test1'? y 11.removed directory: 'test1' 12.[root - gwx-st ~]#>ll -d test* 13.drwxr-xr-x. 2 root root 57 Nov 4 17:21 test/

tee

使用语法:tee [-ai][文件...] 命令功能:读取标准输入的数据,并将其内容输出成文件

常用选项及示例

-a或–append  附加到既有文件的后面,而非覆盖它。 -i-i或–ignore-interrupts  忽略中断信号。

1.[root - gwx-st ~/test]#>cat test1 2.www 3.qqq 4.eee 5.[root - gwx-st ~/test]#>tee -ai test1 6.test 7.test 8.dd 9.dd 10.ss 11.ss 12.[root - gwx-st ~/test]#>cat test1 13.www 14.qqq 15.eee 16.test 17.dd 18.ss

touch

使用语法:touch [-acfm][-d <日期时间>][-r <参考文件或目录>][-t <日期时间>][文件或目录...]

或 touch [-acfm][日期时间][文件或目录...]

命令功能:改变文件或目录时间

默认情况下,如果指定的文件不存在的时候,touch命令会创建对应的文件

常用选项及示例 -t<日期时间>  使用指定的日期时间,而非现在的时间。 -a或–time=atime或–time=access或–time=use  只更改存取时间。

1.[root - gwx-st ~/test]#>ll -u test1 2.-rw-r--r--. 2 root root 24 Mar 11 2016 test1 3.[root - gwx-st ~/test]#>touch -at 201611041700.25 test1 4.[root - gwx-st ~/test]#>ll -u test1 5.-rw-r--r--. 2 root root 24 Nov 4 17:00 test1

-c或–no-create  不建立任何文件。

1.[root - gwx-st ~/test]#>ll 2.total 16K 3.-rw-r--r--. 1 root root 3.0K Oct 31 08:39 passwd 4.-rw-r--r--. 2 root root 24 Nov 4 17:42 test1 5.-rw-r--r--. 1 root root 9 Nov 4 17:35 test2 6.-rw-r--r--. 2 root root 24 Nov 4 17:42 test.mv 7.[root - gwx-st ~/test]#>touch -c gghgag 8.[root - gwx-st ~/test]#>ll 9.total 16K 10.-rw-r--r--. 1 root root 3.0K Oct 31 08:39 passwd 11.-rw-r--r--. 2 root root 24 Nov 4 17:42 test1 12.-rw-r--r--. 1 root root 9 Nov 4 17:35 test2 13.-rw-r--r--. 2 root root 24 Nov 4 17:42 test.mv

-m或–time=mtime或–time=modify  只更改变动时间。

1.[root - gwx-st ~/test]#>ll test1 2.-rw-r--r--. 2 root root 24 Nov 4 17:42 test1 3.[root - gwx-st ~/test]#>touch -mt 201611010000 test1 4.[root - gwx-st ~/test]#>ll test1 5.-rw-r--r--. 2 root root 24 Nov 1 00:00 test1

-r<参考文件或目录>  把指定文件或目录的日期时间,统统设成和参考文件或目录的日期时间相同。

1.[root - gwx-st ~/test]#>ll test1 test2 2.-rw-r--r--. 2 root root 24 Nov 1 00:00 test1 3.-rw-r--r--. 1 root root 9 Nov 4 17:35 test2 4.[root - gwx-st ~/test]#>touch -r test1 test2 5.[root - gwx-st ~/test]#>ll test1 test2 6.-rw-r--r--. 2 root root 24 Nov 1 00:00 test1 7.-rw-r--r--. 1 root root 9 Nov 1 00:00 test2

umask

使用语法:umask [-S][权限掩码] 命令功能:指定在建立文件时预设的权限掩码

注意:

umask设定的值在对象为文件的时候为被6减为设定的默认值,比如umask 077 ,则其权限为600;另外如果是只想对当前的一个操作指定与当前umask不等的设定时,可以使用以下方式实现:即umask只在子shell生效的效果

1. [root - gwx-st ~]#>ll umask 2. ls: cannot access umask: No such file or directory 3. [root - gwx-st ~]#>(umask 333;touch umask) 4. [root - gwx-st ~]#>ll umask 5. -r--r--r--. 1 root root 0 Nov 5 08:22 umask

常用选项及示例

-S  以文字的方式来表示权限掩码

1.[root - gwx-st ~]#>umask 2.0022 3.[root - gwx-st ~]#>umask -S 4.u=rwx,g=rx,o=rx

whereis

使用语法:whereis [-bfmsu][文件...] 命令功能:会在特定目录中查找符合条件的文件

常用选项及示例

-b  只查找二进制文件。

1.[root - gwx-st ~]#>whereis -b ls 2.ls: /usr/bin/ls 3.

which

使用语法:which [文件...] 命令功能:会在环境变量$PATH设置的目录里查找符合条件的文件

常用选项及示例

1.[root - gwx-st ~]#>which ls 2.ls is aliased to `ls -hF --color' 3.ls is /usr/bin/ls

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-11-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 马哥Linux运维 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档