HDFS Shell

1. HDFS Shell

1.1. 查看所有命令的用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
$ hdfs dfs
Usage: hadoop fs [generic options]
[-appendToFile <localsrc> ... <dst>]
[-cat [-ignoreCrc] <src> ...]
[-checksum <src> ...]
[-chgrp [-R] GROUP PATH...]
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-copyFromLocal [-f] [-p] [-l] <localsrc> ... <dst>]
[-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-count [-q] [-h] <path> ...]
[-cp [-f] [-p | -p[topax]] <src> ... <dst>]
[-createSnapshot <snapshotDir> [<snapshotName>]]
[-deleteSnapshot <snapshotDir> <snapshotName>]
[-df [-h] [<path> ...]]
[-du [-s] [-h] <path> ...]
[-expunge]
[-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-getfacl [-R] <path>]
[-getfattr [-R] {-n name | -d} [-e en] <path>]
[-getmerge [-nl] <src> <localdst>]
[-help [cmd ...]]
[-ls [-d] [-h] [-R] [<path> ...]]
[-mkdir [-p] <path> ...]
[-moveFromLocal <localsrc> ... <dst>]
[-moveToLocal <src> <localdst>]
[-mv <src> ... <dst>]
[-put [-f] [-p] [-l] <localsrc> ... <dst>]
[-renameSnapshot <snapshotDir> <oldName> <newName>]
[-rm [-f] [-r|-R] [-skipTrash] <src> ...]
[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
[-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]]
[-setfattr {-n name [-v value] | -x name} <path>]
[-setrep [-R] [-w] <rep> <path> ...]
[-stat [format] <path> ...]
[-tail [-f] <file>]
[-test -[defsz] <path>]
[-text [-ignoreCrc] <src> ...]
[-touchz <path> ...]
[-usage [cmd ...]]

Generic options supported are
-conf <configuration file> specify an application configuration file
-D <property=value> use value for given property
-fs <local|namenode:port> specify a namenode
-jt <local|resourcemanager:port> specify a ResourceManager
-files <comma separated list of files> specify comma separated files to be copied to the map reduce cluster
-libjars <comma separated list of jars> specify comma separated jar files to include in the classpath.
-archives <comma separated list of archives> specify comma separated archives to be unarchived on the compute machines.

The general command line syntax is
bin/hadoop command [genericOptions] [commandOptions]

1.2. help 查看某一命令的具体用法

1
2
hdfs dfs -help get   # 查看get的用法
hdfs dfs -help put # 查看put的用法

1.3. ls 目录浏览

用法:-ls [-d] [-h] [-R] [<path> ...]

选项 描述
-d 查看目录本身信息
-h 显示文件大小单位便于阅读(byte单位省略)
-R 递归显示
1
2
3
4
5
6
7
8
9
10
11
12
13
$ hdfs dfs -ls /     # 列出目录
Found 2 items
-rw-r--r-- 3 root supergroup 199635269 2019-02-23 13:35 /hadoop-2.6.5.tar.gz
drwxr-xr-x - root supergroup 0 2019-02-23 13:03 /input

$ hdfs dfs -ls -d / # 列出目录本身
drwxr-xr-x - root supergroup 0 2019-02-23 13:35 /

$ hdfs dfs -ls -R / # 递归查看根目录
-rw-r--r-- 3 root supergroup 199635269 2019-02-23 13:35 /hadoop-2.6.5.tar.gz
drwxr-xr-x - root supergroup 0 2019-02-23 13:03 /input
-rw-r--r-- 3 root supergroup 280 2019-02-23 13:03 /input/1.txt
-rw-r--r-- 3 root supergroup 58 2019-02-23 13:03 /input/2.txt

1.4. mkdir 创建目录

用法:-mkdir [-p] <path> ...

选项 描述
-p 递归创建
1
2
3
hdfs dfs -mkdir /d1
hdfs dfs -mkdir /d1 /d2 # 创建多个目录
hdfs dfs -mkdir -p /d1/d2

1.5. moveFromLocal 从本地移动到HDFS

用法:-moveFromLocal <localsrc> ... <dst>

说明:与put类似,区别在于moveFromLocal会删除本地文件

1
2
hdfs dfs -moveFromLocal 1.txt /d1  # 移动文件
hdfs dfs -moveFromLocal dir1 /d1 # 移动目录

1.6. cat 查看HDFS的文件

用法:-cat [-ignoreCrc] <src> ...

1
2
hdfs dfs -cat /input/1.txt  # 查看文件
hdfs dfs -cat /input/1.txt /input/2.txt # 查看多个文件

1.7. appendToFile 追加文件到已存在文件末尾

用法:-appendToFile <localsrc> ... <dst>

说明:<localsrc>的值如果是 -,表示从stdin读取输入

1
2
3
4
5
$ hdfs dfs -appendToFile 2.txt /1.txt  # 2.txt追加到1.txt
$ hdfs dfs -appendToFile 2.txt 3.txt /1.txt # 2.txt、3.txt追加到1.txt
$ hdfs dfs -appendToFile - /1.txt # 将控制台的输入追加到1.txt
hello world
^D # CTRL+D结束输入

1.8. chgrp 修改所有组

用法:-chgrp [-R] GROUP PATH...

1
2
3
hdfs dfs -chgrp group1 /1.txt
hdfs dfs -chgrp group1 /dir1
hdfs dfs -chgrp -R group1 /dir1 # 递归修改

1.9. chown 修改所有者和所有组

用法:-chown [-R] [OWNER][:[GROUP]] PATH...

1
2
3
4
hdfs dfs -chown user1 /1.txt
hdfs dfs -chown user1:group1 /1.txt
hdfs dfs -chown user1:group1 /dir1
hdfs dfs -chown -R user1:group1 /dir1 # 递归修改

1.10. chmod 修改权限

用法:-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH..

1
2
3
4
5
hdfs dfs -chmod a+x /1.txt
hdfs dfs -chmod +x /1.txt # 权限+x 即 a+x
hdfs dfs -chmod a-x /1.txt
hdfs dfs -chmod -x /1.txt # 权限-x 即 a-x
hdfs dfs -chmod -R 755 / # -R 递归修改

1.11. put / copyFromLocal 上传本地文件到HDFS

用法:-copyFromLocal [-f] [-p] [-l] <localsrc> ... <dst>-put [-f] [-p] [-l] <localsrc> ... <dst>

选项 描述
-p 保留源文件的access时间戳、modification时间戳、所有者、所有组、和文件权限
-f 如果目标文件已存在,则覆盖
-l 允许DataNode懒惰持久化到磁盘(lazily persist),强制设置副本因子为1。该选项会导致可用性降低,请谨慎使用
1
2
3
4
hdfs dfs -put 1.txt /d1   # 上传1.txt
hdfs dfs -put 1.txt 2.txt /d1 # 上传1.txt和2.txt
hdfs dfs -put *.txt /d1 # 上传所有txt文件
hdfs dfs -put d1 / # 上传整个d1目录

1.12. get / copyToLocal 下载文件到本地

用法:-put [-f] [-p] [-l] <localsrc> ... <dst>-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>

选项 描述
-p 保留源文件的access时间戳、modification时间戳、所有者、所有组、和文件权限
1
2
3
hdfs dfs -get /1.txt ~     # 下载1.txt
hdfs dfs -get /1.txt /2.txt ~ # 下载1.txt和2.txt
hdfs dfs -get /*.txt ~ # 下载所有的txt文件

1.13. cp 从HDFS一个路径拷贝到HDFS的另一个路径

用法:-cp [-f] [-p | -p[topax]] <src> ... <dst>

选项 描述
-p all of the source and target pathnames are in the /.reserved/raw hierarchy. raw namespace xattr preservation is determined solely by the presence (or absence) of the /.reserved/raw prefix and not by the -p option
-f 如果目标文件已存在,则覆盖
1
2
3
4
5
6
7
8
$ hdfs dfs -ls -R /
drwxr-xr-x - root supergroup 0 2018-04-08 23:42 /d1
-rw-r--r-- 1 root supergroup 6 2018-04-08 23:16 /d1/1.txt
drwxr-xr-x - root supergroup 0 2018-04-08 23:42 /d2

hdfs dfs -cp /d1/1.txt /d2 # 将1.txt拷贝到/d2目录下
hdfs dfs -cp /d1/1.txt /dir100 # 不存在dir100目录,所以将1.txt拷贝到根目录下,并命名为"/dir100"
hdfs dfs -cp /d1 /d3 # 可以递归复制整个目录

1.14. getmerge 合并下载多个文件到本地

用法:-getmerge [-nl] <src> <localdst>

选项 描述
-nl 合并时,在每个文件之后添加一个空行
-f 如果目标文件已存在,则覆盖
1
2
hdfs dfs -getmerge /*.txt merge.txt   # 下载合并txt到本地,命名为merge.txt
hdfs dfs -getmerge /1.txt /2.txt /3.txt merge.txt # 下载下载1.txt、2.txt、3.txt

1.15. tail 查看文件末尾1KB数据

用法:-tail [-f] <file>

注意:是查看末尾1KB数据,而不是最后几行

选项 描述
-f 监视文件末尾
1
2
hdfs dfs -tail /1.txt
hdfs dfs -tail -f /1.txt

1.16. rm 删除文件

用法:-rm [-f] [-r|-R] [-skipTrash] <src> ...

选项 描述
-f 若目标文件不存在,不显示提示信息
-r 或 -R 递归删除
1
2
3
4
hadoop fs -rm /1.txt   # 删除1.txt
hadoop fs -rm /1.txt /2.txt # 删除1.txt和2.txt
hadoop fs -rm -r -f /d1 # 删除目录d1
hadoop fs -rm -r -f /'*' # 删除根目录下的一切文件。星号作为参数传入,所以加引号防止星号被转义

1.17. rmdir 删除空目录

用法:-rmdir [--ignore-fail-on-non-empty] <dir>

1
hdfs dfs -rmdir /d1

1.18. rmr 递归删除

用法:rmr等价于 rm -r

1.19. mv 从HDFS一个路径移动到HDFS的另一个路径

用法:-mv <src> ... <dst>

1
2
hadoop fs -mv /d1 /d2  # 更名
hadoop fs -mv /*.txt /d3 # 将所有txt文件移动到d3目录下

1.20. du 统计目录各文件大小

用法:-du [-s] [-h] <path> ...

选项 描述
-s 统计目录总大小
-h 显示文件大小单位便于阅读(byte单位省略)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ hdfs dfs -du /   # 统计目录下各文件大小
115 /1.txt
3 /2.txt
3 /3.txt
199635269 /hadoop-2.6.5.tar.gz
338 /input

$ hdfs dfs -du -h /
115 /1.txt
3 /2.txt
3 /3.txt
190.4 M /hadoop-2.6.5.tar.gz
338 /input

$ hdfs dfs -du -s /
199635728 /

$ hdfs dfs -du -s -h /
190.4 M /

1.21. setrep 修改文件的数据块副本数

用法:-setrep [-R] [-w] <rep> <path> ...

选项 描述
-w It requests that the command waits for the replication to complete. This can potentially take a very long time.
-R It is accepted for backwards compatibility. It has no effect.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ hdfs dfs -ls /1.txt
-rw-r--r-- 3 root root 115 2019-02-23 23:42 /1.txt # 现在有3个副本,分别保存在3个DataNode上

$ hdfs dfs -setrep 2 /1.txt # 修改为2个副本。其中1个DataNode上的副本会被删除
Replication 2 set: /1.txt

$ hdfs dfs -ls /1.txt
-rw-r--r-- 2 root root 115 2019-02-23 23:42 /1.txt # 只有2个副本

$ hdfs dfs -setrep 10 /1.txt # 修改为10个副本。但是DataNode只有3个,所以目前副本数恢复到3个。直到只少动态添加7个DataNode,副本才会达到10个
Replication 10 set: /1.txt

$ hdfs dfs -ls /1.txt
-rw-r--r-- 10 root root 115 2019-02-23 23:42 /1.txt

1.22. touchz 创建空文件

用法:-touchz <path> ...

1
2
hdfs dfs -touchz /file1
hdfs dfs -touchz /file2 /file3
panchaoxin wechat
关注我的公众号
支持一下