HDFS: How do you list files recursively?
Asked Answered
D

4

21

How do you, through Java, list all files (recursively) under a certain path in HDFS. I went through the API and noticed FileSystem.listFiles(Path,boolean) but it looks like that method doesn't exist in my instance of FileSystem when I initialize it.

Dunghill answered 8/6, 2012 at 0:51 Comment(0)
S
16

You can look at the source for org.apache.hadoop.fs.FsShell.ls(FileStatus, FileSystem, boolean, boolean) for your version of hadoop - this is what is called when you perform a hadoop fs -lsr path from the command line

Sclater answered 8/6, 2012 at 2:0 Comment(1)
"-lsr" is depricated, use: hadoop fs -ls -R PATHObit
A
14

Use -R followed by ls command to list files/directorires recursively.

hadoop fs -ls -R Path/Of/File

Possible attributes for ls command are

-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.

Abdicate answered 16/7, 2016 at 6:32 Comment(0)
R
0
hadoop-user@hadoop-desk ~/hadoop
$ bin/hadoop fs -lsr /user/someone_else/myfiles

-rw-r--r--   1 hadoop-user supergroup          0 2013-11-26 02:09 /user/someone_else/myfiles/file1.txt

-rw-r--r--   1 hadoop-user supergroup          0 2013-11-26 02:09 /user/someone_else/myfiles/file2.txt

drwxr-xr-x   - hadoop-user supergroup          0 2013-11-26 02:09 /user/someone_else/myfiles/subdir

-rw-r--r--   1 hadoop-user supergroup          0 2013-11-26 02:09 /user/someone_else/myfiles/subdir/anotherFile.txt
Recollection answered 27/11, 2013 at 21:27 Comment(0)
R
0

Using the hdfs cli with -R option

hdfs dfs -ls -R /path/to/
Reportage answered 28/11, 2022 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.