0
0
Hadoopdata~10 mins

HDFS command line interface in Hadoop - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - HDFS command line interface
Start HDFS CLI
Enter Command
Parse Command
Execute Command on HDFS
Return Output or Error
Prompt for Next Command or Exit
The HDFS CLI starts, waits for user commands, parses and executes them on HDFS, then shows results or errors, and repeats.
Execution Sample
Hadoop
hdfs dfs -ls /user/hadoop
hdfs dfs -mkdir /user/hadoop/newdir
hdfs dfs -put localfile.txt /user/hadoop/newdir
hdfs dfs -cat /user/hadoop/newdir/localfile.txt
These commands list files, create a directory, upload a file, and display file contents in HDFS.
Execution Table
StepCommandActionResultOutput
1hdfs dfs -ls /user/hadoopList directory contentsSuccessFound files: file1.txt, file2.txt
2hdfs dfs -mkdir /user/hadoop/newdirCreate directorySuccessDirectory /user/hadoop/newdir created
3hdfs dfs -put localfile.txt /user/hadoop/newdirUpload local file to HDFSSuccessFile localfile.txt uploaded
4hdfs dfs -cat /user/hadoop/newdir/localfile.txtDisplay file contentsSuccessHello Hadoop!
5hdfs dfs -rm /user/hadoop/newdir/localfile.txtRemove fileSuccessFile deleted
6hdfs dfs -ls /user/hadoop/newdirList directory contentsSuccessNo files found
7exitExit CLIEndSession terminated
💡 User typed 'exit' to end the HDFS CLI session
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
/user/hadoop directory contentsfile1.txt, file2.txtfile1.txt, file2.txtfile1.txt, file2.txt, newdir/file1.txt, file2.txt, newdir/file1.txt, file2.txt, newdir/file1.txt, file2.txt, newdir/file1.txt, file2.txt, newdir/file1.txt, file2.txt, newdir/
/user/hadoop/newdir contentsN/AN/Aemptylocalfile.txtlocalfile.txtemptyemptyempty
localfile.txt in HDFSN/AN/AN/Aexistsexistsdeleteddeleteddeleted
Key Moments - 3 Insights
Why does 'hdfs dfs -ls' show 'newdir/' after creating the directory?
Because after step 2, the directory 'newdir' is created inside /user/hadoop, so listing shows it as a folder with a trailing slash (see execution_table step 2).
What happens if you try to 'cat' a file that does not exist?
The command will return an error saying file not found. In our trace, step 4 succeeds because the file exists, but if missing, it would show an error instead.
Why does the directory become empty after deleting the file?
Because after step 5, the only file in 'newdir' is removed, so listing the directory in step 6 shows no files (see execution_table steps 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what files are listed in /user/hadoop after step 2?
Afile1.txt, file2.txt
Bnewdir/ only
Cfile1.txt, file2.txt, newdir/
DNo files
💡 Hint
Check the 'Output' column at step 2 in the execution_table.
At which step is the file 'localfile.txt' deleted from HDFS?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look for the 'Remove file' action in the execution_table.
If you run 'hdfs dfs -ls /user/hadoop/newdir' after step 5, what will you see?
ANo files found
BError: Directory not found
Clocalfile.txt
Dnewdir/
💡 Hint
Check the output of step 6 in the execution_table.
Concept Snapshot
HDFS CLI lets you interact with Hadoop files.
Use 'hdfs dfs -ls <path>' to list files.
Use 'hdfs dfs -mkdir <dir>' to create directories.
Use 'hdfs dfs -put <local> <hdfs>' to upload files.
Use 'hdfs dfs -cat <file>' to view contents.
Use 'hdfs dfs -rm <file>' to delete files.
Full Transcript
The HDFS command line interface (CLI) starts and waits for user commands. When you type a command like 'hdfs dfs -ls /user/hadoop', it lists files in that directory. Creating a directory with 'hdfs dfs -mkdir' adds a new folder visible in listings. Uploading files with 'hdfs dfs -put' copies local files to HDFS. Viewing file contents uses 'hdfs dfs -cat'. Deleting files removes them from HDFS. The CLI shows success or error messages after each command. You can exit the CLI by typing 'exit'. This trace showed commands listing files, creating directories, uploading, viewing, and deleting files step-by-step.