1. 程式人生 > >hadoop2.8.2 FS SHELL

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命令檢視上面已經新增的檔案,命令如下:

[[email protected] 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

實戰:

[[email protected] hadoop]# bin/hadoop fs -cp -f -p /user/root/hello/core.xml /user/root/hello/core1.xml
[[email protected] 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

deleteSnapshot

df

用法:

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

展示可用空間。
-h會格式化大小為可讀的形式。
例項:

  • hadoop fs -df /user/hadoop/dir1

注意,源文件中使用的例項是:hadoop dfs -df /user/hadoop/dir1.因為dfs已經被棄用了,所以改為fs。
實戰:

[[email protected] 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

實戰:

[[email protected] 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
[[email protected] 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

實戰:

[[email protected] 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

實戰:

[[email protected] hadoop]# bin/hadoop fs -getmerge -nl /user/root/hello/core-site.xml /user/root/hello/core.xml /home/software/hadoop/coremerge.xml
[[email protected] 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命令代替。
實戰:

[[email protected] 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

實戰:

[[email protected] hadoop]# bin/hadoop fs -mkdir -p  /user/root/hello/hello1/hello2/hello3
[[email protected] 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命令相似,不同的是本地檔案會被刪除。

實戰:

[[email protected] hadoop]# bin/hadoop fs -moveFromLocal input/hdfs-site.xml /user/root/hello
[[email protected] 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
[[email protected] 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

實戰:

[[email protected] hadoop]# bin/hadoop fs -mv /user/root/hello/core.xml /user/root/hello/core1.xml /user/root/hello/hello1
[[email protected] 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

實戰:

[[email protected] 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
[[email protected] 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

實戰:

[[email protected] hadoop]# bin/hadoop fs -mkdir /user/root/hello1
[[email protected] 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
[[email protected] hadoop]# bin/hadoop fs -cp -p /user/root/hello/core-site.xml /user/root/hello1/core.xml  
[[email protected] 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 hell