HDFS basic commands
ls: This command is used to list all the files. Use lsr for recursive approach. It is useful when we
want a hierarchy of a folder.
hdfs dfs –ls <path>
hdfs dfs –ls /
mkdir: To create a directory. In Hadoop dfs there is no home directory by default. So let’s first
create it.
hdfs dfs -mkdir <folder name>
hdfs dfs -mkdir /testDir
touchz: It creates an empty file.
hdfs dfs -touchz <file_path>
copyFromLocal (or) put: To copy files/folders from local file system to hdfs store. This is the
most important command. Local filesystem means the files present on the OS.
hdfs dfs -copyFromLocal sample.txt /testDir
cat: To print file contents
hdfs dfs -cat /testDir/sample.txt
copyToLocal (or) get: To copy files/folders from hdfs store to local file system
hdfs dfs -copyToLocal <<srcfile(on hdfs)> <local file dest>
hdfs dfs -copyToLocal /testDir/sample.txt copy_sample.txt
moveFromLocal: This command will move file from local to hdfs
hdfs dfs -moveFromLocal <local src> <dest(on hdfs)>
hdfs dfs -moveFromLocal copy_sample.txt /testDir/
cp: This command is used to copy files within hdfs. Lets copy folder geeks to geeks_copied.
hdfs dfs -cp <src(on hdfs)> <dest(on hdfs)>
hdfs dfs -cp /testDir/sample.txt /testDir/backup
Note: here it is assumed that folder ‘backup’ is present under /testDir
mv: This command is used to move files within hdfs.
hdfs dfs -mv <src(on hdfs)> <src(on hdfs)>
hdfs dfs -mv /testDir/sample.txt /testDir/backup/
rmr: This command deletes a file from HDFS recursively. It is very useful command when you
want to delete a non-empty directory.
hdfs dfs -rmr <filename/directoryName>
hdfs dfs -rmr /testDir/backup
Note: It will delete all the content inside the directory then the
directory itself.
du: It will give the size of each file in directory
hdfs dfs -du <dirName>
dus:: This command will give the total size of directory/file.
hdfs dfs -dus <dirName>
stat: It will give the last modified time of directory or path. In short it will give stats of the
directory or file.
hdfs dfs -stat <hdfs file>