HDFS 命令大全

概要

用户命令
dfs 命令
追加文件内容
查看文件内容
得到文件的校验信息
修改用户组
修改文件权限
修改文件所属用户
本地拷贝到 hdfs
hdfs 拷贝到本地
获取目录,文件数量及大小
hdfs 内拷贝
createSnapshot 创建快照
deleteSnapshot 删除快照
显示空闲空间
显示文件和目录大小
清空回收站
查找文件
显示目录或文件的访问控制列表
显示文件或目录的扩展信息
ls
mkdir
moveFromLocal
moveToLocal
mv
rm
test
tail
usage
参考文档

s
概要
HDFS 文件系统的常用操作和普通 Linux 文件系统非常相似,如读取文件,新建目录,移动文件,删除数据,列出目录等。

本文只列出常用的 HDFS 命名,要查看完整的命令行列表,可点击 这里

所有的 HDFS 命令都是调用 bin/hdfs 脚本完成的,它的基本用法如下

hdfs [SHELL_OPTIONS] COMMAND [GENERIC_OPTIONS] [COMMAND_OPTIONS]
hdfs 命令分成 3 种类型,分别是

用户命令
集群管理员命令
调试命令(集群管理员使用)
在命令中会有一些 URI 参数,其中 URI 的前缀是由 core-site.xml 中的 fs.defaultFS 决定的,通过该属性就可以知道 namenode 在哪里运行进而连接到它。例如:

fs.defaultFS hdfs://hadoop2cluster 而如果在本机操作,就可以省略 URI 前缀。

用户命令
dfs 命令
hdfs dfs [COMMAND [COMMAND_OPTIONS]]
hadoop 所支持的文件系统命令,其中 COMMAND_OPTIONS 可在 File System Shell Guide 查看。

hdfs dfs 命令前缀与 hadoop fs 效果相同。

追加文件内容
-appendToFile …
实例

hdfs dfs -appendToFile hdfs-site.xml /tmp/tests/test.txt
查看文件内容
-cat URI [URI …]
实例

hdfs dfs -cat /tmp/tests/test.txt
得到文件的校验信息
-checksum URI
实例

hdfs dfs -checksum /tmp/tests/test.txt
修改用户组
-chgrp [-R] GROUP URI [URI …]
Change group association of files. The user must be the owner of files, or else a super-user. Additional information is in the Permissions Guide.

修改文件权限
-chmod [-R] <MODE[,MODE]… | OCTALMODE> URI [URI …]
修改文件所属用户
-chown [-R] [OWNER][:[GROUP]] URI [URI ]
本地拷贝到 hdfs
-copyFromLocal URI
hdfs 拷贝到本地
-copyToLocal [-ignorecrc] [-crc] URI
获取目录,文件数量及大小
-count [-q] [-h] [-v]
举例

hdfs dfs -count /tmp/tests/
得到结果

       1            1               3513 /tmp/tests

其中

列 1 表示 目录数量
列 2 表示 文件数量
列 3 表示 文件总大小(字节)
hdfs 内拷贝
-cp [-f] [-p | -p[topax]] URI [URI …]
-f 表示如果文件已存在,会覆盖原文件
-p 表示会保留文件属性(时间错,owner,权限, ACL 等)
createSnapshot 创建快照
See HDFS Snapshots Guide.

deleteSnapshot 删除快照
See HDFS Snapshots Guide.

显示空闲空间
-df [-h] URI [URI …]
-h 选项会以人类友好的方式来显示,如 64M 而不是多少字节
显示文件和目录大小
-du [-s] [-h] URI [URI …]
The -s option will result in an aggregate summary of file lengths being displayed, rather than the individual files.
The -h option will format file sizes in a “human-readable” fashion (e.g 64.0m instead of 67108864)
清空回收站
-expunge
Empty the Trash. Refer to the HDFS Architecture Guide for more information on the Trash feature.

查找文件
-find … …
显示目录或文件的访问控制列表
-getfacl [-R]
显示文件或目录的扩展信息
-getfattr [-R] -n name | -d [-e en]
-R: Recursively list the attributes for all files and directories.
-n name: Dump the named extended attribute value.
-d: Dump all extended attribute values associated with pathname.
-e encoding: Encode values after retrieving them. Valid encodings are “text”, “hex”, and “base64”. Values encoded as text strings are enclosed in double quotes ("), and values encoded as hexadecimal and base64 are prefixed with 0x and 0s, respectively.
path: The file or directory.
ls
-ls [-d] [-h] [-R] [-t] [-S] [-r] [-u]
-d: Directories are listed as plain files.
-h: Format file sizes in a human-readable fashion (eg 64.0m instead of 67108864).
-R: Recursively list subdirectories encountered.
-t: Sort output by modification time (most recent first).
-S: Sort output by file size.
-r: Reverse the sort order.
-u: Use access time rather than modification time for display and sorting.
mkdir
-mkdir [-p]
moveFromLocal
-moveFromLocal
moveToLocal
-moveToLocal [-crc]
mv
-mv URI [URI …]
rm
-rm [-f] [-r |-R] [-skipTrash] URI [URI …]
The -f option will not display a diagnostic message or modify the exit status to reflect an error if the file does not exist.
The -R option deletes the directory and any content under it recursively.
The -r option is equivalent to -R.
The -skipTrash option will bypass trash, if enabled, and delete the specified file(s) immediately. This can be useful when it is necessary to delete files from an over-quota directory.
test
-test -[defsz] URI
-d: f the path is a directory, return 0.
-e: if the path exists, return 0.
-f: if the path is a file, return 0.
-s: if the path is not empty, return 0.
-z: if the file is zero length, return 0.
tail
-tail [-f] URI
显示文件的最后的字节

The -f option will output appended data as the file grows, as in Unix.
usage
显示命令的用法

-usage command

文件复制/位置迁移:
hadoop distcp /station/user /station/data

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值