本文已参与「新人创作礼」活动,一起开启掘金创作之路。
在此要注意的是,在HDFS中执行命令时必须指定绝对路径。
- 创建目录
[root@hadoop01 ~]# hdfs dfs -mkdir /test0315
- 创建多级目录(会将该路径中不存在的目录全部创建出来)
[root@hadoop01 ~]# hdfs dfs -mkdir -p /a/b/c/d
- 将本地文件上传到hdfs指定目录下
[root@hadoop01 ~]# hdfs dfs -put testupload /test0315
- 使用通配符上传多个文件到hdfs指定目录下
[root@hadoop01 ~]# hdfs dfs -put demo* /test0315
#参数一表示本地文件,参数二表示hdfs中的目录
- 将本地的指定目录上传到hdfs指定目录下
[root@hadoop01 ~]# hdfs dfs -put localdir/ /test0315
#参数一表示本地目录,参数二表示hdfs中的目录
- 在hdfs上创建空文件
[root@hadoop01 ~]# hdfs dfs -touchz /test0315/emptyfile
#这里只有一个参数,就是要创建的空文件的绝对路径
- 将本地某文件中的内容追加到hdfs的某个文件中
[root@hadoop01 ~]# hdfs dfs -appendToFile testupload /test0315/emptyfile
#参数一表示本地文件,参数二表示hdfs中文件路径
- 将hdfs中的文件下载到本地指定路径下
[root@hadoop01 ~]# hdfs dfs -get /test0315/emptyfile ./
#参数一表示hdfs中文件路径,参数二表示本地目录
或
[root@hadoop01 ~]# hdfs dfs -copyToLocal /test0315 ./
#参数一表示hdfs中的目录,参数二表示本地路径
- 将hdfs中的多个文件内容合并下载到本地指定文件中
[root@hadoop01 ~]# hdfs dfs -getmerge /test0315/file* ./file
#参数一为hdfs中的多个文件,参数二为下载到的本地文件
- 将hdfs中的文件/目录移动到其他目录下
[root@hadoop01 ~]# hdfs dfs -mv /test0315/file* /test0315/testfiledir
#参数一为要移动的文件,参数二为要移动到的目标目录
- 重命名
[root@hadoop01 ~]# hdfs dfs -mv /test0315/localdir /test0315/hdfsdir
#参数一为重命名前的文件/目录名,参数二为重命名后的文件/目录名
- 将hdfs下的某文件/目录拷贝到另一路径下
[root@hadoop01 ~]# hdfs dfs -cp /test0315/demo* /test0315/hdfsdir
#参数一为要拷贝的文件/目录,参数二为要拷贝到的目录
- 删除文件
[root@hadoop01 ~]# hdfs dfs -rm /test0315/emptyfile
- 删除目录
[root@hadoop01 ~]# hdfs dfs -rm -r /a/b/c
- 查看hdfs中磁盘使用情况
[root@hadoop01 ~]# hdfs dfs -df -h /test0315
- 查看hdfs中某文件/目录的磁盘使用情况
[root@hadoop01 ~]# hdfs dfs -du -h /
- 修改hdfs中目录的权限
[root@hadoop01 ~]# hdfs dfs -chmod 777 /test0315
- 修改hdfs中目录的用户组及所有者
[root@hadoop01 ~]# hdfs dfs -chown test:test /a
- 修改hdfs中文件副本数(默认是3)
[root@hadoop01 ~]# hdfs dfs -setrep 4 /test0315/demo*
- 查看文件的统计信息
[root@hadoop01 ~]# hdfs dfs -stat name:%n,size:%b /test0315/testupload
# %n表示文件名,%b表示文件大小,后面的参数表示获取哪个文件的统计信息
- 监测文件是否存在
[root@hadoop01 ~]# hdfs dfs -test -e /test0315/testupload && echo "file exists" || echo "file not exists"
#-e表示文件是否存在,后面的路径表示要检测的路径,&&后面的表示如果文件存在要执行的操作,||表示文件不存在时要执行的操作