1、操做 HDFS 上的文件有兩個命令能夠用
hdfs dfs:只能操做 HDFS 上的文件html


Usage: hdfs [--config confdir] [--loglevel loglevel] COMMAND where COMMAND is one of: dfs run a filesystem command on the file systems supported in Hadoop. classpath prints the classpath namenode -format format the DFS filesystem secondarynamenode run the DFS secondary namenode namenode run the DFS namenode journalnode run the DFS journalnode zkfc run the ZK Failover Controller daemon datanode run a DFS datanode debug run a Debug Admin to execute HDFS debug commands dfsadmin run a DFS admin client dfsrouter run the DFS router dfsrouteradmin manage Router-based federation haadmin run a DFS HA admin client fsck run a DFS filesystem checking utility balancer run a cluster balancing utility jmxget get JMX exported values from NameNode or DataNode. mover run a utility to move block replicas across storage types oiv apply the offline fsimage viewer to an fsimage oiv_legacy apply the offline fsimage viewer to an legacy fsimage oev apply the offline edits viewer to an edits file fetchdt fetch a delegation token from the NameNode getconf get config values from configuration groups get the groups which users belong to snapshotDiff diff two snapshots of a directory or diff the current directory contents with a snapshot lsSnapshottableDir list all snapshottable dirs owned by the current user Use -help to see options portmap run a portmap service nfs3 run an NFS version 3 gateway cacheadmin configure the HDFS cache crypto configure HDFS encryption zones storagepolicies list/get/set block storage policies version print the version Most commands print help when invoked w/o parameters.
hadoop fs:除了 HDFS 上的文件,還能夠操做本地文件java


Usage: hadoop [--config confdir] [COMMAND | CLASSNAME] CLASSNAME run the class named CLASSNAME or where COMMAND is one of: fs run a generic filesystem user client version print the version jar <jar> run a jar file note: please use "yarn jar" to launch YARN applications, not this command. checknative [-a|-h] check native hadoop and compression libraries availability distcp <srcurl> <desturl> copy file or directories recursively archive -archiveName NAME -p <parent path> <src>* <dest> create a hadoop archive classpath prints the class path needed to get the Hadoop jar and the required libraries credential interact with credential providers daemonlog get/set the log level for each daemon trace view and modify Hadoop tracing settings Most commands print help when invoked w/o parameters.
2、使用
help:查看命令幫助node
hadoop fs -help ls
ls:顯示目錄信息linux
# 查看根目錄 hadoop fs -ls / # 遞歸查看全部目錄 hadoop fs -ls -R / hadoop fs -lsr /
mkdir:建立目錄shell
# 建立多級目錄 hadoop fs -mkdir -p /china/hubei/
moveFromLocal:移動本地文件到 HDFS 上apache
# 移動本地 /opt/java-linux-x64.tar.gz 至 HDFS 的 /china/hubei/ 路徑下 hadoop fs -moveFromLocal /opt/java-linux-x64.tar.gz /china/hubei/
appendToFile:把本地文件的內容追加到 HDFS 上的文件末尾服務器
# 建立兩個文件 echo "AAA" > /tmp/AAA.txt echo "BBB" > /tmp/BBB.txt # 把本地 /tmp/AAA.txt 移動至 HDFS 上的 /china/ 目錄下 hadoop fs -moveFromLocal /tmp/AAA.txt /china/ # 把本地 /tmp/BBB.txt 追加到 HDFS 上的 /china/AAA.txt 文件末尾 hadoop fs -appendToFile /tmp/BBB.txt /china/AAA.txt
cat:查看文件內容app
hadoop fs -cat /china/AAA.txt
chgrp 、chmod、chown:修改文件屬性和權限curl
# 修改 /china/ 目錄及其全部子目錄的用戶組爲 root hadoop fs -chgrp -R root /china/ # 修改 /china/ 目錄及其全部子目錄的權限爲 777 hadoop fs -chmod -R 777 /china/ # 修改 /china/ 目錄及其全部子目錄的全部者爲 root hadoop fs -chown -R root /china/
put、copyFromLocal:拷貝本地文件到 HDFS 上(上傳)tcp
# 複製本地 /tmp/ 目錄到 HDFS 的 /china/ 目錄下 hadoop fs -copyFromLocal /tmp/ /china/ hadoop fs -put /tmp/ /china/
get、copyToLocal:拷貝 HDFS 上的文件到本地(下載)
# 複製 HDFS 上 /china/BBB.txt 文件到本地的當前目錄 hadoop fs -copyToLocal /china/BBB.txt ./ hadoop fs -get /china/BBB.txt ./
moveToLocal:移動 HDFS 上的文件到本地
# Hadoop 目前版本(2.9.2)還沒有實現該功能
cp:在 HDFS 上覆制文件
# 將 HDFS 上的 /china/AAA.txt 複製到 HDFS 的 / 目錄下 hadoop fs -cp /china/AAA.txt /
mv:在 HDFS 上移動文件
# 將 HDFS 上的 /china/BBB.txt 移動到 HDFS 的 / 目錄下 hadoop fs -mv /china/BBB.txt /
getmerge:合併下載
# 清空本地 /tmp/ 目錄 rm -rf /tmp/* # 在本地 /tmp/ 中建立兩個文件 echo "AAA" > /tmp/AAA.txt echo "BBB" > /tmp/BBB.txt # 把本地 /tmp/*.txt 上傳至 HDFS 上的 /china/ 目錄下 hadoop fs -mkdir -p /china/txt/ hadoop fs -put /tmp/*.txt /china/txt/ # 下載 HDFS 上 /china/txt/ 路徑下全部文件的內容到本地 hadoop fs -getmerge /china/txt/* /tmp/CCC.txt
tail:顯示 HDFS 上的文件最後 1KB 的內容
# 直接顯示 hadoop fs -tail /AAA.txt # 監控顯示,有新數據追加進來時會實時顯示 hadoop fs -tail -f /AAA.txt
rmdir:刪除空文件夾
# 須要確保 HDFS 上的 /temp/ 目錄爲空 hadoop fs -rmdir /temp/
rm:刪除文件或文件夾
# 刪除 HDFS 上的 /china/ 目錄 # f 目標目錄不存在不提示 # r|R 遞歸刪除 hadoop fs -rm -f -r /china/ hadoop fs -rmr -f /china/
若是啓用了垃圾箱,則文件系統會將已刪除的文件移動到垃圾箱目錄,默認禁用垃圾箱功能
<!-- core-site.xml --> <!-- value 的值單位爲分鐘,設置大於零的值來啓用垃圾箱功能 --> <!-- 若是在服務器端禁用垃圾,則檢查客戶端配置。 若是在服務器端啓用了垃圾箱,則使用服務器上配置的值,並忽略客戶端配置值 --> <property> <name>fs.trash.interval</name> <value>60*24*2</value> </property> <!-- value 的值單位爲分鐘,檢查回收站的間隔時間,應小於或等於 fs.trash.interval。 若是爲零,則值爲fs.trash.interval的值 --> <!-- 每次 checkpointer 運行時,都會建立一個新的檢查點,並刪除超過 fs.trash.interval 分鐘前建立的檢查點 --> <property> <name>fs.trash.checkpoint.interval</name> <value>60*24*2</value> </property>
count,du:統計文件大小
hadoop fs -du -s -h / hadoop fs -count /
find:查找文件
# name 不區分大小寫 # iname 區分大小寫 # print 打印(默認) # print0 打印在一行 hadoop fs -find / -name *.txt -print
https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/FileSystemShell.html