HDFS 常用 shell 命令
文件系统(FS)shell包括了各种shell类命令,这些命令直接与Hadoop分布式文件系统(HDFS)以及Hadoop支持的其他文件系统交互,比如本地文件系统、HFTP文件系统、S3文件系统等。FS shell被调用:
bin/hadoop fs <args>
所有的FS shell命令都将路径uri作为参数。URI格式是方案:绝对路径。对于HDFS,该方案是HDFS,对于本地文件系统,该方案是file。这个方案和绝对路径是可选的。如果未指定,则使用配置中指定的默认模式。一个HDFS文件或目录,如/父/子目录,可以指定为hdfs://namenodehost/parent/child或简单地作为/parent/child (假定您的配置被设置为指向HDFS://namdehost)。
在FS shell中,大多数命令的行为都类似于相应的Unix命令。每个命令都描述了差异。错误信息被发送到stderr,输出被发送到stdout。
如果使用HDFS,HDFS ,dfs就是一个同义词
appendToFile
hadoop fs -appendToFile <localsrc> ... <dst>
作用:将单个src或多个srcs从本地文件系统扩展到目标文件系统。还可以将读取标准输入做为目标文件系统。成功返回 0,失败返回 1。
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.
cat
hadoop fs -cat URI [URI ...]
作用:打印文件内容,成功返回 0 ,失败返回 -1。
hadoop fs -cat hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
hadoop fs -cat /user/hadoop/file3
checksum
hadoop fs -checksum URI
作用:返回一个文件的校验和信息
hadoop fs -checksum hdfs://nn1.example.com/file1
hadoop fs -checksum /user/hadoop/file2
ls
hadoop fs -ls [-d] [-h] [-R] <args>
-d 目录被列为普通文件
-h 以人类可读的方式格式化文件大小
-R 递归地列出子目录
hadoop fs -ls /user/hadoop/file1
mkdir
hadoop fs -mkdir [-p] <paths>
作用:将path uri作为参数,并创建目录。成功返回 1,失败返回 0。
-p 在路径上创建父目录。
hadoop fs -mkdir /user/hadoop/dir1
count
hadoop fs -count [-q] [-h] [-v] <paths>
作用:计算匹配指定文件模式的路径下的目录、文件和字节数,
带有-count的输出列是:目录数、文件数、内容大小、路径名。
有-count -q的输出列是:配额、余量、空间配额、剩余空间配额、目录数、文件数、内容大小、路径名。
-h 选项显示了人类可读格式的大小。
-v 选项显示一个标题行。
hadoop fs -count /file1 /file2
hadoop fs -count -q/file1
hadoop fs -count -q -h /file1
hadoop fs -count -q -h -v /file1
cp
hadoop fs -cp [-f] [-p | -p[topax]] URI [URI ...] <dest>
作用:将文件从源文件复制到目的地,此命令允许多个来源以及在这种情况下目标必须是一个目录。
如果已经存在,-f 选项将覆盖目标。
-p 选项将保留文件属性topx(时间戳、所有权、权限、ACL、XAttr)。如果-p没有arg指定,那么保留时间戳、所有权和权限。如果-pa是指定的,那么也要保留权限,因为ACL是一组超级权限。是否保留原始名称空间扩展属性的确定独立于-p标志。
hadoop fs -cp /user/hadoop/file1 /user/hadoop/file2
df
hadoop fs -df [-h] URI [URI ...]
作用:显示空闲空间。
-h 选项将以一种“人类可读”的方式格式化文件大小。
hadoop dfs -df /user/hadoop/dir1
du
hadoop fs -du [-s] [-h] URI [URI ...]
作用:显示给定目录中包含的文件和目录的大小或文件的长度。
-s 选项将导致显示文件长度的汇总,而不是单独的文件。
-h 选项将以“人类可读”的方式格式化文件大小。
hadoop fs -du /user/hadoop/dir1 /user/hadoop/file1
get
hadoop fs -get [-ignorecrc] [-crc] <src> <localdst>
作用:将文件复制到本地文件系统。
不能使用CRC检查的文件可以用忽略-crc选项复制。文件和CRCs可以使用-crc选项复制。
hadoop fs -get /user/hadoop/file localfile
copyToLocal
hadoop fs -copyToLocal [-ignorecrc] [-crc] URI <localdst>
类似于get命令
moveToLocal
hadoop fs -moveToLocal [-crc] <src> <dst>
尚未实现
put
hadoop fs -put <localsrc> ... <dst>
作用:将单个src或多个srcs从本地文件系统复制到目标文件系统。还从stdin读取输入并写入目标文件系统。
hadoop fs -put localfile /user/hadoop/hadoopfile
hadoop fs -put localfile1 localfile2 /user/hadoop/hadoopdir
hadoop fs -put localfile hdfs://nn.example.com/hadoop/hadoopfile
hadoop fs -put - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin.
copyFromLocal
hadoop fs -copyFromLocal <localsrc> URI
类似于put命令
moveFromLocal
hadoop fs -moveFromLocal <localsrc> <dst>
类似于put命令,除了在复制之后删除源。
mv
hadoop fs -mv URI [URI ...] <dest>
作用:将文件从源文件移动到目的地。不允许在文件系统中移动文件。
hadoop fs -mv /user/hadoop/file1 /user/hadoop/file2
rm
hadoop fs -rm [-f] [-r |-R] [-skipTrash] URI [URI ...]
作用:删除指定的文件
hadoop fs -rm hdfs://nn.example.com/file /user/hadoop/emptydir