Basically, our goal is to organize the world’s information and to make it universally accessible and useful.-Larry Page
So you already know what Hadoop is? Why it is used for ? and What problems you can solve with it? and you want to know how you can deal with files on HDFS ? Don’t worry, you are at the right place.
In this article I will present Top 10 basic Hadoop HDFS operations managed through shell commands which are useful to manage files on HDFS clusters, For testing purposes ,you can invoke this commands using either some of the VMs from Cloudera, Hortonworks etc or if you have your own setup of pseudo distributed cluster .
Let’s get started.
1. Create a directory in HDFS at given path(s).
hadoop fs -mkdir <paths>
hadoop fs -mkdir /user/saurzcode/dir1 /user/saurzcode/dir2
2. List the contents of a directory.
hadoop fs -ls <args>
hadoop fs -ls /user/saurzcode
3. Upload and download a file in HDFS.
hadoop fs -put:
Copy single src file, or multiple src files from local file system to the Hadoop data file system
hadoop fs -put <localsrc> ... <HDFS_dest_Path>
hadoop fs -put /home/saurzcode/Samplefile.txt /user/saurzcode/dir3/
hadoop fs -get:
Copies/Downloads files to the local file system
hadoop fs -get <hdfs_src> <localdst>
hadoop fs -get /user/saurzcode/dir3/Samplefile.txt /home/
4. See contents of a file
Same as unix cat command:
hadoop fs -cat <path[filename]>
hadoop fs -cat /user/saurzcode/dir1/abc.txt
5. Copy a file from source to destination
This command allows multiple sources as well in which case the destination must be a directory.
hadoop fs -cp <source> <dest>
hadoop fs -cp /user/saurzcode/dir1/abc.txt /user/saurzcode/dir2
6. Copy a file from/To Local file system to HDFS
hadoop fs -copyFromLocal <localsrc> URI
hadoop fs -copyFromLocal /home/saurzcode/abc.txt /user/saurzcode/abc.txt
Similar to put command, except that the source is restricted to a local file reference.
hadoop fs -copyToLocal [-ignorecrc] [-crc] URI <localdst>
Similar to get command, except that the destination is restricted to a local file reference.
7.Move file from source to destination.
Note:- Moving files across filesystem is not permitted.
hadoop fs -mv <src> <dest>
hadoop fs -mv /user/saurzcode/dir1/abc.txt /user/saurzcode/dir2
8. Remove a file or directory in HDFS.
Remove files specified as argument. Deletes directory only when it is empty
hadoop fs -rm <arg>
hadoop fs -rm /user/saurzcode/dir1/abc.txt
Recursive version of delete.
hadoop fs -rmr <arg>
hadoop fs -rmr /user/saurzcode/
9. Display last few lines of a file.
Similar to tail command in Unix.
hadoop fs -tail <path[filename]>
hadoop fs -tail /user/saurzcode/dir1/abc.txt
10.Display the aggregate length of a file.
hadoop fs -du <path>
hadoop fs -du /user/saurzcode/dir1/abc.txt
Please comment which of these commands you found most useful while dealing with Hadoop /HDFS.
Related articles :