HDFS作为分布式存储的文件系统有其对数据的路径的表达方式HDFS同Linux系统一样以/作为根目录在HDFS中区分本地操作和HDFS操作主要通过路径前缀Scheme来识别HDFS操作使用hdfs://前缀hdfs://节点名称:端口号/具体的路径老版本hadoop fs [选项] 命令新版本hdfs dfs [选项] 命令在HDFS中创建文件夹hadoop fs -mkdir [-p] path ... # directory 名录电话簿计算机中的目录 或新版本的命令 hdfs dfs -mkdir [-p] path ...[rootmaster hadoop]# hadoop fs -mkdir -p file:///itheima/bigdata # file://协议头表示在Linux本地中操作 [rootmaster hadoop]# ls / bin boot data demo01 dev etc home itheima lib lib64 media mnt opt proc Projects root run sbin srv sys Tests tmp usr var [rootmaster hadoop]# ls /itheima/ bigdata [rootmaster hadoop]# hadoop fs -mkdir -p hdfs://master:8020/itheima/bigdata # hdfs://协议头表示在HDFS文件系统中操作 [rootmaster hadoop]# hadoop fs -ls / Found 6 items drwxr-xr-x - root supergroup 0 2026-04-27 00:30 /Tests drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data drwxr-xr-x - root supergroup 0 2026-05-17 23:06 /itheima -rw-r--r-- 2 root supergroup 11 2026-04-23 09:38 /test.txt drwxrwx--- - root supergroup 0 2026-04-18 02:27 /tmp drwxr-xr-x - root supergroup 0 2026-05-14 08:58 /user [rootmaster hadoop]# hadoop fs -ls /itheima/ Found 1 items drwxr-xr-x - root supergroup 0 2026-05-17 23:06 /itheima/bigdata[rootmaster hadoop]# hdfs dfs -mkdir -p /home/hadoop/itcast # 不带协议头默认在HDFS文件系统中操作 [rootmaster hadoop]# hdfs dfs -ls / Found 7 items drwxr-xr-x - root supergroup 0 2026-04-27 00:30 /Tests drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data drwxr-xr-x - root supergroup 0 2026-05-17 23:10 /home drwxr-xr-x - root supergroup 0 2026-05-17 23:06 /itheima -rw-r--r-- 2 root supergroup 11 2026-04-23 09:38 /test.txt drwxrwx--- - root supergroup 0 2026-04-18 02:27 /tmp drwxr-xr-x - root supergroup 0 2026-05-14 08:58 /user [rootmaster hadoop]# ls /home # 发现Linux本地中没有itcast这个目录证实不带协议头则默认在HDFS中进行操作 user2HDFS中的ls-R选项递归查看每个目录中存在的子目录和文件下面是部分输出[rootmaster hadoop]# hdfs dfs -ls -R / drwxr-xr-x - root supergroup 0 2026-04-27 00:30 /Tests -rw-r--r-- 2 root supergroup 178 2026-04-27 00:01 /Tests/data1.txt -rw-r--r-- 2 root supergroup 293 2026-04-27 00:30 /Tests/data2.txt -rw-r--r-- 2 root supergroup 0 2026-04-26 23:51 /Tests/input.txt drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data/tmp drwxrwxrwt - root root 0 2026-05-14 09:12 /data/tmp/logs drwxrwx--- - root root 0 2026-05-14 09:12 /data/tmp/logs/root drwxrwx--- - root root 0 2026-05-15 00:02 /data/tmp/logs/root/logs-tfile上传文件到HDFShadoop fs -put [-f] [-p] localsrc dst 解释 -f表示强制覆盖-p表示保留文件信息修改时间、权限等 src是source来源、根源计算中表示源文件的缩写。dst是destination目的地、目标的缩写 前一个路径是Linux本地的文件的路径后一个路径是要上传到HDFS中的路径 hdfs dfs -put [-f] [-p] localsrc dst[rootmaster ~]# vim testfile1.txt [rootmaster ~]# cat testfile1.txt itheima itcast hahaha heiheihei bigdata 666 [rootmaster ~]# hadoop fs -put file:///root/testfile1.txt hdfs://master:8020/ [rootmaster ~]# hdfs dfs -ls / Found 8 items drwxr-xr-x - root supergroup 0 2026-04-27 00:30 /Tests drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data drwxr-xr-x - root supergroup 0 2026-05-17 23:10 /home drwxr-xr-x - root supergroup 0 2026-05-17 23:06 /itheima -rw-r--r-- 2 root supergroup 11 2026-04-23 09:38 /test.txt -rw-r--r-- 2 root supergroup 44 2026-05-17 23:31 /testfile1.txt drwxrwx--- - root supergroup 0 2026-04-18 02:27 /tmp drwxr-xr-x - root supergroup 0 2026-05-14 08:58 /user [rootmaster ~]# hdfs dfs -cat /testfile1.txt # 不带协议头默认操作HDFS文件系统 itheima itcast hahaha heiheihei bigdata 666 [rootmaster ~]#注若带协议头~不会被bash展开于是HDFS命令把~当成了普通字符路径就会出错或者显示没有此文件[rootmaster ~]# vim testfile2.txt [rootmaster ~]# cat testfile2.txt happy delight glad joyful cheerful thrill [rootmaster ~]# hdfs dfs -put ./testfile2.txt / # 将当前目录下的testfile2.txt 上传到HDFS中根目录/下 [rootmaster ~]# hdfs dfs -ls / Found 9 items drwxr-xr-x - root supergroup 0 2026-04-27 00:30 /Tests drwxr-xr-x - root supergroup 0 2026-05-14 09:12 /data drwxr-xr-x - root supergroup 0 2026-05-17 23:10 /home drwxr-xr-x - root supergroup 0 2026-05-17 23:06 /itheima -rw-r--r-- 2 root supergroup 11 2026-04-23 09:38 /test.txt -rw-r--r-- 2 root supergroup 44 2026-05-17 23:31 /testfile1.txt -rw-r--r-- 2 root supergroup 42 2026-05-17 23:38 /testfile2.txt drwxrwx--- - root supergroup 0 2026-04-18 02:27 /tmp drwxr-xr-x - root supergroup 0 2026-05-14 08:58 /user [rootmaster ~]# hdfs dfs -cat /testfile2.txt happy delight glad joyful cheerful thrill[rootmaster ~]# cat testfile2.txt testfile1.txt # 将testfile2.txt的内容追加的testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# cat testfile2.txt testfile1.txt [rootmaster ~]# ls -l testfile1.txt # 查看Linux本地刚追加完的testfile1.txt的大小 -rw-r--r--. 1 root root 2144 May 17 23:41 testfile1.txt # 2144字节 [rootmaster ~]# ls -lh testfile1.txt #-h选项更加人性化的展示文件大小2.1KB -rw-r--r--. 1 root root 2.1K May 17 23:41 testfile1.txt [rootmaster ~]# cat testfile1.txt # 查看文件内容 itheima itcast hahaha heiheihei bigdata 666 happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill [rootmaster ~]# hdfs dfs -ls /testfile1.txt # 普通查看HDFS中testfile1.txt的大小44字节 -rw-r--r-- 2 root supergroup 44 2026-05-17 23:31 /testfile1.txt [rootmaster ~]# hdfs dfs -ls -h /testfile1.txt # 人性化查看还是44字节 -rw-r--r-- 2 root supergroup 44 2026-05-17 23:31 /testfile1.txt [rootmaster ~]# hadoop fs -put testfile1.txt / # 上传已经追加完的文件 put: /testfile1.txt: File exists # HDFS中已经存在该文件上传失败 [rootmaster ~]# hadoop fs -put -f testfile1.txt / # 使用-f选项强制覆盖式上传 [rootmaster ~]# hadoop fs -ls /testfile1.txt # 发现文件变大了文件修改的时间也更新了 -rw-r--r-- 2 root supergroup 2144 2026-05-17 23:44 /testfile1.txt [rootmaster ~]# hdfs dfs -ls -h /testfile1.txt # 人性化2.1KB -rw-r--r-- 2 root supergroup 2.1 K 2026-05-17 23:44 /testfile1.txt查看HDFS中文件的内容hadoop fs -cat src hdfs dfs -cat src读取大文件时可以使用管道符配合more命令(more命令用于对内容进行翻页)hadoop fs -cat src | more hdfs dfs -cat src | more[rootmaster ~]# hdfs dfs -cat /testfile1.txt itheima itcast hahaha heiheihei bigdata 666 happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill [rootmaster ~]# hadoop fs -cat /testfile1.txt | more itheima itcast hahaha heiheihei bigdata 666 happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy delight glad joyful cheerful thrill happy --More--出现“--More--”时按空格键就能翻页HDFS中下载文件到Linux本地hadoop fs -get [-f] [-p] src localdst hdfs dfs -get [-f] [-p] src localdst 解释 -f表示Linux本地路径下已经存在了这个文件但是强制覆盖它 -p表示保留访问和修改等信息[rootmaster ~]# ls anaconda-ks.cfg getJPSInfo.sh hadoop-3.1.4.tar.gz manage_firewall.sh startHadoop.sh stopHadoop.sh testfile1.txt testfile2.txt [rootmaster ~]# rm testfile1.txt testfile2.txt rm: remove regular file ‘testfile1.txt’? y rm: remove regular file ‘testfile2.txt’? y [rootmaster ~]# ls anaconda-ks.cfg getJPSInfo.sh hadoop-3.1.4.tar.gz manage_firewall.sh startHadoop.sh stopHadoop.sh [rootmaster ~]# hadoop fs -get /testfile2.txt . #.表示当前目录 [rootmaster ~]# ls anaconda-ks.cfg getJPSInfo.sh hadoop-3.1.4.tar.gz manage_firewall.sh startHadoop.sh stopHadoop.sh testfile2.txt [rootmaster ~]# cat testfile2.txt happy delight glad joyful cheerful thrillHDFS中拷贝文件HDFS删除文件或目录[rootmaster ~]# hdfs dfs -rm -r /Tests/* rm: /Tests/practice.txt: No such file or directory rm: /Tests/test1.sh: No such file or directory rm: /Tests/test2.sh: No such file or directory rm: /Tests/test3.sh: No such file or directory rm: /Tests/test.sh: No such file or directory [rootmaster ~]# hdfs dfs -ls /Tests Found 3 items -rw-r--r-- 2 root supergroup 178 2026-04-27 00:01 /Tests/data1.txt -rw-r--r-- 2 root supergroup 293 2026-04-27 00:30 /Tests/data2.txt -rw-r--r-- 2 root supergroup 0 2026-04-26 23:51 /Tests/input.txt [rootmaster ~]# hdfs dfs -rm /Tests/* Deleted /Tests/data1.txt Deleted /Tests/data2.txt Deleted /Tests/input.txt [rootmaster ~]# hdfs dfs -ls /Tests [rootmaster ~]#第一次删除失败的原因*通配符没有正确传递给hdfs dfs命令而是在当前的Linux Shellbash中被展开了也就是说在执行命令时Shell先尝试把/Tests/*匹配成本地Linux系统中的文件但是在本地Linux中的/Tests目录下没有子目录了你使用参数-r是递归删除目录的。但这还不是主要原因主要原因是Shell匹配到了本地的/Tests下的文件后把匹配到的内容作为参数传递给hdfs这个程序hdfs接收到这些参数后但是在HDFS系统中不存在Linux本地的/Tests目录下的文件如/Tests/data1.txt等所以Shell在HDFS系统中找不到目标文件就会报错[rootmaster ~]# ls /Tests practice.txt test1.sh test2.sh test3.sh test.sh [rootmaster ~]# ls -l /Tests total 20 -rw-r--r--. 1 root root 279 May 7 16:20 practice.txt -rw-r--r--. 1 root root 37 May 8 17:09 test1.sh -rw-r--r--. 1 root root 36 May 8 17:18 test2.sh -rwxrw---x. 1 root root 36 May 8 17:22 test3.sh -rw-r--r--. 1 root root 88 May 8 16:52 test.sh给路径加上引号就不会被展示成本地Linux的路径了