HADOOP-7485. Add -h option to ls to list file sizes in human readable format. Contributed by XieXianshan.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1151505 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Suresh Srinivas 2011-07-27 15:35:58 +00:00
parent b60772c47d
commit 5dbd26f2c7
4 changed files with 38 additions and 7 deletions

View File

@ -286,6 +286,9 @@ Trunk (unreleased changes)
HADOOP-7298. Add test utility for writing multi-threaded tests. (todd and
Harsh J Chouraria via todd)
HADOOP-7485. Add -h option to ls to list file sizes in human readable
format. (XieXianshan via suresh)
OPTIMIZATIONS
HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole

View File

@ -273,7 +273,7 @@
<section>
<title>ls</title>
<p>
<code>Usage: hdfs dfs -ls &lt;args&gt;</code>
<code>Usage: hdfs dfs -ls [-R] [-h] &lt;args&gt;</code>
</p>
<p>For a file returns stat on the file with the following format:</p>
<p>
@ -283,6 +283,11 @@
<p>
<code>permissions userid groupid modification_date modification_time dirname</code>
</p>
<p>Options:</p>
<ul>
<li>The <code>-R</code> option will list subdirectories recursively.</li>
<li>The <code>-h</code> option will format file sizes in a &quot;human-readable&quot; fashion (e.g 64.0m instead of 67108864)</li>
</ul>
<p>Example:</p>
<p>
<code>hdfs dfs -ls /user/hadoop/file1 </code>

View File

@ -22,6 +22,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import org.apache.hadoop.util.StringUtils;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
@ -41,7 +42,7 @@ public static void registerCommands(CommandFactory factory) {
}
public static final String NAME = "ls";
public static final String USAGE = "[-R] [<path> ...]";
public static final String USAGE = "[-R] [-h] [<path> ...]";
public static final String DESCRIPTION =
"List the contents that match the specified file pattern. If\n" +
"path is not specified, the contents of /user/<currentUser>\n" +
@ -51,7 +52,9 @@ public static void registerCommands(CommandFactory factory) {
"\tfileName(full path) <r n> size \n" +
"where n is the number of replicas specified for the file \n" +
"and size is the size of the file, in bytes.\n" +
" -R Recursively list the contents of directories";
" -R Recursively list the contents of directories.\n" +
" -h Formats the sizes of files in a human-readable fashion\n" +
" rather than of bytes.\n\n";
protected static final SimpleDateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm");
@ -59,12 +62,20 @@ public static void registerCommands(CommandFactory factory) {
protected int maxRepl = 3, maxLen = 10, maxOwner = 0, maxGroup = 0;
protected String lineFormat;
protected boolean humanReadable = false;
protected String formatSize(long size) {
return humanReadable
? StringUtils.humanReadableInt(size)
: String.valueOf(size);
}
@Override
protected void processOptions(LinkedList<String> args)
throws IOException {
CommandFormat cf = new CommandFormat(0, Integer.MAX_VALUE, "R");
CommandFormat cf = new CommandFormat(0, Integer.MAX_VALUE, "R", "h");
cf.parse(args);
setRecursive(cf.getOpt("R"));
humanReadable = cf.getOpt("h");
if (args.isEmpty()) args.add(Path.CUR_DIR);
}
@ -93,7 +104,7 @@ protected void processPath(PathData item) throws IOException {
(stat.isFile() ? stat.getReplication() : "-"),
stat.getOwner(),
stat.getGroup(),
stat.getLen(),
formatSize(stat.getLen()),
dateFormat.format(new Date(stat.getModificationTime())),
item.path.toUri().getPath()
);
@ -118,7 +129,7 @@ private void adjustColumnWidths(PathData items[]) {
fmt.append("%" + maxRepl + "s ");
fmt.append("%-" + maxOwner + "s ");
fmt.append("%-" + maxGroup + "s ");
fmt.append("%" + maxLen + "d ");
fmt.append("%" + maxLen + "s ");
fmt.append("%s %s"); // mod time & path
lineFormat = fmt.toString();
}

View File

@ -54,7 +54,7 @@
<comparators>
<comparator>
<type>RegexpComparator</type>
<expected-output>^-ls \[-R\] \[&lt;path&gt; \.\.\.\]:( |\t)*List the contents that match the specified file pattern. If( )*</expected-output>
<expected-output>^-ls \[-R\] \[-h\] \[&lt;path&gt; \.\.\.\]:( |\t)*List the contents that match the specified file pattern. If( )*</expected-output>
</comparator>
<comparator>
<type>RegexpComparator</type>
@ -88,6 +88,18 @@
<type>RegexpComparator</type>
<expected-output>^( |\t)*and size is the size of the file, in bytes.( )*</expected-output>
</comparator>
<comparator>
<type>RegexpComparator</type>
<expected-output>^( |\t)*-R Recursively list the contents of directories.( )*</expected-output>
</comparator>
<comparator>
<type>RegexpComparator</type>
<expected-output>^( |\t)*-h Formats the sizes of files in a human-readable fashion( )*</expected-output>
</comparator>
<comparator>
<type>RegexpComparator</type>
<expected-output>^( |\t)*rather than a number of bytes.( )*</expected-output>
</comparator>
</comparators>
</test>