HDFS 命令大全

概要

HDFS 文件系統的經常使用操做和普通 Linux 文件系統很是類似,如讀取文件,新建目錄,移動文件,刪除數據,列出目錄等。node

本文只列出經常使用的 HDFS 命名,要查看完整的命令行列表,可點擊 這裏shell

全部的 HDFS 命令都是調用 bin/hdfs 腳本完成的,它的基本用法以下express

hdfs [SHELL_OPTIONS] COMMAND [GENERIC_OPTIONS] [COMMAND_OPTIONS]

hdfs 命令分紅 3 種類型,分別是apache

  • 用戶命令
  • 集羣管理員命令
  • 調試命令(集羣管理員使用)

在命令中會有一些 URI 參數,其中 URI 的前綴是由 core-site.xml 中的 fs.defaultFS 決定的,經過該屬性就能夠知道 namenode 在哪裏運行進而鏈接到它。例如:app

<property>
    <name>fs.defaultFS</name>
    <value>hdfs://hadoop2cluster</value>
  </property>

而若是在本機操做,就能夠省略 URI 前綴。ide

用戶命令

dfs 命令

hdfs dfs [COMMAND [COMMAND_OPTIONS]]

hadoop 所支持的文件系統命令,其中 COMMAND_OPTIONS 可在 File System Shell Guide 查看。oop

hdfs dfs 命令前綴與 hadoop fs 效果相同。ui

追加文件內容

-appendToFile <localsrc> ... <dst>

實例命令行

hdfs dfs -appendToFile hdfs-site.xml /tmp/tests/test.txt

查看文件內容

-cat URI [URI ...]

實例

hdfs dfs -cat /tmp/tests/test.txt

獲得文件的校驗信息

-checksum URI

實例

hdfs dfs -checksum /tmp/tests/test.txt

修改用戶組

-chgrp [-R] GROUP URI [URI ...]

Change group association of files. The user must be the owner of files, or else a super-user. Additional information is in the Permissions Guide.

修改文件權限

-chmod [-R] <MODE[,MODE]... | OCTALMODE> URI [URI ...]

修改文件所屬用戶

-chown [-R] [OWNER][:[GROUP]] URI [URI ]

本地拷貝到 hdfs

-copyFromLocal <localsrc> URI

hdfs 拷貝到本地

-copyToLocal [-ignorecrc] [-crc] URI <localdst>

獲取目錄,文件數量及大小

-count [-q] [-h] [-v] <paths>

舉例

hdfs dfs -count /tmp/tests/

獲得結果

1            1               3513 /tmp/tests

其中

  • 列 1 表示 目錄數量
  • 列 2 表示 文件數量
  • 列 3 表示 文件總大小(字節)

hdfs 內拷貝

-cp [-f] [-p | -p[topax]] URI [URI ...] <dest>
  • -f 表示若是文件已存在,會覆蓋原文件
  • -p 表示會保留文件屬性(時間錯,owner,權限, ACL 等)

createSnapshot 建立快照

See HDFS Snapshots Guide.

deleteSnapshot 刪除快照

See HDFS Snapshots Guide.

顯示空閒空間

-df [-h] URI [URI ...]
  • -h 選項會以人類友好的方式來顯示,如 64M 而不是多少字節

顯示文件和目錄大小

-du [-s] [-h] URI [URI ...]
  • The -s option will result in an aggregate summary of file lengths being displayed, rather than the individual files.
  • The -h option will format file sizes in a 「human-readable」 fashion (e.g 64.0m instead of 67108864)

清空回收站

-expunge

Empty the Trash. Refer to the HDFS Architecture Guide for more information on the Trash feature.

查找文件

-find <path> ... <expression> ...

顯示目錄或文件的訪問控制列表

-getfacl [-R] <path>

顯示文件或目錄的擴展信息

-getfattr [-R] -n name | -d [-e en] <path>
  • -R: Recursively list the attributes for all files and directories.
  • -n name: Dump the named extended attribute value.
  • -d: Dump all extended attribute values associated with pathname.
  • -e encoding: Encode values after retrieving them. Valid encodings are 「text」, 「hex」, and 「base64」. Values encoded as text strings are enclosed in double quotes ("), and values encoded as hexadecimal and base64 are prefixed with 0x and 0s, respectively.
  • path: The file or directory.

ls

-ls [-d] [-h] [-R] [-t] [-S] [-r] [-u] <args>
  • -d: Directories are listed as plain files.
  • -h: Format file sizes in a human-readable fashion (eg 64.0m instead of 67108864).
  • -R: Recursively list subdirectories encountered.
  • -t: Sort output by modification time (most recent first).
  • -S: Sort output by file size.
  • -r: Reverse the sort order.
  • -u: Use access time rather than modification time for display and sorting.

mkdir

-mkdir [-p] <paths>

moveFromLocal

-moveFromLocal <localsrc> <dst>

moveToLocal

-moveToLocal [-crc] <src> <dst>

mv

-mv URI [URI ...] <dest>

rm

-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.

test

-test -[defsz] URI
  • -d: f the path is a directory, return 0.
  • -e: if the path exists, return 0.
  • -f: if the path is a file, return 0.
  • -s: if the path is not empty, return 0.
  • -z: if the file is zero length, return 0.

tail

-tail [-f] URI

顯示文件的最後的字節

  • The -f option will output appended data as the file grows, as in Unix.

usage

顯示命令的用法

-usage command

參考文檔

相關文章
相關標籤/搜索