From 0850205088c8d0a23cd2e2390edf81f0a375725d Mon Sep 17 00:00:00 2001 From: Suresh Srinivas Date: Wed, 27 Jul 2011 19:09:55 +0000 Subject: [PATCH] HADOOP-7378. Add -d option to ls to not expand directories. Contributed by Daryn Sharp. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1151594 13f79535-47bb-0310-9956-ffa450edef68 --- common/CHANGES.txt | 3 ++ .../content/xdocs/file_system_shell.xml | 11 +++---- .../java/org/apache/hadoop/fs/shell/Ls.java | 29 ++++++++++++------- .../core/org/apache/hadoop/cli/testConf.xml | 12 +++++--- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/common/CHANGES.txt b/common/CHANGES.txt index 597f92494d..1e5318db7e 100644 --- a/common/CHANGES.txt +++ b/common/CHANGES.txt @@ -289,6 +289,9 @@ Trunk (unreleased changes) HADOOP-7485. Add -h option to ls to list file sizes in human readable format. (XieXianshan via suresh) + HADOOP-7378. Add -d option to ls to not expand directories. + (Daryn Sharp 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 59268a95cf..ef4f76e5c8 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 [-R] [-h] <args> + Usage: hdfs dfs -ls [-d] [-h] [-R] <args>

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

@@ -284,10 +284,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 a71809f30e..d6adcf8173 100644 --- a/common/src/java/org/apache/hadoop/fs/shell/Ls.java +++ b/common/src/java/org/apache/hadoop/fs/shell/Ls.java @@ -42,7 +42,7 @@ class Ls extends FsCommand { } public static final String NAME = "ls"; - public static final String USAGE = "[-R] [-h] [ ...]"; + public static final String USAGE = "[-d] [-h] [-R] [ ...]"; 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" + @@ -52,15 +52,17 @@ class Ls extends FsCommand { "\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.\n" + + " -d Directories are listed as plain files.\n" + " -h Formats the sizes of files in a human-readable fashion\n" + - " rather than of bytes.\n\n"; + " rather than a number of bytes.\n" + + " -R Recursively list the contents of directories."; protected static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); protected int maxRepl = 3, maxLen = 10, maxOwner = 0, maxGroup = 0; protected String lineFormat; + protected boolean dirRecurse; protected boolean humanReadable = false; protected String formatSize(long size) { @@ -72,22 +74,27 @@ class Ls extends FsCommand { @Override protected void processOptions(LinkedList args) throws IOException { - CommandFormat cf = new CommandFormat(0, Integer.MAX_VALUE, "R", "h"); + CommandFormat cf = new CommandFormat(0, Integer.MAX_VALUE, "d", "h", "R"); cf.parse(args); - setRecursive(cf.getOpt("R")); + dirRecurse = !cf.getOpt("d"); + setRecursive(cf.getOpt("R") && dirRecurse); humanReadable = cf.getOpt("h"); if (args.isEmpty()) args.add(Path.CUR_DIR); } + @Override + protected void processPathArgument(PathData item) throws IOException { + // implicitly recurse once for cmdline directories + if (dirRecurse && item.stat.isDirectory()) { + recursePath(item); + } else { + super.processPathArgument(item); + } + } + @Override protected void processPaths(PathData parent, PathData ... items) throws IOException { - // implicitly recurse once for cmdline directories - if (parent == null && items[0].stat.isDirectory()) { - recursePath(items[0]); - return; - } - if (!isRecursive() && items.length != 0) { out.println("Found " + items.length + " items"); } 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 d8cdc89979..b508c304c8 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\] \[-h\] \[<path> \.\.\.\]:( |\t)*List the contents that match the specified file pattern. If( )* + ^-ls \[-d\] \[-h\] \[-R\] \[<path> \.\.\.\]:( |\t)*List the contents that match the specified file pattern. If( )* RegexpComparator @@ -90,15 +90,19 @@ RegexpComparator - ^( |\t)*-R Recursively list the contents of directories.( )* + ^( |\t)*-d\s+Directories are listed as plain files\. RegexpComparator - ^( |\t)*-h Formats the sizes of files in a human-readable fashion( )* + ^( |\t)*-h\s+Formats the sizes of files in a human-readable fashion( )* RegexpComparator - ^( |\t)*rather than a number of bytes.( )* + ^( |\t)*rather than a number of bytes\.( )* + + + RegexpComparator + ^( |\t)*-R\s+Recursively list the contents of directories\.