Saturday, 2 January 2016

Hadoop: ls: lists all the files in HDFS directory

If you pass directory as an argument to the ls command, it lists all files in that directory. If you pass file name as an argument to the ls command, it displays statistics on the file in below format.

permissions number_of_replicas userid groupid filesize modification_date modification_time filename



Usage:
hadoop fs [generic options] -ls [-d] [-h] [-R] [<path> ...]

Options
-d: Directories are listed as plain files.
-h: Format file sizes in a human-readable fashion (eg 64.0m instead of 67108864).
-R: Recursively list subdirectories encountered.

[cloudera@quickstart ~]$ hadoop fs -ls /
Found 6 items
drwxrwxrwx   - hdfs  supergroup          0 2017-10-23 09:15 /benchmarks
drwxr-xr-x   - hbase supergroup          0 2022-03-22 02:36 /hbase
drwxr-xr-x   - solr  solr                0 2017-10-23 09:18 /solr
drwxrwxrwt   - hdfs  supergroup          0 2022-03-22 02:20 /tmp
drwxr-xr-x   - hdfs  supergroup          0 2017-10-23 09:17 /user
drwxr-xr-x   - hdfs  supergroup          0 2017-10-23 09:17 /var
'ls' command list all the files in the dictionary order of their name. You can confirm the same from above output.

Previous                                                 Next                                                 Home

Hadoop: help: Get help about any hdfs command

“help” command is used to get help for any command.

Usage:
hadoop fs [generic options] -help [cmd ...]
 
Example
hadoop fs -help ls
above command print 'ls' command help to console.
 

 



Previous                                                 Next                                                 Home

Hadoop : get: copy files from HDFS to local system

“get” command is used to copy files from HDFS to local system.

Usgae:
hadoop fs [generic options] -get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>
 
Example
hadoop fs -get /user/cloudera/demo1/atlas.docx /user/cloudera/demo1/atlasClient.docx ./testData/
 
Above command copies atlas.docx, atlasClient.docx to local file system folder testData.
 
$ls -lart ./testData/
total 16
-rw-r--r-- 1 cloudera cloudera   35 Mar 23 03:02 b.txt
-rw-r--r-- 1 cloudera cloudera   55 Mar 23 03:02 c.txt
drwxrwxr-x 6 cloudera cloudera 4096 Mar 23 09:49 ..
drwxrwxr-x 2 cloudera cloudera 4096 Mar 23 10:08 .
$
$hadoop fs -ls /user/cloudera/demo1
Found 5 items
-rw-r--r--   1 cloudera cloudera          2 2022-03-22 23:58 /user/cloudera/demo1/a.txt
-rw-r--r--   1 cloudera cloudera    6166736 2022-03-22 23:57 /user/cloudera/demo1/atlas.docx
-rw-r--r--   1 cloudera cloudera    9147128 2022-03-22 23:57 /user/cloudera/demo1/atlasClient.docx
-rw-r--r--   1 cloudera cloudera    6988969 2022-03-22 23:58 /user/cloudera/demo1/gradle.docx
-rw-r--r--   1 cloudera cloudera    8986359 2022-03-22 23:58 /user/cloudera/demo1/maven.docx
$
$
$hadoop fs -get /user/cloudera/demo1/atlas.docx /user/cloudera/demo1/atlasClient.docx ./testData/
$
$ls -lart ./testData/
total 14976
-rw-r--r-- 1 cloudera cloudera      35 Mar 23 03:02 b.txt
-rw-r--r-- 1 cloudera cloudera      55 Mar 23 03:02 c.txt
drwxrwxr-x 6 cloudera cloudera    4096 Mar 23 09:49 ..
-rw-r--r-- 1 cloudera cloudera 6166736 Mar 23 10:09 atlas.docx
-rw-r--r-- 1 cloudera cloudera 9147128 Mar 23 10:09 atlasClient.docx
drwxrwxr-x 2 cloudera cloudera    4096 Mar 23 10:09 .
 





Previous                                                 Next                                                 Home

Hadoop: expunge: Empty the trash

From HDFS Architecture guide
When a file is deleted by a user or an application, it is not immediately removed from HDFS. Instead, HDFS first renames it to a file in the /trash directory. The file can be restored quickly as long as it remains in /trash. A file remains in /trash for a configurable amount of time. After the expiry of its life in /trash, the NameNode deletes the file from the HDFS namespace. The deletion of a file causes the blocks associated with the file to be freed. Note that there could be an appreciable time delay between the time a file is deleted by a user and the time of the corresponding increase in free space in HDFS.

A user can Undelete a file after deleting it as long as it remains in the /trash directory. If a user wants to undelete a file that he/she has deleted, he/she can navigate the /trash directory and retrieve the file. The /trash directory contains only the latest copy of the file that was deleted. The /trash directory is just like any other directory with one special feature: HDFS applies specified policies to automatically delete files from this directory. Current default trash interval is set to 0 (Deletes file without storing in trash). This value is configurable parameter stored as fs.trash.interval stored in core-site.xml.

Usage:
hadoop fs [generic options] -expunge




Previous                                                 Next                                                 Home

Hadoop: du: displays amount of space used by files

“du” command displays sizes of files and directories contained in the given directory.

Usage:
hadoop fs [generic options] -du [-s] [-h] <path> ...

The -s option will result in an aggregate summary of file lengths being displayed.

The -h option will format file sizes in a “human-readable” fashion.
 

$hadoop fs -du -h /user/cloudera
29.8 M  29.8 M  /user/cloudera/demo1
8.7 M   8.7 M   /user/cloudera/demo2
2.5 K   2.5 K   /user/cloudera/demo3
92      92      /user/cloudera/demo4
4.5 M   4.5 M   /user/cloudera/putDemo
$
$hadoop fs -du -h /user/cloudera/demo1
5.9 M  5.9 M  /user/cloudera/demo1/atlas.docx
8.7 M  8.7 M  /user/cloudera/demo1/atlasClient.docx
6.7 M  6.7 M  /user/cloudera/demo1/gradle.docx
8.6 M  8.6 M  /user/cloudera/demo1/maven.docx


Previous                                                 Next                                                 Home

Hadoop: df : Display free space

“df” command print the free space.

Usage:
hadoop fs [generic options] -df [-h] [<path> ...]

$hadoop fs -df -h
Filesystem                         Size     Used  Available  Use%
hdfs://quickstart.cloudera:8020  54.5 G  875.7 M     42.5 G    2%



Previous                                                 Next                                                 Home

Hadoop: cp: copy files from one HDFS locaiton to other HDFS location

“cp” command is used to copy multiple files to HDFS directory. By using cp you can’t copy files from local file system. cp is used when you wish to copy data from one HDFS location to another HDFS location. To copy data from local file system to HDFS you can use either put or copyFromLocal.

Usage:
hadoop fs [generic options] -cp [-f] [-p | -p[topax]] <src> ... <dst>
 
Example
hadoop fs -cp /user/cloudera/demo1/a.txt /user/cloudera/demo1/info.txt /user/cloudera/demo3
 
Above command copy the files a.txt, info.txt to the folder demo3.

$hadoop fs -ls /user/cloudera/demo1
Found 6 items
-rw-r--r--   1 cloudera cloudera          2 2022-03-22 23:58 /user/cloudera/demo1/a.txt
-rw-r--r--   1 cloudera cloudera    6166736 2022-03-22 23:57 /user/cloudera/demo1/atlas.docx
-rw-r--r--   1 cloudera cloudera    9147128 2022-03-22 23:57 /user/cloudera/demo1/atlasClient.docx
-rw-r--r--   1 cloudera cloudera    6988969 2022-03-22 23:58 /user/cloudera/demo1/gradle.docx
-rw-r--r--   1 cloudera cloudera       2539 2022-03-23 21:10 /user/cloudera/demo1/info.txt
-rw-r--r--   1 cloudera cloudera    8986359 2022-03-22 23:58 /user/cloudera/demo1/maven.docx
$
$hadoop fs -ls /user/cloudera/demo3
$
$hadoop fs -cp /user/cloudera/demo1/a.txt /user/cloudera/demo1/info.txt /user/cloudera/demo3
$
$hadoop fs -ls /user/cloudera/demo3
Found 2 items
-rw-r--r--   1 cloudera cloudera          2 2022-03-23 21:28 /user/cloudera/demo3/a.txt
-rw-r--r--   1 cloudera cloudera       2539 2022-03-23 21:28 /user/cloudera/demo3/info.txt
 
Previous                                                 Next                                                 Home