diff --git a/common/CHANGES.txt b/common/CHANGES.txt index 44b80a7c2e..597f92494d 100644 --- a/common/CHANGES.txt +++ b/common/CHANGES.txt @@ -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 diff --git a/common/src/docs/src/documentation/content/xdocs/file_system_shell.xml b/common/src/docs/src/documentation/content/xdocs/file_system_shell.xml index eaf5e3cc5e..59268a95cf 100644 --- a/common/src/docs/src/documentation/content/xdocs/file_system_shell.xml +++ b/common/src/docs/src/documentation/content/xdocs/file_system_shell.xml @@ -273,7 +273,7 @@
ls

- Usage: hdfs dfs -ls <args> + Usage: hdfs dfs -ls [-R] [-h] <args>

For a file returns stat on the file with the following format:

@@ -283,6 +283,11 @@

permissions userid groupid modification_date modification_time dirname

+

Options:

+

Example:

hdfs dfs -ls /user/hadoop/file1 diff --git a/common/src/java/org/apache/hadoop/fs/shell/Ls.java b/common/src/java/org/apache/hadoop/fs/shell/Ls.java index 36c01a6329..a71809f30e 100644 --- a/common/src/java/org/apache/hadoop/fs/shell/Ls.java +++ b/common/src/java/org/apache/hadoop/fs/shell/Ls.java @@ -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] [ ...]"; + public static final String USAGE = "[-R] [-h] [ ...]"; public static final String DESCRIPTION = "List the contents that match the specified file pattern. If\n" + "path is not specified, the contents of /user/\n" + @@ -51,7 +52,9 @@ public static void registerCommands(CommandFactory factory) { "\tfileName(full path) 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 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(); } diff --git a/common/src/test/core/org/apache/hadoop/cli/testConf.xml b/common/src/test/core/org/apache/hadoop/cli/testConf.xml index e148fffcc1..d8cdc89979 100644 --- a/common/src/test/core/org/apache/hadoop/cli/testConf.xml +++ b/common/src/test/core/org/apache/hadoop/cli/testConf.xml @@ -54,7 +54,7 @@ RegexpComparator - ^-ls \[-R\] \[<path> \.\.\.\]:( |\t)*List the contents that match the specified file pattern. If( )* + ^-ls \[-R\] \[-h\] \[<path> \.\.\.\]:( |\t)*List the contents that match the specified file pattern. If( )* RegexpComparator @@ -88,6 +88,18 @@ RegexpComparator ^( |\t)*and size is the size of the file, in bytes.( )* + + RegexpComparator + ^( |\t)*-R Recursively list the contents of directories.( )* + + + RegexpComparator + ^( |\t)*-h Formats the sizes of files in a human-readable fashion( )* + + + RegexpComparator + ^( |\t)*rather than a number of bytes.( )* +