How to put files with spaces in names into HDFS?

I got this error when I tried to save a file with a space in its name into HDFS:

$ hdfs dfs -put -f "/home/u1/testa/test a" "/u1/testa/test a"
put: unexpected URISyntaxException

while the HDFS seems allow spaces in its file names: https://hadoop.apache.org/docs/r2.7.3/hadoop-project-dist/hadoop-common/filesystem/model.html .

How to achieve the effect of saving the files with spaces in its names into HDFS?

A tip to help you save a file with spaces in its name into HDFS: putting file from STDIN. One example is as follows:

date | hdfs dfs -put - "/path/file with space.txt"

For your example above, the command can be:

cat "/home/u1/testa/test a" | hdfs dfs -put -f - "/u1/testa/test a"

Then your file will be save into HDFS successfully with the space kept.

Note that if you would like to keep the mode of the file, you will need to set it explicitly using hdfs dsf -chmod ....

Leave a Reply

Your email address will not be published. Required fields are marked *