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
This commit is contained in:
Suresh Srinivas 2011-07-27 19:09:55 +00:00
parent c4f7d2f015
commit 0850205088
4 changed files with 35 additions and 20 deletions

View File

@ -289,6 +289,9 @@ Trunk (unreleased changes)
HADOOP-7485. Add -h option to ls to list file sizes in human readable HADOOP-7485. Add -h option to ls to list file sizes in human readable
format. (XieXianshan via suresh) format. (XieXianshan via suresh)
HADOOP-7378. Add -d option to ls to not expand directories.
(Daryn Sharp via suresh)
OPTIMIZATIONS OPTIMIZATIONS
HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole

View File

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

View File

@ -42,7 +42,7 @@ class Ls extends FsCommand {
} }
public static final String NAME = "ls"; public static final String NAME = "ls";
public static final String USAGE = "[-R] [-h] [<path> ...]"; public static final String USAGE = "[-d] [-h] [-R] [<path> ...]";
public static final String DESCRIPTION = public static final String DESCRIPTION =
"List the contents that match the specified file pattern. If\n" + "List the contents that match the specified file pattern. If\n" +
"path is not specified, the contents of /user/<currentUser>\n" + "path is not specified, the contents of /user/<currentUser>\n" +
@ -52,15 +52,17 @@ class Ls extends FsCommand {
"\tfileName(full path) <r n> size \n" + "\tfileName(full path) <r n> size \n" +
"where n is the number of replicas specified for the file \n" + "where n is the number of replicas specified for the file \n" +
"and size is the size of the file, in bytes.\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" + " -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 = protected static final SimpleDateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm"); new SimpleDateFormat("yyyy-MM-dd HH:mm");
protected int maxRepl = 3, maxLen = 10, maxOwner = 0, maxGroup = 0; protected int maxRepl = 3, maxLen = 10, maxOwner = 0, maxGroup = 0;
protected String lineFormat; protected String lineFormat;
protected boolean dirRecurse;
protected boolean humanReadable = false; protected boolean humanReadable = false;
protected String formatSize(long size) { protected String formatSize(long size) {
@ -72,22 +74,27 @@ class Ls extends FsCommand {
@Override @Override
protected void processOptions(LinkedList<String> args) protected void processOptions(LinkedList<String> args)
throws IOException { 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); cf.parse(args);
setRecursive(cf.getOpt("R")); dirRecurse = !cf.getOpt("d");
setRecursive(cf.getOpt("R") && dirRecurse);
humanReadable = cf.getOpt("h"); humanReadable = cf.getOpt("h");
if (args.isEmpty()) args.add(Path.CUR_DIR); 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 @Override
protected void processPaths(PathData parent, PathData ... items) protected void processPaths(PathData parent, PathData ... items)
throws IOException { throws IOException {
// implicitly recurse once for cmdline directories
if (parent == null && items[0].stat.isDirectory()) {
recursePath(items[0]);
return;
}
if (!isRecursive() && items.length != 0) { if (!isRecursive() && items.length != 0) {
out.println("Found " + items.length + " items"); out.println("Found " + items.length + " items");
} }

View File

@ -54,7 +54,7 @@
<comparators> <comparators>
<comparator> <comparator>
<type>RegexpComparator</type> <type>RegexpComparator</type>
<expected-output>^-ls \[-R\] \[-h\] \[&lt;path&gt; \.\.\.\]:( |\t)*List the contents that match the specified file pattern. If( )*</expected-output> <expected-output>^-ls \[-d\] \[-h\] \[-R\] \[&lt;path&gt; \.\.\.\]:( |\t)*List the contents that match the specified file pattern. If( )*</expected-output>
</comparator> </comparator>
<comparator> <comparator>
<type>RegexpComparator</type> <type>RegexpComparator</type>
@ -90,15 +90,19 @@
</comparator> </comparator>
<comparator> <comparator>
<type>RegexpComparator</type> <type>RegexpComparator</type>
<expected-output>^( |\t)*-R Recursively list the contents of directories.( )*</expected-output> <expected-output>^( |\t)*-d\s+Directories are listed as plain files\.</expected-output>
</comparator> </comparator>
<comparator> <comparator>
<type>RegexpComparator</type> <type>RegexpComparator</type>
<expected-output>^( |\t)*-h Formats the sizes of files in a human-readable fashion( )*</expected-output> <expected-output>^( |\t)*-h\s+Formats the sizes of files in a human-readable fashion( )*</expected-output>
</comparator> </comparator>
<comparator> <comparator>
<type>RegexpComparator</type> <type>RegexpComparator</type>
<expected-output>^( |\t)*rather than a number of bytes.( )*</expected-output> <expected-output>^( |\t)*rather than a number of bytes\.( )*</expected-output>
</comparator>
<comparator>
<type>RegexpComparator</type>
<expected-output>^( |\t)*-R\s+Recursively list the contents of directories\.</expected-output>
</comparator> </comparator>
</comparators> </comparators>
</test> </test>