hadoop2.8.2 FS SHELL

概述

文件系统(FS)shell包含很多很像shell的命令,可以直接和HDFS交互。就像 Local FS,HFTP FS,S3 FS等。FS SHELL 可以用下面的命令执行:

bin/hadoop fs <args>

所有的FS SHELL命令都需要一个URI作为参数,URI的格式为:

scheme://authority/path

对于HDFS来说,scheme就是hdfs。对于Local FS来说scheme是file。scheme和authority是可选的,如果不指定那么就是用配置中的。一个HDFS文件或者目录/parent/child被指定为hdfs://namenodehost/parent/child或者就是简单的/parent/child(那么你配置中已经设置了hdfs://namenodehost)

大部分的FS shell命令都和Unix命令有一样的功能。如果有不同之处会在不同命令出指出,Error信息会被发送到stderr,输出会发送到stdout。
如果使用HDFS,那么hdfs和dfs这两个词是相同的。
对于HDFS来说,可以使用相对路径,当前的工作目录就是HDFS home目录/user/,经常需要手动创建。HDFS home目录可以被隐式的访问,当使用HDFS垃圾目录his,.Trash目录就在home目录中。

下面所有的命令的实战部分都执行在hadoop集群上,如果你还没有搭建好集群环境,请参考:hadoop集群实战

首先我们在hadoop安装目录下创建一个input文件,把etc/hadoop中的xml配置文件都复制到input下以供下面的命令使用。

appendToFile

用法:

hadoop fs -appendToFile <localsrc> ... <dst>

从本地文件系统追加一个或者多个文件到目的文件,也可以从标准输入中追加到目标文件系统。如果目标文件存在则追加到文件末尾,如果不存在则创建。

  • hadoop fs -appendToFile localfile /user/hadoop/hadoopfile
  • hadoop fs -appendToFile localfile1 localfile2 /user/hadoop/hadoopfile
  • hadoop fs -appendToFile localfile hdfs://nn.example.com/hadoop/hadoopfile
  • hadoop fs -appendToFile - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin.

如果成功返回0,如果失败返回1。
下面使用实例演示:
首先添加一个hello目录,命令如下:

bin/hadoop fs -mkdir -p hello
[root@hadoop1 hadoop]# bin/hadoop fs -ls /user/root
Found 1 items
drwxr-xr-x   - root supergroup          0 2017-11-03 11:10 /user/root/hello

然后从本地文件系统中添加文件到HDFS中。命令如下:

[root@hadoop1 hadoop]# bin/hadoop fs -appendToFile input/core-site.xml /user/root/hello/core.xml
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxr-xr-x   - root supergroup          0 2017-11-03 11:21 hello
-rw-r--r--   2 root supergroup          0 2017-11-03 11:13 hello/core.xml

可以看到追加文件成功。

cat

用法:

 hadoop fs -cat [-ignoreCrc] URI [URI ...]

查看文件内容,复制源路径到标准输出。
-ignoreCrc:忽略crc验证。

  • hadoop fs -cat hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
  • hadoop fs -cat file:///file3 /user/hadoop/file4

我们使用cat命令查看上面已经添加的文件,命令如下:

[root@hadoop1 hadoop]# bin/hadoop fs -cat /user/root/hello/core.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
     <property>
            <name>fs.defaultFS</name>
            <value>hdfs://hadoop1:9000</value>
     </property>

    <property>
         <name>io.file.buffer.size</name>
         <value>131072</value>
       </property>

        <!-- 指定hadoop临时目录,自行创建 -->
        <property>
                <name>hadoop.tmp.dir</name>
                <value>/home/software/hadoop/tmp</value>
        </property>
</configuration>

checksum

用法:

hadoop fs -checksum URI

返回文件信息的校验和
实例:

  • hadoop fs -checksum hdfs://nn1.example.com/file1
  • hadoop fs -checksum file:///etc/hosts

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -checksum /user/root/hello/core.xml
/user/root/hello/core.xml   MD5-of-0MD5-of-512CRC32C    00000200000000000000000025675b875006ed49e478ad2f08d676da

chgrp

用法:

hadoop fs -chgrp [-R] GROUP URI [URI ...]

改变文件所属的组,用户必须是文件的所有者,或者是超级用户。-R选项会递归的设置目录。

实战:
上面添加的hello目录以及core.xml的组都是supergroup,我们把它改变成root。

[root@hadoop1 hadoop]# bin/hadoop fs -chgrp -R root /user/root/hello
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxr-xr-x   - root root          0 2017-11-03 11:21 hello
-rw-r--r--   2 root root          0 2017-11-03 11:13 hello/core.xml

修改成功。

chmod

用法:
hadoop fs -chmod [-R]

[root@hadoop1 hadoop]# bin/hadoop fs -chmod -R 777 /user/root/hello
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 11:21 hello
-rwxrwxrwx   2 root root          0 2017-11-03 12:13 hello/core.xm

chown

用法:

 hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]

修改文件的拥有者,必须是超级用户。-R是递归的修改目录结构。此命令可以同时修改文件的owner和group。
实战:
把hello目录的group修改为supergroup。

[root@hadoop1 hadoop]# bin/hadoop fs -chown -R root:supergroup /user/root/hello
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root supergroup          0 2017-11-03 11:21 hello
-rwxrwxrwx   2 root supergroup          0 2017-11-03 12:13 hello/core.xml

copyFromLocal

复制本地文件到HDFS。
用法:

hadoop fs -copyFromLocal <localsrc> URI

除非本地文件是有限制,和-put命令用法相似。
选项:

  • -p:保存访问、修改时间,以及拥有者和权限。(假设权限在文件系统之间是通用的)。
  • -f:如果目标文件存在则覆盖。
  • -l:允许DataNode延迟持久化到硬盘,强制复制因子为1,这个标志会导致耐用性降低,慎用。
  • -d:跳过创建以COPYING为后缀的临时文件。

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -copyFromLocal -f -p input/core-site.xml /user/root/hello
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root supergroup          0 2017-11-03 13:12 hello
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello/core-site.xml
-rwxrwxrwx   2 root supergroup          0 2017-11-03 12:13 hello/core.xml

copyToLocal

从HDFS复制文件到本地。
用法:

hadoop fs -copyToLocal [-ignorecrc] [-crc] URI <localdst>

和get命令相似。
实战:

[root@hadoop1 hadoop]# bin/hadoop fs -copyToLocal -p -ignoreCrc /user/root/hello/core-site.xml /home/software/hadoop
[root@hadoop1 hadoop]# ll
total 76
-rw-r--r--. 1 root  root   1181 Oct 31 18:59 core-site.xml
......

count

用法:

 hadoop fs -count [-q] [-h] [-v] [-x] [-t [<storage type>]] [-u] <paths>

根据指定的路径,返回路径、文件、字节的数量。获得配额和使用,输出列为DIR_COUNT, FILE_COUNT, CONTENT_SIZE, PATHNAME。
-u和-q选项控制哪个列会被显示,-q显示配额,-u显示配额和使用。
如果使用-count和-q输出列为:QUOTA, REMAINING_QUOTA, SPACE_QUOTA, REMAINING_SPACE_QUOTA, DIR_COUNT, FILE_COUNT, CONTENT_SIZE, PATHNAME。
如果使用-count和-u输出列位:QUOTA, REMAINING_QUOTA, SPACE_QUOTA, REMAINING_SPACE_QUOTA

-t选项展示每种存储类型的配额和使用。如果不使用-u和-q选项-t就会被忽略,下列的参数可以使用-t选项:”“, ”all“, ”ram_disk“, ”ssd“, ”disk“ 或”archive”.

-h显示可读的大小
-v展示表头
-x选项排除计算结果的快照,如果使用-u和-q选项-t就会被忽略。
实例:

  • hadoop fs -count hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
  • hadoop fs -count -q hdfs://nn1.example.com/file1
  • hadoop fs -count -q -h hdfs://nn1.example.com/file1
  • hadoop fs -count -q -h -v hdfs://nn1.example.com/file1
  • hadoop fs -count -u hdfs://nn1.example.com/file1
  • hadoop fs -count -u -h hdfs://nn1.example.com/file1
  • hadoop fs -count -u -h -v hdfs://nn1.example.com/file1

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -count /user/root/hello
           1            3               3543 /user/root/hello
[root@hadoop1 hadoop]# bin/hadoop fs -count -q  /user/root/hello
        none             inf            none             inf            1            3               3543 /user/root/hello
[root@hadoop1 hadoop]# bin/hadoop fs -count -q -h /user/root/hello
        none             inf            none             inf            1            3              3.5 K /user/root/hello

cp

用法:

hadoop fs -cp [-f] [-p | -p[topax]] URI [URI ...] <dest>

HDFS中拷贝文件。可以使用多个源文件,但是目的必须是一个目录。
下列情况,‘raw.*’扩展属性会被保存,

  • 来源和目的文件系统都支持
  • 所有源和目的路径在/.reserved/raw中

决定是否保存此属性可以独立使用-p选项。
选项介绍:

  • -f表示如果存在则覆盖
  • -p表示保存扩展属性,(时间戳,拥有者,权限, ACL, XAttr)。如果使用-p不带参数,会保留时间戳,拥有者,权限。如果使用-pa则保留权限。

    实例:

  • hadoop fs -cp /user/hadoop/file1 /user/hadoop/file2

  • hadoop fs -cp /user/hadoop/file1 /user/hadoop/file2 /user/hadoop/dir

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -cp -f -p /user/root/hello/core.xml /user/root/hello/core1.xml
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root supergroup          0 2017-11-03 14:32 hello
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello/core-site.xml
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 hello/core.xml
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 hello/core1.xml

createSnapshot

查看HDFS快照手册

deleteSnapshot

查看HDFS快照手册

df

用法:

hadoop fs -df [-h] URI [URI ...]

展示可用空间。
-h会格式化大小为可读的形式。
实例:

  • hadoop fs -df /user/hadoop/dir1

注意,源文档中使用的实例是:hadoop dfs -df /user/hadoop/dir1.因为dfs已经被弃用了,所以改为fs。
实战:

[root@hadoop1 hadoop]# bin/hadoop fs -df /user/root/hello
Filesystem                  Size    Used    Available  Use%
hdfs://hadoop1:9000  37595959296  122880  28844666880    0%

du

用法:

hadoop fs -du [-s] [-h] [-x] URI [URI ...]

展示给定文件和目录的大小,如果是个文件就显示文件的长度。
选项:

  • -s表示总长度.
  • -h表示可读的大小,(比如:67108864将表示为64.0m)
  • -x表示排除快照

返回三列结果,分别是:size,disk_space_consumed_with_all_replicas ,full_path_name

实例:

  • hadoop fs -du /user/hadoop/dir1 /user/hadoop/file1 hdfs://nn.example.com/user/hadoop/dir1

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -du /user/root/hello
1181  /user/root/hello/core-site.xml
1181  /user/root/hello/core.xml
1181  /user/root/hello/core1.xml
[root@hadoop1 hadoop]# bin/hadoop fs -du -h /user/root/hello
1.2 K  /user/root/hello/core-site.xml
1.2 K  /user/root/hello/core.xml
1.2 K  /user/root/hello/core1.xml

dus

用法:

hadoop fs -dus <args>

展示文件的总长度,相当于使用hadoop fs -du -s命令。

expunge

用法:

hadoop fs -expunge

永久的删除检查点中那些超过阈值的文件,并创建新的检查点。
当建立的检查点,那些trash中最近删除的文件就会移动到检查点中。检查点中的文件超过了fs.trash.interval 就会在指定-expunge命令的时候被删除。

如果文件系统支持,用户可以配置,创建和删除检查点。可以通过core-site.xml中的fs.trash.checkpoint.interval属性执行,这个属性和fs.trash.interval相同。
查看HDFS 架构了解更多的trash特性。

find

用法:

hadoop fs -find <path> ... <expression> ...

查找用户的文件,如果没有指定路径,那就是当前工作目录,如果没有表达式默认就是-print;
建议使用下面的表达式:

  • -name pattern,-iname pattern:文件基本名称匹配就评估为true,如果使用-iname表达大小写敏感。
  • -print,-print0:总是评估为true,因为当前路径名被写到标准输出。如果-print0那么会追加一个ASCII NULL

下面的操作符是推荐使用的:

  • expression -a expression,expression -and expression,expression expression:逻辑AND连接两个表达式。

实例:

hadoop fs -find / -name test -print

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -find /user/root/hello -name core.xml -print
/user/root/hello/core.xml

使用时注意文件的权限。

get

用法:

hadoop fs -get [-ignorecrc] [-crc] [-p] [-f] <src> <localdst>

复制文件到本地文件系统,和copyToLocal命令相似。
选项:

  • -p:保存时间戳,所有者和权限。
  • -f:存在则覆盖。
  • -ignorecrc:忽略crc
  • -crc:包括crc校验和

实例:

  • hadoop fs -get /user/hadoop/file localfile
  • hadoop fs -get hdfs://nn.example.com/user/hadoop/file localfile

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -get -p /user/root/hello/core-site.xml /home/software/hadoop/core2.xml
[root@hadoop1 hadoop]# ll
total 84
-rw-r--r--. 1 root  root   1181 Oct 31 18:59 core2.xml
......

使用时注意文件的权限。

getfacl

用法:

hadoop fs -getfacl [-R] <path>

获得文件的访问控制列表。
选项:

  • -R:递归的列出文件。
  • path:文件或者目录

实例:

  • hadoop fs -getfacl /file
  • hadoop fs -getfacl -R /dir

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -chown root:root /user/root/hello
[root@hadoop1 hadoop]# bin/hadoop fs -getfacl -R /user/root/hello
# file: /user/root/hello
# owner: root
# group: root
user::rwx
group::rwx
other::rwx

# file: /user/root/hello/core-site.xml
# owner: root
# group: root
user::rw-
group::r--
other::r--

# file: /user/root/hello/core.xml
# owner: root
# group: supergroup
user::rwx
group::rwx
other::rwx

# file: /user/root/hello/core1.xml
# owner: root
# group: supergroup
user::rwx
group::rwx
other::rwx

如果你在执行此命令的时候,提示如下:

getfacl: The ACL operation has been rejected.  Support for ACLs has been disabled by setting dfs.namenode.acls.enabled to false.

那么你需要在hdfs-site.xml中加入以下配置:

<property>
    <name>dfs.namenode.acls.enabled</name>
    <value>true</value>
</property>

getfattr

用法:

hadoop fs -getfattr [-R] -n name | -d [-e en] <path>

展示文件和目录的扩展属性。
选项:

  • -R:递归显示
  • -n name:打印名称的扩展属性值
  • -d:所有和路径名相关的扩展属性值
  • -e encoding:获得的时候进行编码,可用的有“text”, “hex”, and “base64”。如果是text就会加上双引号,并且值会进行十六进制和base64编码,前缀0x和 0s。

    实例:

  • hadoop fs -getfattr -d /file

  • hadoop fs -getfattr -R -n user.myAttr /dir

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -getfattr -d -R /user/root/hello
# file: /user/root/hello
# file: /user/root/hello/core-site.xml
# file: /user/root/hello/core.xml
# file: /user/root/hello/core1.xml

getmerge

用法:

hadoop fs -getmerge [-nl] <src> <localdst>

源文件和目的文件作为输入,合并源文件后写入目标文件。可选的-nl选项可以在每个文件的结尾换行,-skip-empty-file可以跳过那些空文件。目标文件为本地文件。
实例:

  • hadoop fs -getmerge -nl /src /opt/output.txt
  • hadoop fs -getmerge -nl /src/file1.txt /src/file2.txt /output.txt

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -getmerge -nl /user/root/hello/core-site.xml /user/root/hello/core.xml /home/software/hadoop/coremerge.xml
[root@hadoop1 hadoop]# more coremerge.xml 
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
     <property>
            <name>fs.defaultFS</name>
            <value>hdfs://hadoop1:9000</value>
     </property>

    <property>
         <name>io.file.buffer.size</name>
         <value>131072</value>
       </property>

        <!-- 指定hadoop临时目录,自行创建 -->
        <property>

.....此处省略文件内容....

ls

用法:

hadoop fs -ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] <args>

选项说明:

  • -C:仅显示文件的路径和目录。
  • -d:目录作为普通文件。
  • -h:格式化大小为可读方式。
  • -q:使用?代替不可打印的字符.
  • -R:递归显示目录下的文件
  • -t:按照修改时间排序。
  • -S:按照文件大小排序.
  • -r:反向排序
  • -u:使用访问时间替换修改时间显示。

返回格式如下:

权限,副本数量,用户,用户组,文件大小,修改日志,修改时间,文件名。

实例:

hadoop fs -ls /user/hadoop/file1

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -ls /user/root/hello/core.xml
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 /user/root/hello/core.xml

lsr

递归显示目录中的文件,此命令就相当于使用:hadoop fs -ls -R。也建议使用ls命令代替。
实战:

[root@hadoop1 hadoop]# bin/hadoop fs -lsr /user/root/hello
lsr: DEPRECATED: Please use 'ls -R' instead.
-rw-r--r--   2 root root             1181 2017-10-31 18:59 /user/root/hello/core-site.xml
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 /user/root/hello/core.xml
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 /user/root/hello/core1.xml

可以看到此命令已经被弃用了。

mkdir

用法:

hadoop fs -mkdir [-p] <paths>

-p表示如果没有父目录则创建。

实例:

  • hadoop fs -mkdir /user/hadoop/dir1 /user/hadoop/dir2
  • hadoop fs -mkdir hdfs://nn1.example.com/user/hadoop/dir hdfs://nn2.example.com/user/hadoop/dir

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -mkdir -p  /user/root/hello/hello1/hello2/hello3
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 17:10 hello
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello/core-site.xml
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 hello/core.xml
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 hello/core1.xml
drwxr-xr-x   - root root                0 2017-11-03 17:10 hello/hello1
drwxr-xr-x   - root root                0 2017-11-03 17:10 hello/hello1/hello2
drwxr-xr-x   - root root                0 2017-11-03 17:10 hello/hello1/hello2/hello3

moveFromLocal

用法:

hadoop fs -moveFromLocal <localsrc> <dst>

和put命令相似,不同的是本地文件会被删除。

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -moveFromLocal input/hdfs-site.xml /user/root/hello
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 17:14 hello
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello/core-site.xml
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 hello/core.xml
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 hello/core1.xml
-rw-r--r--   2 root root             1244 2017-11-03 17:14 hello/hdfs-site.xml
drwxr-xr-x   - root root                0 2017-11-03 17:10 hello/hello1
drwxr-xr-x   - root root                0 2017-11-03 17:10 hello/hello1/hello2
drwxr-xr-x   - root root                0 2017-11-03 17:10 hello/hello1/hello2/hello3
[root@hadoop1 hadoop]# ls input/
capacity-scheduler.xml  core-site.xml  hadoop-policy.xml  httpfs-site.xml  kms-acls.xml  kms-site.xml  mapred-site.xml  yarn-site.xml

看到input目录中的hdfs-site.xml已经被删除了。

moveToLocal

用法:

hadoop fs -moveToLocal [-crc] <src> <dst>

此命令还没有实现,会提示:
实战:

[root@hadoop1 hadoop]# bin/hadoop fs -moveToLocal /user/root/hello/hdfs-site.xml input
moveToLocal: Option '-moveToLocal' is not implemented yet.

mv

用法:

hadoop fs -mv URI [URI ...] <dest>

移动文件,可以多文件移动,但是目标必须是一个目录。跨平台移动文件是不允许的。
实例:

  • hadoop fs -mv /user/hadoop/file1 /user/hadoop/file2
  • hadoop fs -mv hdfs://nn.example.com/file1 hdfs://nn.example.com/file2 hdfs://nn.example.com/file3 hdfs://nn.example.com/dir1

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -mv /user/root/hello/core.xml /user/root/hello/core1.xml /user/root/hello/hello1
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 17:51 hello
-rw-r--r--   2 root root       1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root       1244 2017-11-03 17:14 hello/hdfs-site.xml
drwxr-xr-x   - root root          0 2017-11-03 17:51 hello/hello1
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 hello/hello1/core.xml
-rwxrwxrwx   2 root supergroup       1181 2017-11-03 11:21 hello/hello1/core1.xml
drwxr-xr-x   - root root                0 2017-11-03 17:10 hello/hello1/hello2
drwxr-xr-x   - root root                0 2017-11-03 17:10 hello/hello1/hello2/hello3

put

用法:

hadoop fs -put [-f] [-p] [-l] [-d] [ - | <localsrc1> .. ]. <dst>

从本地复制一个或多个文件到目标文件系统。也可以从标准输入中读取。
选项说明:

  • -p:保留访问和修改时间,拥有者和权限。
  • -f:如果存在则覆盖
  • -l:允许延迟持久化,会导致耐久性降低,慎用。
  • -d:跳过创建临时文件。

实例:

  • hadoop fs -put localfile /user/hadoop/hadoopfile
  • hadoop fs -put -f localfile1 localfile2 /user/hadoop/hadoopdir
  • hadoop fs -put -d localfile hdfs://nn.example.com/hadoop/hadoopfile
  • hadoop fs -put - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin.

和copyFromLocal类似,这里就不在实战演示了。

rm

用法:

hadoop fs -rm [-f] [-r |-R] [-skipTrash] [-safely] URI [URI ...]

删除指定的文件。如果垃圾文件允许的话,被删除的文件会保存到垃圾目录。当前垃圾特性默认是不可用的。我们可以通过设置core-site.xml中的fs.trash.interval来开启。
查看expunge命令来删除垃圾中的文件。
选项说明:

  • -f:如果文件不存在,不显示提示消息。
  • -R:递归删除。
  • -r:和-R相同
  • -skipTrash:如果垃圾特性开启,此选项不会把删除的文件放入垃圾目录。
  • -safely:安全删除,当你删除文件的数量大于core-site.xml中hadoop.shell.delete.limit.num.files属性值时,会让你确认是否删除。可以和-skipTrash选项一起使用防止意外删除大量文件。

实例:

  • hadoop fs -rm hdfs://nn.example.com/file /user/hadoop/emptydir

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -rm -R /user/root/hello/hello1
17/11/03 18:06:33 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes.
Deleted /user/root/hello/hello1
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 18:06 hello
-rw-r--r--   2 root root       1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root       1244 2017-11-03 17:14 hello/hdfs-site.xml

这里没有开启垃圾特性,并且没有使用-skipTrash选项,所以打印出一条提示消息。

rmdir

用法:

hadoop fs -rmdir [--ignore-fail-on-non-empty] URI [URI ...]

删除一个目录,–ignore-fail-on-non-empty表示如果目录中包含文件不会显示提示消息。

实例:

  • hadoop fs -rmdir /user/hadoop/emptydir

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -mkdir /user/root/hello1
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root                0 2017-11-03 18:06 hello
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root             1244 2017-11-03 17:14 hello/hdfs-site.xml
drwxr-xr-x   - root supergroup          0 2017-11-03 18:12 hello1
[root@hadoop1 hadoop]# bin/hadoop fs -cp -p /user/root/hello/core-site.xml /user/root/hello1/core.xml  
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root                0 2017-11-03 18:06 hello
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root             1244 2017-11-03 17:14 hello/hdfs-site.xml
drwxr-xr-x   - root supergroup          0 2017-11-03 18:17 hello1
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello1/core.xml
[root@hadoop1 hadoop]# bin/hadoop fs -rmdir /user/root/hello1
rmdir: `/user/root/hello1': Directory is not empty
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root                0 2017-11-03 18:06 hello
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root             1244 2017-11-03 17:14 hello/hdfs-site.xml
drwxr-xr-x   - root supergroup          0 2017-11-03 18:17 hello1
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello1/core.xml
[root@hadoop1 hadoop]# bin/hadoop fs -rmdir --ignore-fail-on-non-empty /user/root/hello1
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root                0 2017-11-03 18:06 hello
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root             1244 2017-11-03 17:14 hello/hdfs-site.xml
drwxr-xr-x   - root supergroup          0 2017-11-03 18:17 hello1
-rw-r--r--   2 root root             1181 2017-10-31 18:59 hello1/core.xml
[root@hadoop1 hadoop]# bin/hadoop fs -rm /user/root/hello1/core.xml
17/11/03 18:22:32 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes.
Deleted /user/root/hello1/core.xml
[root@hadoop1 hadoop]# bin/hadoop fs -rmdir  /user/root/hello1
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 18:06 hello
-rw-r--r--   2 root root       1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root       1244 2017-11-03 17:14 hello/hdfs-site.xml

rmr

用法:

hadoop fs -rmr [-skipTrash] URI [URI ...]

递归删除,相当于:-rm -r。此命令已废弃,不建议使用。

setfacl

用法:

hadoop fs -setfacl [-R] [-b |-k -m |-x <acl_spec> <path>] |[--set <acl_spec> <path>]

设置文件和目录的访问控制列表(ACL)。

选项说明:

  • -b:除了ACL删除所有属性和用户的拥有者和组.其他用户的保留。
  • -k:删除默认的ACL
  • -R:递归
  • -m:修改ACL,新的ACL将会添加,之前存在的也会保留
  • -x:删除指定的ACL,其他ACL保留。
  • –set:全替换ACL,
  • acl_spec:逗号分隔的ACL。
  • path:文件或者目录

实例:

  • hadoop fs -setfacl -m user:hadoop:rw- /file
  • hadoop fs -setfacl -x user:hadoop /file
  • hadoop fs -setfacl -b /file
  • hadoop fs -setfacl -k /dir
  • hadoop fs -setfacl –set user::rw-,user:hadoop:rw-,group::r–,other::r– /file
  • hadoop fs -setfacl -R -m user:hadoop:r-x /dir
  • hadoop fs -setfacl -m default:user:hadoop:r-x /dir

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 18:06 hello
-rw-r--r--   2 root root       1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root       1244 2017-11-03 17:14 hello/hdfs-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -setfacl -m user:root:rwx /user/root/hello/core-site.xml 
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 18:06 hello
-rw-rwxr--+  2 root root       1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root       1244 2017-11-03 17:14 hello/hdfs-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -setfacl -x user:root /user/root/hello/core-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 18:06 hello
-rw-r--r--+  2 root root       1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root       1244 2017-11-03 17:14 hello/hdfs-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -setfacl -b /user/root/hello/core-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 18:06 hello
-rw-r--r--   2 root root       1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root       1244 2017-11-03 17:14 hello/hdfs-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -setfacl -k /user/root/hello/core-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 18:06 hello
-rw-r--r--   2 root root       1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root       1244 2017-11-03 17:14 hello/hdfs-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -setfacl --set user::rw-,user:root:rw-,group::r--,other::r-- /user/root/hello/core-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 18:06 hello
-rw-rw-r--+  2 root root       1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root       1244 2017-11-03 17:14 hello/hdfs-site.xml

setfattr

用法:

 hadoop fs -setfattr -n name [-v value] | -x name <path>

为文件和目录设置扩展属性和值。
选项说明:

  • -b:除了基础的ACL删除其他所有属性,所有者,组和其他保留。
  • -n name:扩展属性名字
  • -v value:扩展属性值。有三种不同的编码方式。双引号表示字符串,前缀0x或者0X表示十六进制,0s或者0S表示base64。
  • -x name:删除扩展属性。
  • path:文件或者目录。

实例:

  • hadoop fs -setfattr -n user.myAttr -v myValue /file
  • hadoop fs -setfattr -n user.noValue /file
  • hadoop fs -setfattr -x user.myAttr /file

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -setfattr -n user.myAttr -v myValue /user/root/hello/core-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -getfattr -n user.myAttr /user/root/hello/core-site.xml
# file: /user/root/hello/core-site.xml
user.myAttr="myValue"

setrep

用法:

hadoop fs -setrep [-R] [-w] <numReplicas> <path>

改变文件的复制因子,如果path是一个目录,这个命令会递归的修改这个目录下所有的文件。
选项说明:

  • -w:等待命令结果,可能会等待一段时间。
  • -R:向后兼容,没什么作用。
    实例:
  • hadoop fs -setrep -w 3 /user/hadoop/dir1

stat

用法:

 hadoop fs -stat [format] <path> ...

打印格式化后的文件或目录的信息,
格式化说明:

  • %b:文件块大小
  • %F:类型
  • %g:拥有者组名
  • %n:名称
  • %o:块大小
  • %r:副本
  • %u:所有者用户名
  • %y/Y:修改时间

实例:

hadoop fs -stat "%F %u:%g %b %y %n" /file

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -stat "%F %u:%g %b %y %n %r %o" /user/root/hello/core-site.xml
regular file root:root 1181 2017-10-31 10:59:13 core-site.xml 2 134217728

tail

用法:

hadoop fs -tail [-f] URI

显示文件结尾的一千字节
-f:如果文件增长,则追加输出。和linux中相似。

test

用法:

hadoop fs -test -[defsz] URI

选项说明:

  • -d:路径是目录,返回0
  • -e:路径存在,返回0
  • -f:路径是文件,返回0
  • -s:不空,返回0
  • -z:长度是0返回0

实例:

hadoop fs -test -e filenam

text

用法:

hadoop fs -text <src>

把文件进行文本格式化。允许zip和TextRecordInputStream

touchz

用法:

hadoop fs -touchz URI [URI ...]

创建一个空文件。
实例:

hadoop fs -touchz pathname

实战:

[root@hadoop1 hadoop]# bin/hadoop fs -touchz /user/root/hello/aaa.xml
[root@hadoop1 hadoop]# bin/hadoop fs -ls -R
drwxrwxrwx   - root root          0 2017-11-03 19:18 hello
-rw-r--r--   2 root root          0 2017-11-03 19:18 hello/aaa.xml
-rw-rw-r--+  2 root root       1181 2017-10-31 18:59 hello/core-site.xml
-rw-r--r--   2 root root       1244 2017-11-03 17:14 hello/hdfs-site.xml
[root@hadoop1 hadoop]# bin/hadoop fs -cat /user/root/hello/aaa.xml
[root@hadoop1 hadoop]# 

truncate

用法:

 hadoop fs -truncate [-w] <length> <paths>

截断文件为指定长度。
选项说明:

  • -w:等待块恢复完成。不适用此选项这个文件可能会存在一段时间,为了恢复使用。在这段时间内,文件不能追加内容。

实例:

  • hadoop fs -truncate 55 /user/hadoop/file1 /user/hadoop/file2
  • hadoop fs -truncate -w 127 hdfs://nn1.example.com/user/hadoop/file1

usage

用法:

hadoop fs -usage command

返回命令的帮助文档。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值