-1

With this way we can get all .jar files list:

#  su -l hdfs -c " hdfs dfs -ls /home/test/jt/*.jar "
-rw-r--r--   3 hdfs hdfs     501879 2019-03-04 10:35 /home/test/jt/cfjrfr-3.8.1.jar
-rw-r--r--   3 hdfs hdfs     870215 2019-03-04 10:35 /home/test/jt/dhe-1.2.1.jar
-rw-r--r--   3 hdfs hdfs    2734339 2019-03-04 10:35 /home/test/jt/34343-25.1-jre.jar
-rw-r--r--   3 hdfs hdfs      30053 2019-03-04 10:35 /home/test/jt/23424.jar
-rw-r--r--   3 hdfs hdfs      16481 2019-03-04 10:35 /home/test/jt/h324.jar
-rw-r--r--   3 hdfs hdfs      29725 2019-03-04 10:35 /home/test/jt/3223kj3.jar

We try also different approach with grep, but this syntax does not return any output:

 su -l hdfs -c " hdfs dfs -ls /home/test/jt" |  grep  "*.jar$"

Where I am wrong?

closed as unclear what you're asking by G-Man, jimmij, roaima, Mr Shunz, Haxiel Mar 7 at 18:37

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Well, for one thing you are confusing regex and glob patterns: if you want to grep for lines ending in .jar try grep "\.jar$" – steeldriver Mar 6 at 17:39
  • but what about all words before .jar , so why not set *\.jar$ – yael Mar 6 at 17:56
  • 1
    (a) first it's incorrect, you would need .*\.jar and (b) second it's superfluous because grep outputs whole lines when a match is found (unless you are using -o for example) – steeldriver Mar 6 at 18:03
  • 2
    Why are you using su? What is hdfs? Basically, why aren't you just running ls /home/test/jt/*.jar? – terdon Mar 6 at 18:32
3

* is excessful here. In regular expressions, * is used to specify that the previous symbol can appear any number of times, including 0. Using it without a preceding symbol is pointless, so in this special case grep looks for the * symbol itself (in general case you need to precede it with \ for this purpose).

. has a special meaning of matching any symbol too, so if you want to cover the situations where there are some bogus extensions like .djar, or extensionless files ending with jar, you need to precede it with \ too.

So, in short, you need just:

su -l hdfs -c " hdfs dfs -ls /home/test/jt" |  grep  "\.jar$"
  • but what about all words before .jar , so why not set *\.jar$ – yael Mar 6 at 17:51
  • 1
    If you don't care about them, why specify them in the expression? Also, in RE, * is meaningless without a character class before it; * means "zero or more of the previous match". – DopeGhoti Mar 6 at 18:03
  • ... at least with GNU grep, BRE treats a leading * a literal, whereas ERE treats it as zero or more occurrences of a leading empty string (which matches everything) – steeldriver Mar 6 at 18:19
  • @yael grep searches for a substring by default. It doesn't care about other symbols if there is enough consequent symbols to match the given pattern. If you want it to match the whole line, the expression should both start with ^ and end with $. In this case, if you want to make sure that file has an actual name besides of the extension, you can check it with [^/]\.jar$, which means that any symbol except of directory separator (/) can appear before the dot. [^ symbol ] is an inversion operator. – bodqhrohro Mar 6 at 19:19
0

With grep -E

su -l hdfs -c " hdfs dfs -ls /home/test/jt" |  grep  -E .jar$

It searches in the output of first command all files that end with .jar

  • No, that will find all lines that end in "any character" then jar. For example fajar. You want grep '\.jar$. But in any case, this is the same as what the OP already did. The -E doesn't make any difference at all here. – terdon Mar 6 at 18:34
-2

Try this one:

ls -l | grep *.jar*
  • Apart from the problems with parsing the output of ls, this will also find names like foojarbar. Why not just ls -l *.jar? – terdon Mar 6 at 18:31
  • @terdon "ls -l * .jar " will list all files with .doc at the at the end if you wanna list ALL files speicfically which have ".jar" you should use " ls -l * .jar * " this specfically looks for th phrase ".jar" instead of only jar. – Qasim Mar 6 at 18:34
  • I know the formatting here is complicated. Sorry! Please look at the formatting tools help page for help on formatting your posts. – terdon Mar 6 at 18:38
  • yea the format is pretty annoying – Qasim Mar 6 at 18:39
  • 1
    @G-Man Interesting how things are so much clearer after a good nights sleep. You're quite right, of course, ls *.jar will list files ending in .jar. My point about the regex was because the OP was using ls | grep .jar and that does something very different. I just copied the wrong thing into my comment. – terdon Mar 7 at 16:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.