Shell操作HDFS的文件系統命令詳解

Shell操作HDFS的文件系統命令詳解

前言:

如lunix系統一樣,對於HDFS的操作,也有一系列的命令,並且大多和lunix系統的命令差不多,本文對常用的命令進行介紹。

一、HDFS有以下Shell命令

[root@hadoop2 ~]# 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] [-d] <localsrc> ... <dst>]
	[-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] <path> ...]
	[-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>]
	[-createSnapshot <snapshotDir> [<snapshotName>]]
	[-deleteSnapshot <snapshotDir> <snapshotName>]
	[-df [-h] [<path> ...]]
	[-du [-s] [-h] [-x] <path> ...]
	[-expunge]
	[-find <path> ... <expression> ...]
	[-get [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-getfacl [-R] <path>]
	[-getfattr [-R] {-n name | -d} [-e en] <path>]
	[-getmerge [-nl] [-skip-empty-file] <src> <localdst>]
	[-help [cmd ...]]
	[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...]]
	[-mkdir [-p] <path> ...]
	[-moveFromLocal <localsrc> ... <dst>]
	[-moveToLocal <src> <localdst>]
	[-mv <src> ... <dst>]
	[-put [-f] [-p] [-l] [-d] <localsrc> ... <dst>]
	[-renameSnapshot <snapshotDir> <oldName> <newName>]
	[-rm [-f] [-r|-R] [-skipTrash] [-safely] <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> ...]
	[-truncate [-w] <length> <path> ...]
	[-usage [cmd ...]]

Generic options supported are:
-conf <configuration file>        specify an application configuration file
-D <property=value>               define a value for a given property
-fs <file:///|hdfs://namenode:port> specify default filesystem URL to use, overrides 'fs.defaultFS' property from configurations.
-jt <local|resourcemanager:port>  specify a ResourceManager
-files <file1,...>                specify a comma-separated list of files to be copied to the map reduce cluster
-libjars <jar1,...>               specify a comma-separated list of jar files to be included in the classpath
-archives <archive1,...>          specify a comma-separated list of archives to be unarchived on the compute machines

The general command line syntax is:
command [genericOptions] [commandOptions]

二、重點挑一些常用的命令進行介紹

如-cat ;-ls;-rm這些命令應該都知道

1、-appendToFile將一個文件的內容追加到hdfs中的一個文件中
hdfs dfs -appendToFile test.txt /test.txt

2、-checksum 獲取文件的校驗值(由MD5生成的字符串),用於檢驗文件的完整性
hdfs dfs -checksum /test.txt

3、-chgrp 修改文件的【組】

4、chmod 修改文件權限
hdfs dfs -chmod g+w /test.txt

目錄文件權限的查看和修改博文:
https://blog.51cto.com/zhaoyuqiang/1214718

5、-chown修改文件的【用戶】
hdfs dfs -chown sun /test.txt

6、-copyFromLocal將本地文件拷貝到hdfs上
hdfs dfs -copyFromLocal test.txt /bb.log

7、-copyToLocal從hdfs上拷貝文件到本地lunix系統
hdfs dfs -copyToLocal /test.txt /root/aa.xml

8、-count對目錄下的文件個數進行統計
hdfs dfs -count /

9、-cp 在hdfs上相互拷貝文件
hdfs dfs -cp /test.txt /cc.xml

10、-du 顯示文件所佔的磁盤大小
hdfs dfs -du -h /

11、-find查看目錄下文件(-name根據文件名進行查找)
hdfs dfs -find / -name "test.txt"

12、-get從hdfs上下載文件
hdfs dfs -get /cc.xml /root/mm.log

13、-mkdir創建hdfs目錄(-p代表創建多級目錄)
hdfs dfs -mkdir -p /aa/bb

14、-mv在hdfs上移動目錄
hdfs dfs -mv /test.txt /aa/bb

15、-rm刪除hdfs上文件, -rm –r 遞歸刪除多級目錄,
-rm –r –f 強制遞歸刪除多級目錄
hdfs dfs -rm /cc.xml
hdfs dfs -rm -r /aa/bb
hdfs dfs -rm -r -f /aa/bb
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章