News for hackers. Stuff that matters.
Re:What's the one Linux command you wish you knew years ago? by tikhonj on Sunday November 20, 2011 @02:20AM (#3257532)
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
Re:What's the one Linux command you wish you knew years ago? by sjs on Sunday November 20, 2011 @02:43AM (#3257557)
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? by jedbrown on Sunday November 20, 2011 @02:51AM (#3257568)
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? by X-Istence on Sunday November 20, 2011 @03:26AM (#3257619)
Note that the + may not be portable. I know it doesn't work on older Solaris versions (<10) and HP-UX (from a couple years ago when I last touched it).

Also, find may or may not split the files it finds into multiple invocations.

Re:What's the one Linux command you wish you knew years ago? by Karellen on Sunday November 20, 2011 @08:36AM (#3258060)
Too many invocations of "cat"

find . -name \*.java -print0 | xargs -0 cat | wc -l

Edit: Dammit. Others have already posted this exact version further below. :-(

Re:What's the one Linux command you wish you knew years ago? by tikhonj on Sunday November 20, 2011 @02:52AM (#3257571)
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? by thelastnode on Sunday November 20, 2011 @02:41AM (#3257554)
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 <filename> for every file that matches.

The -regex flag lets you do the same thing as -name with regular expressions.

The problem with this is we run n different wc's, as opposed to just one with all the parameters. We can use xargs to fix this: find . -name .java | xargs wc -l

This even gives you a total!

The really* simple solution (which is probably best) is to just use shell globbing: wc -l *.java

While find and xargs are definitely useful, the easy solution here works better

Re:What's the one Linux command you wish you knew years ago? by sirn on Sunday November 20, 2011 @02:46AM (#3257561)
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? by jedbrown on Sunday November 20, 2011 @02:53AM (#3257573)
With bash-4 or zsh

  wc -l **/*.java
Re:What's the one Linux command you wish you knew years ago? by reinhardt on Sunday November 20, 2011 @09:05AM (#3258112)
I was an avid find user until I discovered ack-grep. These days it's one of the first things I sudo apt-get install.
Re:What's the one Linux command you wish you knew years ago? by re_todd on Sunday November 20, 2011 @02:54AM (#3257574)
Agree with "find", especially finding files with recent changes, e.g. in last 5 minutes:

find . -mmin -5 -print

Re:What's the one Linux command you wish you knew years ago? by gnosis on Sunday November 20, 2011 @02:19PM (#3258772)
zsh's extended globbing support makes the use of find unnecessary in most cases. See the "extended globbing" section of the link below[1] for some examples.

zsh also has the 'zargs' command, which works something like xargs, except that it gets its arguments from the command line (often in conjunction with extended globbing) rather than from stdin.

You might also be interested in reading about the "useless uses of cat"[2]

[1] - http://linuxgazette.net/184/silva.html

[2] - http://partmaps.org/era/unix/award.html

Re:What's the one Linux command you wish you knew years ago? by sbov on Sunday November 20, 2011 @02:31AM (#3257541)
Never thought to use ``. I always used xargs:

find . -name *.java | xargs cat | wc -l

Re:What's the one Linux command you wish you knew years ago? by cperciva on Sunday November 20, 2011 @02:39AM (#3257551)
Your approach works better than backtick expansion, since it won't run into 'command line too long' issues; but it will still break on "weird" file names (e.g., file names with spaces in them). The correct way to do this is

    find . -name *.java -print0 | xargs -0 cat | wc -l
Re:What's the one Linux command you wish you knew years ago? by ars on Sunday November 20, 2011 @04:24AM (#3257706)
BTW, recent versions of linux don't have a fixed limit on command line length.

The command line can be a large as available memory (for a single processes). (Although the ratio between command line length and allocated memory is not obvious since it depends on the number of words in the command line.)

Re:What's the one Linux command you wish you knew years ago? by ralph on Sunday November 20, 2011 @05:12AM (#3257777)
That glob could cause trouble.

    find . -name '*.java' -print0 | xargs -0 cat | wc -l
Re:What's the one Linux command you wish you knew years ago? by pyre on Sunday November 20, 2011 @05:33AM (#3257810)
xargs can still run you into the 'too many args' issue. You'll want to use this:

       --max-args=max-args
       -n max-args
              Use  at  most max-args arguments per command
              line.  Fewer than max-args arguments will be used
              if the size (see the -s option) is exceeded,
              unless the -x option is given, in which case xargs
              will exit.
Re:What's the one Linux command you wish you knew years ago? by cperciva on Sunday November 20, 2011 @06:39AM (#3257892)
Only if there's a bug in your xargs. POSIX states:

The xargs utility shall limit the command line length such that when the command line is invoked, the combined argument and environment lists (see the exec family of functions in the System Interfaces volume of POSIX.1-2008) shall not exceed {ARG_MAX}-2048 bytes

so you should never run into the ARG_MAX limit.

Re:What's the one Linux command you wish you knew years ago? by dredmorbius on Sunday November 20, 2011 @03:31PM (#3258945)
Dollar-paren expansion beats backticks for being nestable.

E.g.: I like dumping temporary files with timestamps:

some-command-to-generate-log > /tmp/log-$( date +%Y%m%d-%H%M%S )

Now, if I want to wrap that in a larger loop -- say, iterating over a number of files or parameters:

command $( expansion one $( expansion 2 ))

Works

With backticks you'd have to do escapes:

command `expansion 1 \`expansion 2\` `

... which gets tedious.

Re:What's the one Linux command you wish you knew years ago? by jbarber on Monday November 21, 2011 @07:06AM (#3260828)
I like to use (for GNU date at least): $ date -Iseconds 2011-11-21T10:56:17+0000

because it requires less typing. The ISO 8601 standard format.

-Is, -Im, -Ih, and -Id allow you to change the displayed resolution.

Bizarrely, this doesn't seem to be documented in my version of date (coreutils 8.10).

Re:What's the one Linux command you wish you knew years ago? by dredmorbius on Monday November 21, 2011 @05:19PM (#3263061)
A lot of the date documentation is unfortunately in info only. Debian (and derived distros) do a good job of updating the manpages.

That said, I can't find "-I" documented anywhere.

What I like about the timestamp I use is that it's semantic but sorts lexically as well. Though yours does as well. Hrm.

Re:What's the one Linux command you wish you knew years ago? by sirn on Sunday November 20, 2011 @02:43AM (#3257556)
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? by asharp on Sunday November 20, 2011 @02:41AM (#3257553)
Also remember find's -print0 and xargs -0 so that you can deal with all filenames.
Re:What's the one Linux command you wish you knew years ago? by alextingle on Monday November 21, 2011 @08:23AM (#3260993)
In zsh:

    cat **/*.java | wc -l
Re:What's the one Linux command you wish you knew years ago? by chalst on Monday November 21, 2011 @08:37AM (#3261030)
Bash got this functionality in v4.

It's not as useful as find.