File Operations
Similarly you can use the -ls
parameter to list the files in the specified directory:
/home/hadoop/hadoop/bin/hdfs dfs -ls /user/hadoop
The files listed here may vary depending on the experiment environment.
If you need to see the content of a file, you can use the cat
parameter. The easiest thing to think about is to specify a file path directly on HDFS. If you need to compare local directories with files on top of HDFS, you can specify their paths separately. However, it should be noted that the local directory needs to start with the file://
indicator, supplemented by the file path (such as /home/hadoop/.bashrc
, don't forget the /
in the front). Otherwise, any path specified here will be recognized by default as the path on HDFS:
/home/hadoop/hadoop/bin/hdfs dfs -cat file:///home/hadoop/.bashrc /user/hadoop/mapred-env.sh
The output is as follows:
hadoop:~$ hdfs dfs -cat file:///home/hadoop/.bashrc /user/hadoop/mapred-env.sh
## ~/.bashrc: executed by bash(1) for non-login shells.
## see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
## for examples
## If not running interactively, don't do anything
case $- in
...
If you need to copy a file to another path, you can use the cp
parameter:
/home/hadoop/hadoop/bin/hdfs dfs -cp /user/hadoop/mapred-env.sh /user/hadoop/copied_file.txt
Similarly, if you need to move a file, use the mv
parameter. This is basically the same as the Linux file system command format:
/home/hadoop/hadoop/bin/hdfs dfs -mv /user/hadoop/mapred-env.sh /moved_file.txt
Use the lsr
parameter to list the contents of the current directory, including the contents of subdirectories. The output is as follows:
hdfs dfs -lsr /
If you want to append some new content to a file on HDFS, you can use the appendToFile
parameter. And, when specifying the local file path to be appended, you can specify multiple of them. The last parameter will be the object to be appended. The file must exist on HDFS, otherwise an error will be reported:
echo 1 >> a.txt
echo 2 >> b.txt
/home/hadoop/hadoop/bin/hdfs dfs -appendToFile a.txt b.txt /user/hadoop/mapred-env.sh
You can use the tail
parameter to see the contents of the file tail (the end part of the file) to confirm whether the append was successful:
/home/hadoop/hadoop/bin/hdfs dfs -tail /user/hadoop/mapred-env.sh
View the output of the tail
command:
hadoop:~$ echo 1 >> a.txt
echo 2 >> b.txt
hdfs dfs -appendToFile a.txt b.txt /user/hadoop/mapred-env.sh
hadoop:~$ hdfs dfs -tail /user/hadoop/mapred-env.sh
1
2
If you need to delete a file or a directory, use the rm
parameter. This parameter can also be accompanied by -r
and -f
, which have the same meanings as they have for the Linux file system command rm
:
/home/hadoop/hadoop/bin/hdfs dfs -rm /moved_file.txt
The content of the file moved_file.txt
will be deleted, and the command will return the following output 'Deleted /moved_file.txt'