Search code examples
hdfshadoop2

How to exclude directories while using `copyToLocal`


I want to copy files from HDFS. I want folders to be excluded while copying files. I tried hdfs dfs -copyToLocal but it also copies directories as I tested.

Is there any way/command to copy files but not directories?


Solution

  • A variant of @daemon12's answer that achieves the same thing.

    hadoop fs -ls <HDFS_DIR_PATH> | grep "^-" | \
    awk '{print $8'} | hadoop fs -copyToLocal $(xargs) .
    
    • awk '{print $8'} is used to obtain the actual path column from ls output.
    • $(xargs) is used to concatenate lines of paths into space separated string.