hadoop FS Shell

来源:互联网 发布:淘宝直通车黑车 编辑:程序博客网 时间:2024/04/28 10:40

[root@redhat5 ~]# hadoop fs -help


hadoop fs is the command to execute fs commands. The full syntax is: 
hadoop fs [-fs <local | file system URI>] [-conf <configuration file>]
[-D <property=value>] [-ls <path>] [-lsr <path>] [-du <path>]
[-dus <path>] [-mv <src> <dst>] [-cp <src> <dst>] [-rm [-skipTrash] <src>]
[-rmr [-skipTrash] <src>] [-put <localsrc> ... <dst>] [-copyFromLocal <localsrc> ... <dst>]
[-moveFromLocal <localsrc> ... <dst>] [-get [-ignoreCrc] [-crc] <src> <localdst>
[-getmerge <src> <localdst> [addnl]] [-cat <src>]
[-copyToLocal [-ignoreCrc] [-crc] <src> <localdst>] [-moveToLocal <src> <localdst>]
[-mkdir <path>] [-report] [-setrep [-R] [-w] <rep> <path/file>]
[-touchz <path>] [-test -[ezd] <path>] [-stat [format] <path>]
[-tail [-f] <path>] [-text <path>]
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-chgrp [-R] GROUP PATH...]
[-count[-q] <path>]
[-help [cmd]]


-fs [local | <file system URI>]: Specify the file system to use.
If not specified, the current configuration is used, 
taken from the following, in increasing precedence: 
core-default.xml inside the hadoop jar file 
core-site.xml in $HADOOP_CONF_DIR 
'local' means use the local file system as your DFS. 
<file system URI> specifies a particular file system to 
contact. This argument is optional but if used must appear
appear first on the command line.  Exactly one additional
argument must be specified. 


-ls <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 
dirName (full path) <dir> 
and file entries are of the form 
fileName(full path) <r n> size 
where n is the number of replicas specified for the file 
and size is the size of the file, in bytes.


-lsr <path>: Recursively list the contents that match the specified
file pattern.  Behaves very similarly to hadoop fs -ls,
except that the data is shown for all the entries in the
subtree.


-du <path>: Show the amount of space, in bytes, used by the files that 
match the specified file pattern.  Equivalent to the unix
command "du -sb <path>/*" in case of a directory, 
and to "du -b <path>" in case of a file.
The output is in the form 
name(full path) size (in bytes)


-dus <path>: Show the amount of space, in bytes, used by the files that 
match the specified file pattern.  Equivalent to the unix
command "du -sb"  The output is in the form 
name(full path) size (in bytes)


-mv <src> <dst>:   Move files that match the specified file pattern <src>
to a destination <dst>.  When moving multiple files, the 
destination must be a directory. 


-cp <src> <dst>:   Copy files that match the file pattern <src> to a 
destination.  When copying multiple files, the destination
must be a directory. 


-rm [-skipTrash] <src>: Delete all files that match the specified file pattern.
Equivalent to the Unix command "rm <src>"
-skipTrash option bypasses trash, if enabled, and immediately
deletes <src>
-rmr [-skipTrash] <src>: Remove all directories which match the specified file 
pattern. Equivalent to the Unix command "rm -rf <src>"
-skipTrash option bypasses trash, if enabled, and immediately
deletes <src>
-put <localsrc> ... <dst>: Copy files from the local file system 
into fs. 


-copyFromLocal <localsrc> ... <dst>: Identical to the -put command.


-moveFromLocal <localsrc> ... <dst>: Same as -put, except that the source is
deleted after it's copied.


-get [-ignoreCrc] [-crc] <src> <localdst>:  Copy files that match the file pattern <src> 
to the local name.  <src> is kept.  When copying mutiple, 
files, the destination must be a directory. 


-getmerge <src> <localdst>:  Get all the files in the directories that 
match the source file pattern and merge and sort them to only
one file on local fs. <src> is kept.


-cat <src>: Fetch all files that match the file pattern <src> 
and display their content on stdout.


-copyToLocal [-ignoreCrc] [-crc] <src> <localdst>:  Identical to the -get command.


-moveToLocal <src> <localdst>:  Not implemented yet 


-mkdir <path>: Create a directory in specified location. 


-setrep [-R] [-w] <rep> <path/file>:  Set the replication level of a file. 
The -R flag requests a recursive change of replication level 
for an entire tree.


-tail [-f] <file>:  Show the last 1KB of the file. 
The -f option shows apended data as the file grows. 


-touchz <path>: Write a timestamp in yyyy-MM-dd HH:mm:ss format
in a file at <path>. An error is returned if the file exists with non-zero length


-test -[ezd] <path>: If file { exists, has zero length, is a directory
then return 0, else return 1.


-text <src>: Takes a source file and outputs the file in text format.
The allowed formats are zip and TextRecordInputStream.


-stat [format] <path>: Print statistics about the file/directory at <path>
in the specified format. Format accepts filesize in blocks (%b), filename (%n),
block size (%o), replication (%r), modification date (%y, %Y)


-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...
Changes permissions of a file.
This works similar to shell's chmod with a few exceptions.


-R modifies the files recursively. This is the only option
currently supported.


MODE Mode is same as mode used for chmod shell command.
Only letters recognized are 'rwxX'. E.g. a+r,g-w,+rwx,o=r


OCTALMODE Mode specifed in 3 digits. Unlike shell command,
this requires all three digits.
E.g. 754 is same as u=rwx,g=rx,o=r


If none of 'augo' is specified, 'a' is assumed and unlike
shell command, no umask is applied.


-chown [-R] [OWNER][:[GROUP]] PATH...
Changes owner and group of a file.
This is similar to shell's chown with a few exceptions.


-R modifies the files recursively. This is the only option
currently supported.


If only owner or group is specified then only owner or
group is modified.


The owner and group names may only cosists of digits, alphabet,
and any of '-_.@/' i.e. [-_.@/a-zA-Z0-9]. The names are case
sensitive.


WARNING: Avoid using '.' to separate user name and group though
Linux allows it. If user names have dots in them and you are
using local file system, you might see surprising results since
shell command 'chown' is used for local files.


-chgrp [-R] GROUP PATH...
This is equivalent to -chown ... :GROUP ...


-count[-q] <path>: Count the number of directories, files and bytes under the paths
that match the specified file pattern.  The output columns are:
DIR_COUNT FILE_COUNT CONTENT_SIZE FILE_NAME or
QUOTA REMAINING_QUATA SPACE_QUOTA REMAINING_SPACE_QUOTA 
     DIR_COUNT FILE_COUNT CONTENT_SIZE FILE_NAME
-help [cmd]: Displays help for given command or all commands if none
is specified.


一些常见的命令:


[root@redhat5 bin]# hadoop fs -ls /

[root@redhat5 bin]# hadoop fs -lsr /

[root@redhat5 bin]# hadoop fs -lsr /wc


在本地创建一个文件夹和一个文件:


[root@redhat5 /]# pwd
/
[root@redhat5 /]# cd ..
[root@redhat5 /]# ls
bin   dev  gaozhen  lib    lost+found  misc  myoracle  opt   root  selinux  sys       tmp  var
boot  etc  home     lib64  media       mnt   net       proc  sbin  srv      tftpboot  usr
[root@redhat5 /]# mkdir data
[root@redhat5 /]# cd data
[root@redhat5 data]# touch 01.data
[root@redhat5 data]# ls
01.data
[root@redhat5 data]# vim 01.data
[root@redhat5 data]# 


-put上传命令:


[root@redhat5 data]# hadoop fs -mkdir /wc/test
[root@redhat5 data]# hadoop fs -ls /wc

Found 3 items
drwxr-xr-x   - root supergroup          0 2016-01-20 06:21 /wc/input
drwxr-xr-x   - root supergroup          0 2016-01-20 22:31 /wc/output
drwxr-xr-x   - root supergroup          0 2016-01-22 21:55 /wc/test



[root@redhat5 data]# hadoop fs -put ./01.data /wc/test


[root@redhat5 data]# hadoop fs -ls /wc/test
Found 1 items
-rw-r--r--   1 root supergroup         14 2016-01-22 21:57 /wc/test/01.data
[root@redhat5 data]# hadoop fs -text /wc/test/01.data
hello hadoop!
[root@redhat5 data]# 



-mv  重命名:


[root@redhat5 data]# hadoop fs -mv /wc/test/01.data /wc/test/01.data.bak
[root@redhat5 data]# hadoop fs -ls /wc/test
Found 1 items
-rw-r--r--   1 root supergroup         14 2016-01-22 21:57 /wc/test/01.data.bak
[root@redhat5 data]# 


-cp 拷贝命令:


[root@redhat5 data]# hadoop fs -cp /wc/test/01.data.bak /wc/test/01.data
[root@redhat5 data]# hadoop fs -ls /wc/test
Found 2 items
-rw-r--r--   1 root supergroup         14 2016-01-22 22:05 /wc/test/01.data
-rw-r--r--   1 root supergroup         14 2016-01-22 21:57 /wc/test/01.data.bak
[root@redhat5 data]# 


-rm -rmr命令:


[root@redhat5 data]# hadoop fs -ls /wc/test
Found 2 items
-rw-r--r--   1 root supergroup         14 2016-01-22 22:05 /wc/test/01.data
-rw-r--r--   1 root supergroup         14 2016-01-22 21:57 /wc/test/01.data.bak
[root@redhat5 data]# hadoop fs -rm /wc/test/01.data
Deleted hdfs://redhat5:9000/wc/test/01.data
[root@redhat5 data]# hadoop fs -ls /wc/test
Found 1 items
-rw-r--r--   1 root supergroup         14 2016-01-22 21:57 /wc/test/01.data.bak
[root@redhat5 data]# hadoop fs -rm /wc/test
rm: Cannot remove directory "hdfs://redhat5:9000/wc/test", use -rmr instead
[root@redhat5 data]# hadoop fs -rmr /wc/test
Deleted hdfs://redhat5:9000/wc/test



-get:

复制到当前文件下:


[root@redhat5 data]# hadoop fs -get /wc/input/core-site.xml ./
[root@redhat5 data]# ls
01.data  core-site.xml
[root@redhat5 data]# 


上传文件,其实是对HDFS文件写的过程:

[root@redhat5 data]# hadoop fs -mkdir /wc/test
[root@redhat5 data]# hadoop fs -ls /wc
Found 3 items
drwxr-xr-x   - root supergroup          0 2016-01-20 06:21 /wc/input
drwxr-xr-x   - root supergroup          0 2016-01-20 22:31 /wc/output
drwxr-xr-x   - root supergroup          0 2016-01-22 22:38 /wc/test
[root@redhat5 data]# hadoop fs -put ./01.data /wc/test


读取文件内容:

[root@redhat5 data]# hadoop fs -text /wc/test/01.data
hello hadoop!
[root@redhat5 data]# 



调用文件系统(FS)Shell命令应使用 bin/hadoop fs <args>的形式。 所有的的FS shell命令使用URI路径作为参数。URI格式是scheme://authority/path。对HDFS文件系统,scheme是hdfs,对本地文件系统,scheme是file。其中scheme和authority参数都是可选的,如果未加指定,就会使用配置中指定的默认scheme。一个HDFS文件或目录比如/parent/child可以表示成hdfs://namenode:namenodeport/parent/child,或者更简单的/parent/child(假设你配置文件中的默认值是namenode:namenodeport)。大多数FS Shell命令的行为和对应的Unix Shell命令类似,不同之处会在下面介绍各命令使用详情时指出。出错信息会输出到stderr,其他信息输出到stdout。



cat
使用方法:hadoop fs -cat URI [URI …]


将路径指定文件的内容输出到stdout。


示例:


hadoop fs -cat hdfs://host1:port1/file1 hdfs://host2:port2/file2
hadoop fs -cat file:///file3 /user/hadoop/file4
返回值:
成功返回0,失败返回-1。


chgrp
使用方法:hadoop fs -chgrp [-R] GROUP URI [URI …] Change group association of files. With -R, make the change recursively through the directory structure. The user must be the owner of files, or else a super-user. Additional information is in the Permissions User Guide. -->


改变文件所属的组。使用-R将使改变在目录结构下递归进行。命令的使用者必须是文件的所有者或者超级用户。更多的信息请参见HDFS权限用户指南。


chmod
使用方法:hadoop fs -chmod [-R] <MODE[,MODE]... | OCTALMODE> URI [URI …]


改变文件的权限。使用-R将使改变在目录结构下递归进行。命令的使用者必须是文件的所有者或者超级用户。更多的信息请参见HDFS权限用户指南。


chown
使用方法:hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]


改变文件的拥有者。使用-R将使改变在目录结构下递归进行。命令的使用者必须是超级用户。更多的信息请参见HDFS权限用户指南。


copyFromLocal
使用方法:hadoop fs -copyFromLocal <localsrc> URI


除了限定源路径是一个本地文件外,和put命令相似。


copyToLocal
使用方法:hadoop fs -copyToLocal [-ignorecrc] [-crc] URI <localdst>


除了限定目标路径是一个本地文件外,和get命令类似。


cp
使用方法:hadoop fs -cp URI [URI …] <dest>


将文件从源路径复制到目标路径。这个命令允许有多个源路径,此时目标路径必须是一个目录。 
示例:


hadoop fs -cp /user/hadoop/file1 /user/hadoop/file2
hadoop fs -cp /user/hadoop/file1 /user/hadoop/file2 /user/hadoop/dir
返回值:


成功返回0,失败返回-1。


du
使用方法:hadoop fs -du URI [URI …]


显示目录中所有文件的大小,或者当只指定一个文件时,显示此文件的大小。
示例:
hadoop fs -du /user/hadoop/dir1 /user/hadoop/file1 hdfs://host:port/user/hadoop/dir1 
返回值:
成功返回0,失败返回-1。


dus
使用方法:hadoop fs -dus <args>


显示文件的大小。


expunge
使用方法:hadoop fs -expunge


清空回收站。请参考HDFS设计文档以获取更多关于回收站特性的信息。


get
使用方法:hadoop fs -get [-ignorecrc] [-crc] <src> <localdst>


复制文件到本地文件系统。可用-ignorecrc选项复制CRC校验失败的文件。使用-crc选项复制文件以及CRC信息。


示例:


hadoop fs -get /user/hadoop/file localfile
hadoop fs -get hdfs://host:port/user/hadoop/file localfile
返回值:


成功返回0,失败返回-1。


getmerge
使用方法:hadoop fs -getmerge <src> <localdst> [addnl]


接受一个源目录和一个目标文件作为输入,并且将源目录中所有的文件连接成本地目标文件。addnl是可选的,用于指定在每个文件结尾添加一个换行符。


ls
使用方法:hadoop fs -ls <args>


如果是文件,则按照如下格式返回文件信息:
文件名 <副本数> 文件大小 修改日期 修改时间 权限 用户ID 组ID 
如果是目录,则返回它直接子文件的一个列表,就像在Unix中一样。目录返回列表的信息如下:
目录名 <dir> 修改日期 修改时间 权限 用户ID 组ID 
示例:
hadoop fs -ls /user/hadoop/file1 /user/hadoop/file2 hdfs://host:port/user/hadoop/dir1 /nonexistentfile 
返回值:
成功返回0,失败返回-1。


lsr
使用方法:hadoop fs -lsr <args> 
ls命令的递归版本。类似于Unix中的ls -R。


mkdir
使用方法:hadoop fs -mkdir <paths>


接受路径指定的uri作为参数,创建这些目录。其行为类似于Unix的mkdir -p,它会创建路径中的各级父目录。


示例:


hadoop fs -mkdir /user/hadoop/dir1 /user/hadoop/dir2
hadoop fs -mkdir hdfs://host1:port1/user/hadoop/dir hdfs://host2:port2/user/hadoop/dir
返回值:


成功返回0,失败返回-1。


movefromLocal
使用方法:dfs -moveFromLocal <src> <dst>


输出一个”not implemented“信息。


mv
使用方法:hadoop fs -mv URI [URI …] <dest>


将文件从源路径移动到目标路径。这个命令允许有多个源路径,此时目标路径必须是一个目录。不允许在不同的文件系统间移动文件。 
示例:


hadoop fs -mv /user/hadoop/file1 /user/hadoop/file2
hadoop fs -mv hdfs://host:port/file1 hdfs://host:port/file2 hdfs://host:port/file3 hdfs://host:port/dir1
返回值:


成功返回0,失败返回-1。


put
使用方法:hadoop fs -put <localsrc> ... <dst>


从本地文件系统中复制单个或多个源路径到目标文件系统。也支持从标准输入中读取输入写入目标文件系统。


hadoop fs -put localfile /user/hadoop/hadoopfile
hadoop fs -put localfile1 localfile2 /user/hadoop/hadoopdir
hadoop fs -put localfile hdfs://host:port/hadoop/hadoopfile
hadoop fs -put - hdfs://host:port/hadoop/hadoopfile 
从标准输入中读取输入。
返回值:


成功返回0,失败返回-1。


rm
使用方法:hadoop fs -rm URI [URI …]


删除指定的文件。只删除非空目录和文件。请参考rmr命令了解递归删除。
示例:


hadoop fs -rm hdfs://host:port/file /user/hadoop/emptydir
返回值:


成功返回0,失败返回-1。


rmr
使用方法:hadoop fs -rmr URI [URI …]


delete的递归版本。
示例:


hadoop fs -rmr /user/hadoop/dir
hadoop fs -rmr hdfs://host:port/user/hadoop/dir
返回值:


成功返回0,失败返回-1。


setrep
使用方法:hadoop fs -setrep [-R] <path>


改变一个文件的副本系数。-R选项用于递归改变目录下所有文件的副本系数。


示例:


hadoop fs -setrep -w 3 -R /user/hadoop/dir1
返回值:


成功返回0,失败返回-1。


stat
使用方法:hadoop fs -stat URI [URI …]


返回指定路径的统计信息。


示例:


hadoop fs -stat path
返回值:
成功返回0,失败返回-1。


tail
使用方法:hadoop fs -tail [-f] URI


将文件尾部1K字节的内容输出到stdout。支持-f选项,行为和Unix中一致。


示例:


hadoop fs -tail pathname
返回值:
成功返回0,失败返回-1。


test
使用方法:hadoop fs -test -[ezd] URI


选项:
-e 检查文件是否存在。如果存在则返回0。
-z 检查文件是否是0字节。如果是则返回0。 
-d 如果路径是个目录,则返回1,否则返回0。


示例:


hadoop fs -test -e filename
text
使用方法:hadoop fs -text <src>


将源文件输出为文本格式。允许的格式是zip和TextRecordInputStream。


touchz
使用方法:hadoop fs -touchz URI [URI …]


创建一个0字节的空文件。


示例:


hadoop -touchz pathname
返回值:
成功返回0,失败返回-1。
0 0
原创粉丝点击