使用hadoop bin目录的hdfs命令,后面指定dfs,表示是操作分布式文件系统的,这些属于固定格式。
如果在PATH中配置了hadoop的bin目录,那么这里可以直接使用hdfs就可以了。
这里的xxx是一个占位符,具体我们想对hdfs做什么操作,就可以在这里指定对应的命令了。
大多数hdfs 的命令和对应的Linux命令类似。
HDFS的schema是hdfs,authority是集群中namenode所在节点的ip和对应的端口号,把ip换成主机名也是一样的,path是我们要操作的文件路径信息。
其实后面这一长串内容就是core-site.xml配置文件中fs.defaultFS属性的值,这个代表的是HDFS的地址。
2、目录列表直接在命令行中输入hdfs dfs,可以查看dfs后面可以跟的所有参数。
注意:这里面的[]表示是可选项,<>表示是必填项
[root@bigdata01 hadoop-3.2.0]# hdfs dfs
Usage: hadoop fs [generic options]
[-appendToFile ... ]
[-cat [-ignoreCrc] ...]
[-checksum ...]
[-chgrp [-R] GROUP PATH...]
[-chmod [-R] PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-copyFromLocal [-f] [-p] [-l] [-d] [-t ] ... ]
[-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] ... ]
[-count [-q] [-h] [-v] [-t []] [-u] [-x] [-e] ...]
[-cp [-f] [-p | -p[topax]] [-d] ... ]
[-createSnapshot []]
[-deleteSnapshot ]
[-df [-h] [ ...]]
[-du [-s] [-h] [-v] [-x] ...]
[-expunge]
[-find ... ...]
[-get [-f] [-p] [-ignoreCrc] [-crc] ... ]
[-getfacl [-R] ]
[-getfattr [-R] {-n name | -d} [-e en] ]
[-getmerge [-nl] [-skip-empty-file] ]
[-head ]
[-help [cmd ...]]
[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [-e] [ ...]]
[-mkdir [-p] ...]
[-moveFromLocal ... ]
[-moveToLocal ]
[-mv ... ]
[-put [-f] [-p] [-l] [-d] ... ]
[-renameSnapshot ]
[-rm [-f] [-r|-R] [-skipTrash] [-safely] ...]
[-rmdir [--ignore-fail-on-non-empty] ...]
[-setfacl [-R] [{-b|-k} {-m|-x } ]|[--set ]]
[-setfattr {-n name [-v value] | -x name} ]
[-setrep [-R] [-w] ...]
[-stat [format] ...]
[-tail [-f] ]
[-test -[defsz] ]
[-text [-ignoreCrc] ...]
[-touch [-a] [-m] [-t TIMESTAMP ] [-c] ...]
[-touchz ...]
[-truncate [-w] ...]
[-usage [cmd ...]]
Generic options supported are:
-conf specify an application configuration file
-D define a value for a given property
-fs specify default filesystem URL to use, overrides 'fs.defaultFS' property from configurations.
-jt specify a ResourceManager
-files specify a comma-separated list of files to be copied to the map reduce cluster
-libjars specify a comma-separated list of jar files to be included in the classpath
-archives ,...> specify a comma-separated list of archives to be unarchived on the compute machines
The general command line syntax is:
command [genericOptions] [commandOptions]
3、ls:查询指定路径信息
查看hdfs根目录下的内容
[root@bigdata01 hadoop-3.2.0]# hdfs dfs -ls / [root@bigdata01 hadoop-3.2.0]#4、-put:从本地上传文件
接下来我们向hdfs中上传一个文件,使用Hadoop中的README.txt,直接上传到hdfs的根目录即可
[root@bigdata01 hadoop-3.2.0]# hdfs dfs -put README.txt /5、-cat:查看HDFS文件内容
文件上传上去以后,我们还想查看一下HDFS中文件的内容,很简单,使用cat即可
[root@bigdata01 hadoop-3.2.0]# hdfs dfs -cat /README.txt For the latest information about Hadoop, please visit our website at: http://hadoop.apache.org/ and our wiki, at: http://wiki.apache.org/hadoop/ ...........6、-get:下载文件到本地
如果我们想把hdfs中的文件下载到本地linux文件系统中需要怎么做呢?使用get即可实现
[root@bigdata01 hadoop-3.2.0]# hdfs dfs -get /README.txt . [root@bigdata01 hadoop-3.2.0]#7、-mkdir [-p]:创建文件夹
后期我们需要在hdfs中维护很多文件,所以就需要创建文件夹来进行分类管理了
[root@bigdata01 hadoop-3.2.0]# hdfs dfs -mkdir /test [root@bigdata01 hadoop-3.2.0]# hdfs dfs -ls / Found 2 items -rw-r--r-- 2 root supergroup 1361 2020-04-08 15:34 /README.txt drwxr-xr-x - root supergroup 0 2020-04-08 15:43 /test
如果要递归创建多级目录,还需要再指定-p参数
[root@bigdata01 hadoop-3.2.0]# hdfs dfs -mkdir -p /abc/xyz You have new mail in /var/spool/mail/root [root@bigdata01 hadoop-3.2.0]# hdfs dfs -ls / Found 3 items -rw-r--r-- 2 root supergroup 1361 2020-04-08 15:34 /README.txt drwxr-xr-x - root supergroup 0 2020-04-08 15:44 /abc drwxr-xr-x - root supergroup 0 2020-04-08 15:43 /test8、-rm [-r]:删除文件/文件夹
如果想要删除hdfs中的目录或者文件,可以使用rm
[root@bigdata01 hadoop-3.2.0]# hdfs dfs -rm /README.txt Deleted /README.txt
删除目录,注意,删除目录需要指定-r参数
[root@bigdata01 hadoop-3.2.0]# hdfs dfs -rm /test rm: `/test': Is a directory [root@bigdata01 hadoop-3.2.0]# hdfs dfs -rm -r /test Deleted /test9、chmod:改变文件/目录的权限,使用-R参数实现递归改变权限,命令的使用者必须是文件的所有者或者超级用户。
格式:hdfs dfs -chmod [-R]
修改前:
[root@bigdata04 ~]# hdfs dfs -ls / Found 33 items -rw-r--r-- 2 root supergroup 1361 2026-03-12 21:32 /README.txt drwxr-xr-x - root supergroup 0 2026-09-02 17:54 /abc
给这个文件和目录改变权限信息,将权限改为最大的777
[root@bigdata04 ~]# hdfs dfs -chmod 777 /README.txt [root@bigdata04 ~]# hdfs dfs -chmod 777 /abc
查看修改后的权限信息。
[root@bigdata04 ~]# hdfs dfs -ls / Found 33 items -rwxrwxrwx 2 root supergroup 1361 2026-03-12 21:32 /README.txt drwxrwxrwx - root supergroup 0 2026-09-02 17:54 /abc10、安全模式
格式:hdfs dfsadmin [-safemode
安全模式开启 : 能读不能写
[root@bigdata04 ~]# hdfs dfsadmin -safemode enter Safe mode is ON
安全模式关闭 : 读写正常
[root@bigdata04 ~]# hdfs dfsadmin -safemode leave



