Hadoop HDFS Commands With Examples
Hadoop HDFS Commands With Examples
1. Create a Directory
Command: hadoop fs -mkdir /richa
Description: Creates a new directory named richa in HDFS.
Execution: Run this command in the terminal.
2. List the Contents of a Directory
Command: hadoop fs -ls /
Description: Lists all files and directories in the root / of HDFS.
3. Create an Empty File
Command: hadoop fs -touchz /richa/demo.txt
Description: Creates an empty file demo.txt inside /richa directory.
4. Display the Created File
Command: hadoop fs -ls /richa
Description: Lists all files inside /richa directory.
5. Copy from Local to HDFS
Command: hadoop fs -put "C:\Users\91991\OneDrive\Desktop\sample.txt" /abc
Description: Uploads sample.txt from local system to /abc in HDFS.
6. Display File Contents
Command: hadoop fs -cat /abc/sample.txt
Description: Displays contents of sample.txt stored in HDFS.
7. Copy from HDFS to Local
Command: hadoop fs -get /abc/demo.txt "C:\Users\91991\OneDrive\Desktop\"
Description: Downloads demo.txt from HDFS to the local system.
8. Copy from One HDFS Directory to Another
Command: hadoop fs -cp /abc/sample.txt /richa/
Description: Copies sample.txt from /abc to /richa.
9. Move from One HDFS Directory to Another
Command: hadoop fs -mv /richa/demo1.txt /abc/
Description: Moves demo1.txt from /richa to /abc.
10. Check Disk Usage (Size of Each Directory)
Command: hadoop fs -du /
Description: Displays size of each directory.
11. Check Disk Usage (Size of Specific Directory)
Command: hadoop fs -du /abc
Description: Displays size of files under /abc.
12. Check Total Size of a Directory
Command: hadoop fs -dus /abc
Description: Displays total disk usage of /abc.
13. Check if Path is a Directory
Command: hadoop fs -test -d /abc && echo $?
Description: Returns 0 if /abc is a directory, else 1.
14. Check if Path Exists
Command: hadoop fs -test -e /abc && echo $?
Description: Returns 0 if /abc exists, else 1.
15. Move from Local to HDFS
Command: hadoop fs -moveFromLocal "C:\Users\91991\OneDrive\Desktop\testing.txt" /abc/
Description: Moves testing.txt from local to HDFS.
16. Merge Multiple HDFS Files into One
Command: hadoop fs -getmerge -nl /abc/ "C:/Users/91991/OneDrive/Desktop/mergeResult.txt"
Description: Merges files in /abc/ into a single file in local.
17. Append Local Files into One HDFS File
Command: hadoop fs -appendToFile "C:\Users\91991\OneDrive\Desktop\t1.txt" "C:\Users\91991\
OneDrive\Desktop\t2.txt" /richa/appendHDFS.txt
Description: Appends t1.txt and t2.txt into appendHDFS.txt in HDFS.
18. Check File Integrity Using Checksum
Command: hadoop fs -checksum /abc/sample.txt
Description: Checks whether sample.txt is modified or not.
19. Check HDFS Status
Command: hadoop fsck /
Description: Diagnoses the health of the Hadoop filesystem.
20. Count Directories and Files
Command: hadoop fs -count /richa
Description: Displays number of directories and files in /richa.
21. Delete a File in HDFS
Command: hadoop fs -rm /richa/test.txt
Description: Deletes test.txt from /richa.
22. Delete an Empty Directory
Command: hadoop fs -rmdir /sachin
Description: Deletes the empty directory /sachin.
23. Delete a Non-Empty Directory
Command: hadoop fs -rm -r /abc/Desktop
Description: Deletes the entire /abc/Desktop directory.
24. Change Group Ownership of a File
Command: hadoop fs -chgrp RichaNigam /richa/sample.txt
Description: Changes the group of sample.txt to RichaNigam.
25. Check File Statistics
Command: hadoop fs -stat %b /abc/sample.txt
Description: Displays file size in bytes.
26. Change File Permissions
Command: hadoop fs -chmod 777 /abc/head_and_tail.txt
Description: Grants full permissions to the file.
27. Change Replication Factor
Command: hadoop fs -setrep -w 3 /abc/sample.txt
Description: Sets replication factor of sample.txt to 3.
28. Reduce File Size Using Truncate
Command: hadoop fs -truncate -w 10 /abc/sample.txt
Description: Truncates sample.txt to 10 bytes.
29. Set Extended Attributes to a File
Command: hadoop fs -setfattr -n 'user.ap' -v 'this is dummy file' /abc/sample.txt
Description: Adds an extended attribute to sample.txt.
30. Retrieve Extended Attributes of a File
Command: hadoop fs -getfattr -d /abc/sample.txt
Description: Displays extended attributes of sample.txt.