Linux下HDFS常用文件操作命令

以下命令均需要在hadoop目录下运行:即当前所在目录下有bin目录,bin目录中有hdfs这个二进制文件。

1ls

bin/hdfs dfs -ls /
列出hdfs文件系统根目录下的目录和文件

bin/hdfs dfs -ls -R /
列出hdfs文件系统所有的目录和文件


2put

bin/hdfs dfs -put < local file > < hdfs file >
hdfs file的父目录一定要存在,否则命令不会执行

bin/hdfs dfs -put < local file or dir >…< hdfs dir >
hdfs dir 一定要存在,否则命令不会执行

bin/hdfs dfs -put < hdsf file>
从键盘读取输入到hdfs file中,按Ctrl+D结束输入,hdfs file不能存在,否则命令不会执行。默认上传到hdfs文件系统的家目录(/user/hadoop)下


2.1moveFromLocal

bin/hdfs dfs -moveFromLocal < local src > … < hdfs dst >
与put相类似,命令执行后源文件 local src 被删除,也可以从从键盘读取输入到hdfs file中


2.2copyFromLocal

bin/hdfs dfs -copyFromLocal < local src > … < hdfs dst >
与put相类似,也可以从从键盘读取输入到hdfs file中


3get

bin/hdfs dfs -get < hdfs file > < local file or dir>
local file不能和 hdfs file名字不能相同,否则会提示文件已存在,没有重名的文件会复制到本地

bin/hdfs dfs -get < hdfs file or dir > … < local dir >
拷贝多个文件或目录到本地时,本地要为文件夹路径
注意:如果用户不是root, local 路径要为用户文件夹下的路径,否则会出现权限问题,


3.1moveToLocal

当前版本中还未实现此命令


3.2copyToLocal

bin/hdfs dfs -copyToLocal < local src > … < hdfs dst >
与get相类似


4rm

bin/hdfs dfs -rm < hdfs file > …
bin/hdfs dfs -rm -r < hdfs dir>…
每次可以删除多个文件或目录


5mkdir

bin/hdfs dfs -mkdir < hdfs path>
只能一级一级的建目录,父目录不存在的话使用这个命令会报错

bin/hdfs dfs -mkdir -p < hdfs path>
所创建的目录如果父目录不存在就创建该父目录


6getmerge

bin/hdfs dfs -getmerge < hdfs dir > < local file >
将hdfs指定目录下所有文件排序后合并到local指定的文件中,文件不存在时会自动创建,文件存在时会覆盖里面的内容

bin/hdfs dfs -getmerge -nl < hdfs dir > < local file >
加上nl后,合并到local file中的hdfs文件之间会空出一行


7cp

bin/hdfs dfs -cp < hdfs file > < hdfs file >
目标文件不能存在,否则命令不能执行,相当于给文件重命名并保存,源文件还存在

bin/hdfs dfs -cp < hdfs file or dir >… < hdfs dir >
目标文件夹要存在,否则命令不能执行


8mv

bin/hdfs dfs -mv < hdfs file > < hdfs file >
目标文件不能存在,否则命令不能执行,相当于给文件重命名并保存,源文件不存在

bin/hdfs dfs -mv < hdfs file or dir >… < hdfs dir >
源路径有多个时,目标路径必须为目录,且必须存在。
注意:跨文件系统的移动(local到hdfs或者反过来)都是不允许的


9count

bin/hdfs dfs -count < hdfs path >
统计hdfs对应路径下的目录个数,文件个数,文件总计大小
显示为目录个数,文件个数,文件总计大小,输入路径


10du

bin/hdfs dfs -du < hdsf path>
显示hdfs对应路径下每个文件夹和文件的大小

bin/hdfs dfs -du -s < hdsf path>
显示hdfs对应路径下所有文件和的大小

bin/hdfs dfs -du - h < hdsf path>
显示hdfs对应路径下每个文件夹和文件的大小,文件的大小用方便阅读的形式表示,例如用64M代替67108864


11text

bin/hdfs dfs -text < hdsf file>
将文本文件或某些格式的非文本文件通过文本格式输出


12setrep

bin/hdfs dfs -setrep -R 3 < hdfs path >
改变一个文件在hdfs中的副本个数,上述命令中数字3为所设置的副本个数,-R选项可以对一个人目录下的所有目录+文件递归执行改变副本个数的操作


13stat

bin/hdfs dfs -stat [format] < hdfs path >
返回对应路径的状态信息
[format]可选参数有:%b(文件大小),%o(Block大小),%n(文件名),%r(副本个数),%y(最后一次修改日期和时间)
可以这样书写bin/hdfs dfs -stat %b%o%n < hdfs path >,不过不建议,这样每个字符输出的结果不是太容易分清楚


14tail

bin/hdfs dfs -tail < hdfs file >
在标准输出中显示文件末尾的1KB数据


15dfsadmin

bin/hdfs dfs dfsadmin -help
管理员可以通过dfsadmin管理HDFS,用法可以通过上述命令查看

bin/hdfs dfs dfsadmin -report
显示文件系统的基本数据

bin/hdfs dfs dfsadmin -safemode < enter | leave | get | wait >
enter:进入安全模式;leave:离开安全模式;get:获知是否开启安全模式;
wait:等待离开安全模式


16、distcp

用来在两个HDFS之间拷贝数据

猜你喜欢

转载自blog.csdn.net/qq_42303254/article/details/89162624