hadoop之 HDFS fs 命令總結

張衝andy發表於2017-08-28

版本:Hadoop 2.7.4

-- 檢視hadoop fs幫助資訊
[root@hadp-master sbin]# hadoop fs
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]
[-find <path> ... <expression> ...]
[-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> ...]
[-truncate [-w] <length> <path> ...]
[-usage [cmd ...]]

 

注意:

以下指令均是在,Linux 命令列視窗介面操作。
[ ]表示可選引數,<>表示必須引數。
開始使用命令前,必須啟動Hadoop
(1)-appendToFile

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

作用:是將一個或者多個檔案新增到HDFS系統中。

示例:

hadoop fs -appendToFile localfile /user/hadoop/hadoopfile
hadoop fs -appendToFile localfile1 localfile2 /user/hadoop/hadoopfile
hadoop fs -appendToFile localfile hdfs://nn.example.com/hadoop/hadoopfile
hadoop fs -appendToFile - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin.
  
(2)-cat

用法:hadoop fs -cat URI [URI ...]

作用:檢視檔案內容(可以檢視本地和HDFS上的內容)。

示例:

hadoop fs -cat hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
hadoop fs -cat file:///file3 /user/hadoop/file4  

(3)-checksum

用法: hadoop fs -checksum URI

作用:檢視校驗碼資訊。(例子顯示了MD5)

示例:

hadoop fs -checksum hdfs://nn1.example.com/file1
hadoop fs -checksum file:///etc/hosts
  
(4)-chgrp

用法: hadoop fs -chgrp [-R] GROUP URI [URI ...]

作用:改變檔案所屬的組。(Change group association of files.)

使用-R 將使改變在目錄結構下遞迴進行。

(5)-chmod

作用:改變檔案訪問許可權。

用法:hadoop fs -chmod [-R] <MODE[,MODE]... | OCTALMODE> URI [URI ...]

這裡可以參考 Linux下檔案系統的chmod的用法,基本類似。

(6)-chown

作用:hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]

用法:改變檔案的所有者。使用-R 將使改變在目錄結構下遞迴進行。命令的使用者必須是超級使用者。

(7)-copyFromLocal

用法:hadoop fs -copyFromLocal <localsrc> URI

作用:類似於put命令,和put不同的是,複製的源地址必須是本地檔案地址。

-f 引數 當複製的目標檔案存在時,進行覆蓋。

示例:

[root@two1 fanrui]# hadoop fs -copyFromLocal testFlatMap.txt /1.txt
copyFromLocal: `/1.txt': File exists
  
這個時候加上-f引數。即可覆蓋。

[root@two1 fanrui]# hadoop fs -copyFromLocal -f testFlatMap.txt /1.txt

(8)-copyToLocal

用法: hadoop fs -copyToLocal [-ignorecrc] [-crc] URI <localdst>

作用:類似於get指令。和get不同的是,複製的目的地址必須是本地檔案地址。

(9)-count

作用:計算paths下的目錄數,檔案數和位元組數。

用法: hadoop fs -count [-q] [-h] [-v] <paths>

hadoop fs -count hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
hadoop fs -count -q hdfs://nn1.example.com/file1
hadoop fs -count -q -h hdfs://nn1.example.com/file1
hdfs dfs -count -q -h -v hdfs://nn1.example.com/file1
  
(10)-cp
用法:hadoop fs -cp [-f] [-p | -p[topax]] URI [URI ...] <dest>
作用:複製,HDFS檔案系統中進行的複製操作。
-f 引數選項:當檔案存在時,進行覆蓋。
-p 引數選項:將許可權、所屬組、時間戳、ACL以及XAttr等也進行複製。下面是官網的描述。

The -p option will preserve file attributes [topx] (timestamps, ownership, permission, ACL, XAttr). If -p is specified with no arg, then preserves timestamps, ownership, permission. If -pa is specified, then preserves permission also because ACL is a super-set of permission. Determination of whether raw namespace extended attributes are preserved is independent of the -p flag.

示例:

[root@two1 fanrui]# hadoop fs -cp -p /tmp/fan /tmp/fan1
  
(11)-df
用法:hadoop fs -df [-h] URI [URI ...]
作用:顯示剩餘空間。
示例:

[root@two1 fanrui]# hadoop fs -df /
Filesystem Size Used Available Use%
hdfs://localhost:9000 37626667008 311296 24792702976 0%

(12)-dus
作用:顯示檔案長度概要。該方法已經被捨去,等價於 -du -s 方法。見(11)

(13)-expunge
作用:從垃圾桶目錄永久刪除超過保留閾值的檢查點中的檔案,並建立新檢查點。

用法:hadoop fs -expunge

(14)-find
作用:查詢滿足表示式的檔案和資料夾。沒有配置path的話,預設的就是全部目錄/;如果表示式沒有配置,則預設為-print。
用法: hadoop fs -find <path> ... <expression> ...
-name pattern 所要查詢檔案的檔名。
-iname pattern 所要查詢的檔名,不區分大小寫。
-print 列印。
-print0 列印在一行,如下圖所示。

示例:

hadoop fs -find / -name test -print
  
(15)-get
作用:從HDFS上複製檔案到本地。
用法:hadoop fs -get [-ignorecrc] [-crc] <src> <localdst>
示例:

hadoop fs -get /user/hadoop/file localfile
hadoop fs -get hdfs://nn.example.com/user/hadoop/file localfile

(16)getfacl

作用:顯示檔案和資料夾的ACLs(Access Control Lists)。如果目錄有預設的ACL,則顯示之。
-R引數:遞迴顯示。
用法:

hadoop fs -getfacl [-R] <path>

Options:

-R: List the ACLs of all files and directories recursively.
path: File or directory to list.
示例:

hadoop fs -getfacl /file
hadoop fs -getfacl -R /dir
  
Exit Code:

Returns 0 on success and non-zero on error.

(17)getfattr

作用:顯示檔案或目錄的擴充套件屬性名和值(如果有的話)

用法:hadoop fs -getfattr [-R] -n name | -d [-e en] <path>

Options:

-R:遞迴顯示資料夾和檔案。
-n name:轉儲命名的擴充套件屬性值。
-d:轉儲與路徑名相關聯的所有擴充套件屬性值。
-e en: 檢索後的值進行編碼。 有效的編碼是 “text”, “hex”, and “base64”. 值編碼作為文字字串是用雙引號括起來的(“),值編碼作為16進位制和64進位制,字首分別為 0x 和 0s。
path:檔案或資料夾路徑。

示例:

hadoop fs -getfattr -d /file
hadoop fs -getfattr -R -n user.myAttr /dir
  
(18)-getmerge
作用:是將HDFS上一個目錄中所有的檔案合併到一起輸出到一個本地檔案上。
用法:hadoop fs -getmerge [-nl] <src> <localdst>

示例:

hadoop fs -getmerge -nl /src /opt/output.txt
hadoop fs -getmerge -nl /src/file1.txt /src/file2.txt /output.txt
  
(19)-help
作用:幫助文件

用法:hadoop fs -help

(20)-ls
作用:檢視檔案,與linux下ls命令基本類似。
用法:hadoop fs -ls [-d] [-h] [-R] <args>
選項:
-d:只展示查詢展示目錄;

-h:顯示為人眼更易識別的單位(原來是位元組)。

-R:遞迴展示,顯示所有的資料夾及檔案

示例:

hadoop fs -ls -d /
hadoop fs -ls -h /
hadoop fs -ls -R /
  
-lsr
作用:已經被捨去,效果等同於-ls -R

(21)-mkdir
作用:建立資料夾。

用法:hadoop fs -mkdir [-p] <paths>
選項:
-p:建立父目錄。類似於Unix的mkdir -p命令。
示例:

hadoop fs -mkdir /user/hadoop/dir1 /user/hadoop/dir2
hadoop fs -mkdir hdfs://nn1.example.com/user/hadoop/dir hdfs://nn2.example.com/user/hadoop/dir
  
(22)-moveFromLocal
用法:hadoop fs -moveFromLocal <localsrc> <dst>
作用:類似於put命令,不同put命令的是,該操作是移動(意思就是localsrc將被刪除)。localsrc應是本地檔案。

(23)-moveToLocal
用法:hadoop fs -moveToLocal [-crc] <src> <dst>
作用:該命令尚未實現,顯示“Not implemented yet”。

(24)-mv 
用法:移動檔案。
作用: hadoop fs -mv URI [URI ...] <dest>
示例:

hadoop fs -mv /user/hadoop/file1 /user/hadoop/file2
hadoop fs -mv hdfs://nn.example.com/file1 hdfs://nn.example.com/file2 hdfs://nn.example.com/file3 hdfs://nn.example.com/dir1

(25)-put
用法: hadoop fs -put <localsrc> ... <dst>
作用:將本地的檔案上傳(複製)到HDFS是dst目錄下。

示例:

hadoop fs -put localfile /user/hadoop/hadoopfile
hadoop fs -put localfile1 localfile2 /user/hadoop/hadoopdir
hadoop fs -put localfile hdfs://nn.example.com/hadoop/hadoopfile
hadoop fs -put - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin.
  
(26)-rm
用法:hadoop fs -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.

示例:

hadoop fs -rm hdfs://nn.example.com/file /user/hadoop/emptydir

(27)-rmdir
用法:hadoop fs -rmdir [--ignore-fail-on-non-empty] URI [URI ...]
作用:刪除空目錄。
選項:
—ignore-fail-on-non-empty:使用它的時候,忽略因資料夾非空刪除失敗的資訊。

(28)-rmr
作用:該方法已經被捨去。和-rm -r效果一樣。遞迴刪除。

(29)-setfacl
用法:hadoop fs -setfacl [-R] [-b |-k -m |-x <acl_spec> <path>] |[--set <acl_spec> <path>]
作用:設定訪問控制列表(ACL)的檔案和目錄。

選項:

-b:移除所有除了基本的ACL條目。使用者、組和其他的條目被保留為與許可權位的相容性。
-k:刪除預設的ACL。
-R: 遞回應用於所有檔案和目錄的操作。
-m:修改ACL。新的專案新增到ACL,並保留現有的條目。
-x:刪除指定的ACL條目。其他保留ACL條目。
–set:完全替換ACL,丟棄所有現有的條目。acl_spec必須包括使用者,組,和其他有許可權位的相容性。
acl_spec:逗號分隔的ACL條目列表。
path:修改檔案或目錄。

示例:

hadoop fs -setfacl -m user:hadoop:rw- /file
hadoop fs -setfacl -x user:hadoop /file
hadoop fs -setfacl -b /file
hadoop fs -setfacl -k /dir
hadoop fs -setfacl --set user::rw-,user:hadoop:rw-,group::r--,other::r-- /file
hadoop fs -setfacl -R -m user:hadoop:r-x /dir
hadoop fs -setfacl -m default:user:hadoop:r-x /dir
  
(30)-setrep
用法:hadoop fs -setrep [-R] [-w] <numReplicas> <path>
作用:改變檔案的目標副本系數,放入REP中。選項-R將遞迴的改變PATH指定的目錄中所有檔案的目標副本系數。副本系數需要一定的時間才能達到目標值。選項-w將等待副本系數以與目標值相匹配。

示例:

hadoop fs -setrep -w 3 /user/hadoop/dir1
  
(31)-stat
用法: hadoop fs -stat [format] <path> ...
作用:根據一定格式列印檔案/資料夾的統計資訊。 檔案大小 (%b), 型別 (%F), 所有者所在組 (%g), 名字 (%n), 塊大小 (%o), 副本 (%r), 使用者名稱(%u), 修改時間 (%y, %Y)。預設的是%y。
示例:
hadoop fs -stat "%F %u:%g %b %y %n" /file

(32)-tail
用法:hadoop fs -tail [-f] URI
作用:輸出檔案最後1kb的內容。
選項:
-f:和unix中tail -f命令類似,當檔案內容更新時,輸出將會改變,具有實時性。

示例:用一個場景測試下。首先HDFS的/目錄下有檔案mpwtest1.txt
命令:hadoop fs -tail -f /mpwtest1.txt
開啟另外一個終端。輸入命令: hadoop fs -appendToFile mpwtest2.txt /mpwtest1.txt
可以發現 視窗1 有變化。

(33)-test

作用:判斷檔案資訊

用法:hadoop fs -test -[defsz] URI

選項:

-d:如果路徑是一個目錄,返回0
-e:如果路徑已經存在,返回0
-f: 如果路徑是一個檔案,返回0
-s:如果路徑不是空,返回0
-z:如果檔案長度為0,返回0
URI:資源地址,可以是檔案也可以是目錄。

示例:

hadoop fs -test -e filename

(34)-text
用法:hadoop fs -text <src>
作用:將HDFS中檔案以文字形式輸出(包括zip包,jar包等形式)
示例:hadoop fs -text /wc.jar

(35)-touchz
用法: hadoop fs -touchz URI [URI ...]
作用:建立一個空檔案。
示例:hadoop fs -touchz /hello.jar

(35)-truncate
用法: hadoop fs -truncate [-w] <length> <paths>

作用:截斷指定長度匹配的所有檔案內容。

選項:

-w:需要等待命令完成塊恢復。如果沒有-w選項,在恢復的過程中可能是未閉合的。
length:截斷處的值,如果是100,則表示在100B處截斷。
paths:檔案地址。

示例:

hadoop fs -truncate 55 /user/hadoop/file1 /user/hadoop/file2
hadoop fs -truncate -w 127 hdfs://nn1.example.com/user/hadoop/file1

(36)-usage
用法: hadoop fs -usage command
作用:返回命令的help資訊。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31383567/viewspace-2144204/,如需轉載,請註明出處,否則將追究法律責任。

相關文章