Live data from Hacker News

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

reddit.com

1–10 of 216 posts

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

#3
100% agree with CTRL-R. It changed my life. Others that I love:

screen, pdsh,

perl -pi -e 's/find/replace/g' *.txt

redis-cli's new ability to have piped in data be the last argument of a command.

vim -o file1.txt file2.txt file3.txt (opening multiple files with vim)

Using vim instead of less as a pager: cat file.txt | vim -

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

#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

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

#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

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

#8
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

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?

#9
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

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?

#10
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 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

Post reply on HN