一、操作 HDFS 上的文件有两个命令可以用
hdfs dfs:只能操作 HDFS 上的文件

1Usage: hdfs [--config confdir] [--loglevel loglevel] COMMAND 2 where COMMAND is one of: 3 dfs run a filesystem command on the file systems supported in Hadoop. 4 classpath prints the classpath 5 namenode -format format the DFS filesystem 6 secondarynamenode run the DFS secondary namenode 7 namenode run the DFS namenode 8 journalnode run the DFS journalnode 9 zkfc run the ZK Failover Controller daemon 10 datanode run a DFS datanode 11 debug run a Debug Admin to execute HDFS debug commands 12 dfsadmin run a DFS admin client 13 dfsrouter run the DFS router 14 dfsrouteradmin manage Router-based federation 15 haadmin run a DFS HA admin client 16 fsck run a DFS filesystem checking utility 17 balancer run a cluster balancing utility 18 jmxget get JMX exported values from NameNode or DataNode. 19 mover run a utility to move block replicas across 20 storage types 21 oiv apply the offline fsimage viewer to an fsimage 22 oiv_legacy apply the offline fsimage viewer to an legacy fsimage 23 oev apply the offline edits viewer to an edits file 24 fetchdt fetch a delegation token from the NameNode 25 getconf get config values from configuration 26 groups get the groups which users belong to 27 snapshotDiff diff two snapshots of a directory or diff the 28 current directory contents with a snapshot 29 lsSnapshottableDir list all snapshottable dirs owned by the current user 30 Use -help to see options 31 portmap run a portmap service 32 nfs3 run an NFS version 3 gateway 33 cacheadmin configure the HDFS cache 34 crypto configure HDFS encryption zones 35 storagepolicies list/get/set block storage policies 36 version print the version 37 38Most commands print help when invoked w/o parameters.
View Code
hadoop fs:除了 HDFS 上的文件,还可以操作本地文件

1Usage: hadoop [--config confdir] [COMMAND | CLASSNAME] 2 CLASSNAME run the class named CLASSNAME 3 or 4 where COMMAND is one of: 5 fs run a generic filesystem user client 6 version print the version 7 jar <jar> run a jar file 8 note: please use "yarn jar" to launch 9 YARN applications, not this command. 10 checknative [-a|-h] check native hadoop and compression libraries availability 11 distcp <srcurl> <desturl> copy file or directories recursively 12 archive -archiveName NAME -p <parent path> <src>* <dest> create a hadoop archive 13 classpath prints the class path needed to get the 14 Hadoop jar and the required libraries 15 credential interact with credential providers 16 daemonlog get/set the log level for each daemon 17 trace view and modify Hadoop tracing settings 18 19Most commands print help when invoked w/o parameters.
View Code
二、使用
help:查看命令帮助
hadoop fs -help ls
ls:显示目录信息
1# 查看根目录 2hadoop fs -ls / 3 4# 递归查看所有目录 5hadoop fs -ls -R / 6hadoop fs -lsr /
mkdir:创建目录
1# 创建多级目录 2hadoop fs -mkdir -p /china/hubei/
moveFromLocal:移动本地文件到 HDFS 上
1# 移动本地 /opt/java-linux-x64.tar.gz 至 HDFS 的 /china/hubei/ 路径下 2hadoop fs -moveFromLocal /opt/java-linux-x64.tar.gz /china/hubei/
appendToFile:把本地文件的内容追加到 HDFS 上的文件末尾
1# 创建两个文件 2echo "AAA" > /tmp/AAA.txt 3echo "BBB" > /tmp/BBB.txt 4 5# 把本地 /tmp/AAA.txt 移动至 HDFS 上的 /china/ 目录下 6hadoop fs -moveFromLocal /tmp/AAA.txt /china/ 7 8# 把本地 /tmp/BBB.txt 追加到 HDFS 上的 /china/AAA.txt 文件末尾 9hadoop fs -appendToFile /tmp/BBB.txt /china/AAA.txt
cat:查看文件内容
hadoop fs -cat /china/AAA.txt
chgrp 、chmod、chown:修改文件属性和权限
1# 修改 /china/ 目录及其所有子目录的用户组为 root 2hadoop fs -chgrp -R root /china/ 3 4# 修改 /china/ 目录及其所有子目录的权限为 777 5hadoop fs -chmod -R 777 /china/ 6 7# 修改 /china/ 目录及其所有子目录的所有者为 root 8hadoop fs -chown -R root /china/
put、copyFromLocal:拷贝本地文件到 HDFS 上(上传)
1# 复制本地 /tmp/ 目录到 HDFS 的 /china/ 目录下 2hadoop fs -copyFromLocal /tmp/ /china/ 3hadoop fs -put /tmp/ /china/
get、copyToLocal:拷贝 HDFS 上的文件到本地(下载)
1# 复制 HDFS 上 /china/BBB.txt 文件到本地的当前目录 2hadoop fs -copyToLocal /china/BBB.txt ./ 3hadoop fs -get /china/BBB.txt ./
moveToLocal:移动 HDFS 上的文件到本地
# Hadoop 目前版本(2.9.2)尚未实现该功能
cp:在 HDFS 上复制文件
1# 将 HDFS 上的 /china/AAA.txt 复制到 HDFS 的 / 目录下 2hadoop fs -cp /china/AAA.txt /
mv:在 HDFS 上移动文件
1# 将 HDFS 上的 /china/BBB.txt 移动到 HDFS 的 / 目录下 2hadoop fs -mv /china/BBB.txt /
getmerge:合并下载
1# 清空本地 /tmp/ 目录 2rm -rf /tmp/* 3 4# 在本地 /tmp/ 中创建两个文件 5echo "AAA" > /tmp/AAA.txt 6echo "BBB" > /tmp/BBB.txt 7 8# 把本地 /tmp/*.txt 上传至 HDFS 上的 /china/ 目录下 9hadoop fs -mkdir -p /china/txt/ 10hadoop fs -put /tmp/*.txt /china/txt/ 11 12# 下载 HDFS 上 /china/txt/ 路径下所有文件的内容到本地 13hadoop fs -getmerge /china/txt/* /tmp/CCC.txt
tail:显示 HDFS 上的文件最后 1KB 的内容
1# 直接显示 2hadoop fs -tail /AAA.txt 3 4# 监控显示,有新数据追加进来时会实时显示 5hadoop fs -tail -f /AAA.txt
rmdir:删除空文件夹
1# 需要确保 HDFS 上的 /temp/ 目录为空 2hadoop fs -rmdir /temp/
rm:删除文件或文件夹
1# 删除 HDFS 上的 /china/ 目录 2# f 目标目录不存在不提示 3# r|R 递归删除 4hadoop fs -rm -f -r /china/ 5hadoop fs -rmr -f /china/
如果启用了垃圾箱,则文件系统会将已删除的文件移动到垃圾箱目录,默认禁用垃圾箱功能
1<!-- core-site.xml --> 2<!-- value 的值单位为分钟,设置大于零的值来启用垃圾箱功能 --> 3<!-- 如果在服务器端禁用垃圾,则检查客户端配置。 如果在服务器端启用了垃圾箱,则使用服务器上配置的值,并忽略客户端配置值 --> 4<property> 5 <name>fs.trash.interval</name> 6 <value>60*24*2</value> 7</property> 8<!-- value 的值单位为分钟,检查回收站的间隔时间,应小于或等于 fs.trash.interval。 如果为零,则值为fs.trash.interval的值 --> 9<!-- 每次 checkpointer 运行时,都会创建一个新的检查点,并删除超过 fs.trash.interval 分钟前创建的检查点 --> 10<property> 11 <name>fs.trash.checkpoint.interval</name> 12 <value>60*24*2</value> 13</property>
count,du:统计文件大小
1hadoop fs -du -s -h / 2hadoop fs -count /
find:查找文件
1# name 不区分大小写 2# iname 区分大小写 3# print 打印(默认) 4# print0 打印在一行 5hadoop fs -find / -name *.txt -print
https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/FileSystemShell.html