HDFS command line interface lets you manage files in Hadoop's storage system easily using simple commands.
HDFS command line interface in Hadoop
hdfs dfs -command [options] [path]
# Example commands:
hdfs dfs -ls /path/to/directory
hdfs dfs -put localfile.txt /hdfs/path/
hdfs dfs -cat /hdfs/path/file.txt
hdfs dfs -rm /hdfs/path/file.txtAll commands start with hdfs dfs followed by the operation.
Paths starting with / refer to locations inside HDFS.
/user/hadoop directory in HDFS.hdfs dfs -ls /user/hadoop
localfile.txt from your computer to the HDFS directory /user/hadoop/.hdfs dfs -put localfile.txt /user/hadoop/
file.txt stored in HDFS on the screen.hdfs dfs -cat /user/hadoop/file.txt
file.txt from the HDFS directory /user/hadoop/.hdfs dfs -rm /user/hadoop/file.txt
This script shows how to list files, upload a file, check its content, and delete it using HDFS CLI.
# This is a shell script to demonstrate HDFS CLI commands # Step 1: List files in root directory (likely empty or default files) hdfs dfs -ls / # Step 2: Upload a local file to HDFS # Assume localfile.txt exists in current directory hdfs dfs -put localfile.txt /user/hadoop/ # Step 3: List files in /user/hadoop to confirm upload hdfs dfs -ls /user/hadoop # Step 4: Show content of uploaded file hdfs dfs -cat /user/hadoop/localfile.txt # Step 5: Remove the file from HDFS hdfs dfs -rm /user/hadoop/localfile.txt # Step 6: List files again to confirm removal hdfs dfs -ls /user/hadoop
Time complexity: Commands run quickly but depend on network and cluster size.
Space complexity: Commands themselves use little memory; storage depends on file size.
Common mistake: Forgetting to specify full HDFS path or mixing local and HDFS paths.
Use HDFS CLI for quick file management; use Hadoop APIs for complex data processing.
HDFS CLI helps manage files in Hadoop storage with simple commands.
Common commands include -ls, -put, -cat, and -rm.
Always use full HDFS paths and check file presence after operations.
Practice
hdfs dfs -ls /user/hadoop do?Solution
Step 1: Understand the command structure
The commandhdfs dfs -lsis used to list files and directories in HDFS.Step 2: Analyze the given path
The path/user/hadoopspecifies the directory to list.Final Answer:
Lists all files and directories in the HDFS path /user/hadoop -> Option CQuick Check:
hdfs dfs -lslists files [OK]
- Confusing '-ls' with '-rm' which deletes files
- Thinking '-ls' uploads files
- Assuming it shows file content
data.txt to HDFS directory /user/hadoop?Solution
Step 1: Identify the correct command for uploading
-putuploads local files to HDFS, and the syntax ishdfs dfs -put <local_path> <hdfs_path>.Step 2: Match the syntax with options
hdfs dfs -put data.txt /user/hadoop correctly placesdata.txtas local file and/user/hadoopas HDFS destination.Final Answer:
hdfs dfs -put data.txt /user/hadoop -> Option DQuick Check:
-put local_file hdfs_pathis correct [OK]
- Swapping local and HDFS paths
- Using '-get' which downloads files
- Using '-copyFromLocal' with wrong argument order
hdfs dfs -cat /user/hadoop/sample.txt if the file contains:Hello Hadoop Welcome to HDFS
Solution
Step 1: Understand the '-cat' command
hdfs dfs -catprints the content of a file in HDFS to the console.Step 2: Analyze the file content
The filesample.txtcontains two lines: 'Hello Hadoop' and 'Welcome to HDFS'.Final Answer:
Prints the content of sample.txt to the console -> Option BQuick Check:
-catshows file content [OK]
- Confusing '-cat' with '-ls' which lists files
- Thinking it deletes files
- Assuming it uploads files
hdfs dfs -rm /user/hadoop/data.csv but get an error saying file not found. What is the most likely cause?Solution
Step 1: Understand the error message
The error 'file not found' means the specified file does not exist at the given path.Step 2: Check command correctness
-rmis the correct command to delete files in HDFS, so the error is not due to wrong command.Final Answer:
The file /user/hadoop/data.csv does not exist in HDFS -> Option AQuick Check:
File missing causes 'file not found' error [OK]
- Assuming '-rm' is wrong command
- Trying to delete with '-put'
- Ignoring file path correctness
/local/data/ to HDFS directory /user/hadoop/data/ and then verify the files are uploaded. Which sequence of commands is correct?Solution
Step 1: Choose the correct upload command
-putuploads local files to HDFS. Using/local/data/*uploads all files from local directory.Step 2: Verify files in HDFS
hdfs dfs -ls /user/hadoop/data/lists files in the HDFS directory to confirm upload.Final Answer:
hdfs dfs -put /local/data/* /user/hadoop/data/ then hdfs dfs -ls /user/hadoop/data/ -> Option AQuick Check:
Upload with '-put' then list with '-ls' [OK]
- Using '-get' which downloads files
- Deleting local files before upload
- Using '-copyToLocal' which copies from HDFS to local
Ready to prove it?
Pass the test and this pattern moves unknown
