hadoop的shell命令总结(实践总结)

13 篇文章 1 订阅
13 篇文章 0 订阅

-ls 查看命令

hdfs dfs -ls  路径         查看该路径下的文件和目录,只看一层
hdfs dfs -ls -R  路径    递归查看该路径下的所有目录和文件
hdfs dfs -ls -h 路径      将文件或目录的大小以人类能够理解的方式呈现 K  M等
hdfs dfs -ls -d 路径      仅显示当前路径的文件或目录的信息

hdfs dfs -ls /    <----查看hdfs根目录
hdfs dfs -ls hdfs://192.168.10.101:8020/    <!--这里192.168.10.101是博主运行的hdfs中的namenode所在节点,8020是namenode接受datanode的心跳反馈信息服务的端口号,不同版本的hadoop会不同,hadoop1.x 9000   hadoop2.x  8020-->

----->结果是一样的,都是下面的  因此 命令中的 /  表示hdfs://192.168.10.101:8020/,也就是写/就可以省略这些
drwxr-xr-x   - root supergroup          0 2020-06-12 14:19 hdfs://192.168.10.101:8020/data
drwxr-xr-x   - root supergroup          0 2020-06-12 08:52 hdfs://192.168.10.101:8020/datasource
drwxr-xr-x   - root supergroup          0 2020-06-12 14:23 hdfs://192.168.10.101:8020/output
drwxr-xr-x   - root supergroup          0 2020-06-12 12:47 hdfs://192.168.10.101:8020/output1
drwxr-xr-x   - root supergroup          0 2020-06-12 01:16 hdfs://192.168.10.101:8020/start_mulu
drwx------   - root supergroup          0 2020-06-12 01:14 hdfs://192.168.10.101:8020/tmp

----->还可以通过hdfs命令查看本地文件 file:///表示本地文件
[root@hdp01 ~]# hdfs dfs -ls file:///usr/local/hadoop  
Found 12 items
-rw-r--r--   1 20415 101       86424 2018-04-18 09:39 file:///usr/local/hadoop/LICENSE.txt
-rw-r--r--   1 20415 101       14978 2018-04-18 09:39 file:///usr/local/hadoop/NOTICE.txt
-rw-r--r--   1 20415 101        1366 2018-04-18 09:39 file:///usr/local/hadoop/README.txt
drwxr-xr-x   - 20415 101         194 2020-06-12 21:04 file:///usr/local/hadoop/bin
drwxr-xr-x   - 20415 101          20 2020-06-12 00:29 file:///usr/local/hadoop/etc
drwxr-xr-x   - 20415 101         106 2018-04-18 09:39 file:///usr/local/hadoop/include
drwxr-xr-x   - 20415 101          20 2018-04-18 09:39 file:///usr/local/hadoop/lib
drwxr-xr-x   - 20415 101         239 2018-04-18 09:39 file:///usr/local/hadoop/libexec
drwxr-xr-x   - root  root       4096 2020-06-12 22:14 file:///usr/local/hadoop/logs
drwxr-xr-x   - 20415 101        4096 2018-04-18 09:39 file:///usr/local/hadoop/sbin
drwxr-xr-x   - 20415 101          31 2018-04-18 09:39 file:///usr/local/hadoop/share
drwxr-xr-x   - root  root         37 2020-06-12 01:11 file:///usr/local/hadoop/tmp

-mkdir 创建目录

hdfs dfs -mkdir  路径   创建单个目录 (路径可以为多个参数)
hdfs dfs -mkdir -p 路径 同时创建多级目录 (路径可以为多个参数)

-put 上传文件

hdfs dfs -put  需上传文件路径   目的地文件路径

-copyFromLocal 将本地文件复制到hdfs中

hdfs dfs -copyFromLocal  源路径  目标路径   将本地文件拷贝到hdfs中,源路径可以有多个

-copyToLocal 将hdfs中的文件拷贝到本地

hdfs dfs -copyToLocal  源路径  目标路径   将hdfs文件或目录拷贝至本地,这里可以有多个源路径

-get 下载文件

hdfs dfs -get hdfs文件系统的文件路径   本地的路径   (可以复制整个目录)
hdfs dfs -get -ignoreCrc   hdfs文件系统的文件路径   本地的路径    复制文件和CRC校验失败的文件。
hdfs dfsd -get -crc  hdfs文件系统的文件路径   本地的路径   复制文件和CRC

-moveFromLocal 将本地文件移动到hdfs中

hdfs dfs -moveFromLocal 源文件路径 目标文件路径    将本地的文件或目录移动到hdfs中的相应路径(源文件可以有多个)

-moveToLocal 功能尚未实现

-du 查看文件或目录的大小

hdfs dfs -du 路径      分别显示当前路径下的文件和目录的大小,单位字节
hdfs dfs -du -s  路径   显示当前路径下的总的文件或者目录的大小
hdfs dfs -du -h 路径   以人类能够理解的方式显示每个文件和目录的大小

-df 显示文件系统的容量,可用空间和已用空间。

如果文件系统具有多个分区,并且未指定到特定分区的路径,则将显示根分区的状态。

hdfs dfs -df    显示根分区(根路径)的状态
hdfs dfs -df   分区路径   显示该分区的容量,可用空间和已用空间
hdfs dfs -df -h 分区路径  以人类能够理解的方式显示分区的容量,可用空间和已用空间

-find 在hdfs中查找符合筛选条件的文件

hdfs dfs -find 路径 -name/-iname 筛选的表达式 [-print/-print0]   -iname 大小写不区分  -print打印一条信息换行  -print0 所有信息在一行中进行显示
hdfs dfs -find / -name "s*" -print  将筛选出来的文件信息一个文件一行的形式进行打印.如果是-print0则所有信息将在一行进行输出 


[root@hdp01 ~]# hdfs dfs -find / -name "s*" -print
/salary.txt
/start_mulu
/tmp/hadoop-yarn/staging
[root@hdp01 ~]# hdfs dfs -find / -name "s*" -print0
/salary.txt/start_mulu/tmp/hadoop-yarn/staging[root@hdp01 ~]#

-cp 在hdfs文件系统中,从源路径复制文件到目标路径

(注意全程都是在hdfs系统中)

hdfs dfs -cp 源路径  目标路径    复制文件 (这里源路径可以是多个)

-mv 在hdfs系统中,将源路径下的文件或目录移动到目标路径

hdfs dfs -mv 源路径 目标路径     (源路径可以有多个)

-appendToFile 将本地文件中的内容追加到hdfs的文件中

(注意是追加不是覆盖)

hdfs dfs -appendToFile 源路径文件  目标文件路径  源文件可以是多个

hdfs dfs -appendToFile - 目标文件路径   (当源文件路径为 - 时,目标文件中追加的内容将从校准输入中进行读取,用ctrl + c 结束读取)

-cat 查看hdfs文件中的内容

hdfs dfs -cat 目标路径文件

-count 在hdfs文件系统中统计当前路径下总的目录数,文件数,以及文件总大小

hdfs dfs -count 需要进行统计的目录

[root@hdp01 ~]# hdfs dfs -count /test
           1            2               1166 /test

-chgrp 在hdfs系统中,修改文件或目录的组

hdfs dfs -chgrp 修改后的组   需要修改的组的路径 (只修改当前路径的文件或目录)
hdfs dfs -chgrp -R 修改后的组  需要修改的组的路径 (递归修改当前路径下的所有文件和目录的组)

-chmod 在hdfs系统中修改文件或目录的权限

hdfs dfs -chmod 修改后的权限   需要修改全限的文件或目录路径  (只修改当前路径下的文件或目录的权限,这里的权限用三个数字表示,比如777表示文件拥有者,同组的用户,以及其他用户都具有可读,可写,可执行权限)
hdfs dfs -chmod -R 修改后的权限 需要修改权限的文件或路径    递归修改该路径下的所有文件和目录的权限

-chown 在hdfs中 可以同时修改文件或目录的拥有者和组,也可以单独修改拥有者

hdfs dfs -chown 修改后的拥有者:组 文件路径     同时修改拥有者和组
hdfs dfs -chown 修改后的拥有者  文件路径       只修改组

-getmerge 将hdfs中的多个文件或目录合并到本地的一个文件中,文件可以是不存在的

hdfs dfs -getmerge 源文件或目录的路径  合并后的本地文件路径  (源文件路径可以有多个,合并后的文件如果之前存在就直接覆盖,如果不存在则会创建)

-rm 删除指定路径下的所有文件和目录

hdfs dfs -rm 路径 
hdfs dfs -rm -r 路径  可以删除指定路径下的文件和目录
hdfs dfs -rm -f 路径   当路径不存在时,不会抛出异常
[root@hdp01 ~]# hdfs dfs -rm  /llldfldlfs  删除一个不存在的文件时,会抛出异常
rm: `/llldfldlfs': No such file or directory
[root@hdp01 ~]# hdfs dfs -rm  -f /llldfldlfs  -f选项在删除不存在的文件时,不会出现异常
[root@hdp01 ~]#

-rmdir 删除空目录

hdfs dfs -rmdir 目录路径


<!-- 这里/test1不是空目录,/test3是空目录 -->
[root@hdp01 ~]# hdfs dfs -rmdir /test1
rmdir: `/test1': Directory is not empty   ---->因为不是空文件夹,所以报错
[root@hdp01 ~]# hdfs dfs -rmdir --ignore-fail-on-non-empty /test1   --->--ignore-fail-on-non-empty 在目录为非空状态时不在提示错误,但也不会删除
[root@hdp01 ~]# hdfs dfs -rmdir /test3  ---->空目录完成删除

-stat 将指定路径下的文件和目录信息以指定的格式显示

(只显示当前路径,如比是个目录,那么只显示这个目录,是个文件就显示这个文件信息) 格式接受文件大小块(%b),类型(%F),所有者的组名(%g),名称(%n),块大小(%o),复制(%r),用户名
所有者(%u),修改日期(%y,%Y),%y将UTC日期显示为“ yyyy-MM-dd HH:mm:ss”,并且%Y显示自1970年1月1日UTC以来的毫秒数,如果未指定格式,则默认使用%y。

hdfs dfs -stat "%F %g %n %u %b %y" /output

[root@hdp01 ~]# hdfs dfs -stat "%F %g %n %u %b %y" /output
directory supergroup output root 0 2020-06-12 06:23:36

-tail 在hdfs中显示文件的最后1k的内容

hdfs dfs -tail 目标文件路径              显示文件的最后1k的内容 
hdfs dfs -tail -f 目标文件路径           显示文件最后1k的内容,然后会进入阻塞状态,如果这个文件有新的内容加入,会继续显示

[root@hdp01 ~]# hdfs dfs -tail /kkk   会显示/kkk文件最后1k的内容,命令执行结束
[root@hdp01 ~]# hdfs dfs -tail -f  /kkk 显示1k的内容后进入阻塞,然后用另外一个虚拟机执行下面的命令
[root@hdp02 ~]# hdfs dfs -appendToFile ./aaa /kkk   向/kkk文件中追加了内容
接着在hdp01的虚拟机中会显示更才追加的内容

-touchz 创建长度为0的文件

hdfs dfs -touchz 目标文件路径    创建一个长度为0的文件

[root@hdp01 ~]# hdfs dfs -touchz /gggg
[root@hdp01 ~]# hdfs dfs -ls /gggg
-rw-r--r--   3 root supergroup          0 2020-06-13 02:51 /gggg   ---->可以看到文件的大小是0字节

-truncate 将指定路径下的文件变成指定长度(大小)

hdfs dfs -truncate 指定文件大小 目标文件路径   

[root@hdp01 ~]# hdfs dfs -ls /anaconda-ks.cfg   <---先查看一下文件大小为1000字节
-rw-r--r--   3 root supergroup       1000 2020-06-13 02:56 /anaconda-ks.cfg
[root@hdp01 ~]# hdfs dfs -truncate 10000 /anaconda-ks.cfg   <---想将文件大小变成10000字节,但是抛出异常,描述为不能设置大于原文件大小的值

-truncate: Cannot truncate to a larger file size. Current size: 1000, truncate size: 10000.
Usage: hadoop fs [generic options] -truncate [-w] <length> <path> ...
[root@hdp01 ~]# hdfs dfs -truncate 999 /anaconda-ks.cfg   设置文件大小为999成功
Truncating /anaconda-ks.cfg to length: 999. Wait for block recovery to complete before further updating this file.
[root@hdp01 ~]# hdfs dfs -ls /anaconda-ks.cfg    <---查看文件大小确认修改成功
-rw-r--r--   3 root supergroup        999 2020-06-13 02:57 /anaconda-ks.cfg

-help 帮助命令,会显示每个命令的选项的含义

---->当然你会发现其实直接写hdfs dfs回车也是可以出现哪些命令的,只是和-help相比,少了每个选项的含义

hdfs dfs -help          显示所有的hdfs dfs 命令
hdfs dfs -help ls       显示-ls的相关命令,指定命令的时候前面的 - 不要加


[root@hdp01 ~]# hdfs dfs -help ls
-ls [-d] [-h] [-R] [<path> ...] :
  List the contents that match the specified file pattern. If path is not
  specified, the contents of /user/<currentUser> will be listed. Directory entries
  are of the form:
        permissions - userId groupId sizeOfDirectory(in bytes)
  modificationDate(yyyy-MM-dd HH:mm) directoryName

  and file entries are of the form:
        permissions numberOfReplicas userId groupId sizeOfFile(in bytes)
  modificationDate(yyyy-MM-dd HH:mm) fileName

  -d  Directories are listed as plain files.
  -h  Formats the sizes of files in a human-readable fashion rather than a number
      of bytes.
  -R  Recursively list the contents of directories.

-usage 显示命令的用法

hdfs dfs -usage   			   显示所有命令的用法
hdfs dfs -usage 指定某个命令    显示指定命令的用法(命令前面的 - 不要加)
 
[root@hdp01 ~]# hdfs dfs -usage ls     <----仅显示-ls命令的用法
Usage: hadoop fs [generic options] -ls [-d] [-h] [-R] [<path> ...]

-test 加上不同的选项可以获得文件或者目录的状态,通常用于判断

    -d  return 0 if <path> is a directory.  如果是目录返回0
    -e  return 0 if <path> exists.          如果文件或目录存在返回0
    -f  return 0 if <path> is a file.       如果是一个文件返回0
    -s  return 0 if file <path> is greater than zero bytes in size. 如果文件大于0字节,返回0
    -z  return 0 if file <path> is zero bytes in size, else return 1. 如果文件只有0字节,则返回0
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值