Live data from Hacker News

What's the one Linux command you wish you knew years ago?

reddit.com

11–20 of 216 posts

Re: What's the one Linux command you wish you knew years ago?

#12
post #6
post #4

For me it was definitely find. I went through a ton of contortions to replicate its behavior several times before somebody knowledgeable clued me in. Also, `` and $(). Find combined with one of these is great. One of my favorite one-liners (I even figured it out myself :)) is: cat `find . --name *.java` | wc -l

Never thought to use ``. I always used xargs: find . -name *.java | xargs cat | wc -l

Without xargs:

    find . -iname '*.java' -exec cat {} + |wc -l
But if you do that, why not just:

    find . -iname '*.java' -exec wc -l {} +
Which will also print out output of wc -l for each files along with total lines.

Re: What's the one Linux command you wish you knew years ago?

#13
post #4

For me it was definitely find. I went through a ton of contortions to replicate its behavior several times before somebody knowledgeable clued me in. Also, `` and $(). Find combined with one of these is great. One of my favorite one-liners (I even figured it out myself :)) is: cat `find . --name *.java` | wc -l

You'll want to quote .java so the shell doesn't interpret it as a glob, and find doesn't use GNU style flags. You can also pass wc the filenames directly like this:

    wc -l $(find . -name '*.java')

Re: What's the one Linux command you wish you knew years ago?

#14
post #4

For me it was definitely find. I went through a ton of contortions to replicate its behavior several times before somebody knowledgeable clued me in. Also, `` and $(). Find combined with one of these is great. One of my favorite one-liners (I even figured it out myself :)) is: cat `find . --name *.java` | wc -l

For find, you need to use single quotes so your shell doesn't automatically expand the .java glob into the names of all the files. Also, find uses one dash for parameters, and can take an exec parameter: find . -name .java -exec wc -l {} \; The {} means "replace with filename" and the semicolon (escaped so your shell doesn't eat it). This will run wc -l for every file that matches. The -regex flag lets you do the sam…

You can skip xargs altogether with the plus sign instead of semicolon.

    -exec utility [argument ...] {} +
            Same as -exec, except that ``{}'' is replaced with as many path-
            names as possible for each invocation of utility.  This behaviour
            is similar to that of xargs(1).
The problem with just wc -l *.java is that it don't recursive directory.

Re: What's the one Linux command you wish you knew years ago?

#15
post #13
post #4

For me it was definitely find. I went through a ton of contortions to replicate its behavior several times before somebody knowledgeable clued me in. Also, `` and $(). Find combined with one of these is great. One of my favorite one-liners (I even figured it out myself :)) is: cat `find . --name *.java` | wc -l

You'll want to quote .java so the shell doesn't interpret it as a glob, and find doesn't use GNU style flags. You can also pass wc the filenames directly like this: wc -l $(find . -name '*.java')

That has different semantics (it prints one line per file) and can have line length limitations. This has the same semantics as the parent's version, but without line length issues.

  find . -name \*.java -exec cat {} + | wc -l

Re: What's the one Linux command you wish you knew years ago?

#17
post #13
post #4

For me it was definitely find. I went through a ton of contortions to replicate its behavior several times before somebody knowledgeable clued me in. Also, `` and $(). Find combined with one of these is great. One of my favorite one-liners (I even figured it out myself :)) is: cat `find . --name *.java` | wc -l

You'll want to quote .java so the shell doesn't interpret it as a glob, and find doesn't use GNU style flags. You can also pass wc the filenames directly like this: wc -l $(find . -name '*.java')

Yeah, the flags always get me. For some reason, I've never had issues with the *. It's good to know about passing files to wc directly.

I just use M-x find-dired and friends these days for most things, admittedly.

Re: What's the one Linux command you wish you knew years ago?

#18
post #14

Earlier quoted context omitted.

For find, you need to use single quotes so your shell doesn't automatically expand the .java glob into the names of all the files. Also, find uses one dash for parameters, and can take an exec parameter: find . -name .java -exec wc -l {} \; The {} means "replace with filename" and the semicolon (escaped so your shell doesn't eat it). This will run wc -l for every file that matches. The -regex flag lets you do the sam…

You can skip xargs altogether with the plus sign instead of semicolon. -exec utility [argument ...] {} + Same as -exec, except that ``{}'' is replaced with as many path- names as possible for each invocation of utility. This behaviour is similar to that of xargs(1). The problem with just wc -l *.java is that it don't recursive directory.

With bash-4 or zsh

  wc -l **/*.java

Re: What's the one Linux command you wish you knew years ago?

#19
post #4

For me it was definitely find. I went through a ton of contortions to replicate its behavior several times before somebody knowledgeable clued me in. Also, `` and $(). Find combined with one of these is great. One of my favorite one-liners (I even figured it out myself :)) is: cat `find . --name *.java` | wc -l

Agree with "find", especially finding files with recent changes, e.g. in last 5 minutes:

find . -mmin -5 -print

Post reply on HN