hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sur...@apache.org
Subject svn commit: r1151505 - in /hadoop/common/trunk/common: CHANGES.txt src/docs/src/documentation/content/xdocs/file_system_shell.xml src/java/org/apache/hadoop/fs/shell/Ls.java src/test/core/org/apache/hadoop/cli/testConf.xml
Date Wed, 27 Jul 2011 15:35:59 GMT
Author: suresh
Date: Wed Jul 27 15:35:58 2011
New Revision: 1151505

URL: http://svn.apache.org/viewvc?rev=1151505&view=rev
Log:
HADOOP-7485. Add -h option to ls to list file sizes in human readable format. Contributed
by XieXianshan.


Modified:
    hadoop/common/trunk/common/CHANGES.txt
    hadoop/common/trunk/common/src/docs/src/documentation/content/xdocs/file_system_shell.xml
    hadoop/common/trunk/common/src/java/org/apache/hadoop/fs/shell/Ls.java
    hadoop/common/trunk/common/src/test/core/org/apache/hadoop/cli/testConf.xml

Modified: hadoop/common/trunk/common/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/common/CHANGES.txt?rev=1151505&r1=1151504&r2=1151505&view=diff
==============================================================================
--- hadoop/common/trunk/common/CHANGES.txt (original)
+++ hadoop/common/trunk/common/CHANGES.txt Wed Jul 27 15:35:58 2011
@@ -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

Modified: hadoop/common/trunk/common/src/docs/src/documentation/content/xdocs/file_system_shell.xml
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/common/src/docs/src/documentation/content/xdocs/file_system_shell.xml?rev=1151505&r1=1151504&r2=1151505&view=diff
==============================================================================
--- hadoop/common/trunk/common/src/docs/src/documentation/content/xdocs/file_system_shell.xml
(original)
+++ hadoop/common/trunk/common/src/docs/src/documentation/content/xdocs/file_system_shell.xml
Wed Jul 27 15:35:58 2011
@@ -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>

Modified: hadoop/common/trunk/common/src/java/org/apache/hadoop/fs/shell/Ls.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/common/src/java/org/apache/hadoop/fs/shell/Ls.java?rev=1151505&r1=1151504&r2=1151505&view=diff
==============================================================================
--- hadoop/common/trunk/common/src/java/org/apache/hadoop/fs/shell/Ls.java (original)
+++ hadoop/common/trunk/common/src/java/org/apache/hadoop/fs/shell/Ls.java Wed Jul 27 15:35:58
2011
@@ -22,6 +22,7 @@ import java.io.IOException;
 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 @@ class Ls extends FsCommand {
   }
   
   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 @@ class Ls extends FsCommand {
     "\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 @@ class Ls extends FsCommand {
   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 @@ class Ls extends FsCommand {
         (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 @@ class Ls extends FsCommand {
     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();
   }

Modified: hadoop/common/trunk/common/src/test/core/org/apache/hadoop/cli/testConf.xml
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/common/src/test/core/org/apache/hadoop/cli/testConf.xml?rev=1151505&r1=1151504&r2=1151505&view=diff
==============================================================================
--- hadoop/common/trunk/common/src/test/core/org/apache/hadoop/cli/testConf.xml (original)
+++ hadoop/common/trunk/common/src/test/core/org/apache/hadoop/cli/testConf.xml Wed Jul 27
15:35:58 2011
@@ -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>
 



Mime
View raw message