Live data from Hacker News

Unix Commands I Abuse Every Day

everythingsysadmin.com

91–100 of 108 posts

Re: Unix Commands I Abuse Every Day

#91
post #90

Earlier quoted context omitted.

From the man page cat doc1 doc2 > doc.all concatenates the files doc1 and doc2 and writes the result to doc.all

I think I'm missing your point. doc[12] aren't changed.

Doc 1 & 2 are not changed but chained together to create doc.all

Re: Unix Commands I Abuse Every Day

#92
post #72
post #33

Earlier quoted context omitted.

I use xargs a lot for refactoring work when I cannot simply use sed, e.g. vim $(grep -lr foo | xargs) and doing what I need to do on a file by file basis. Otherwise, for renaming functions and the like, I do a lot of: find . -name foo_fn exec sed -i s/foo_fn/bar_fn/g '{}' \; I generally love abusing bash. Just today I was asked about how to rename a bunch of files, specifically containing spaces, and came up with eit…

Sorry, what do you think vim $(grep -lr foo | xargs) is doing? Assuming a missing directory after the `foo' why can't it just be vim $(grep -lr foo .) I don't see that xargs's default behaviour is adding anything. For find . -name foo_fn exec sed -i s/foo_fn/bar_fn/g '{}' \; you may find -exec's + of use. The above has the fork/execve overhead for each file found. find -name '*.[ch]' -exec sed -i 's/\ /bar/g' {} +

You are correct on both counts regarding the vim and grep example - I guess I just assumed I would have to have all the files on a single line before handing them off to vim.

Thanks for the suggestion about -exec +; I will have to remember it in the future.

Re: Unix Commands I Abuse Every Day

#93
post #2

My #1 abuse is xargs -n1. A lot of people like writing bash for loops, I will try and avoid that as much as possible, xargs -n1 is the bash equivalent of a call to 'map' in a functional language. For instance, let's say you want to create thumbnails of a bunch of jpegs: find images -name "*.jpg" | xargs -n1 -IF echo F F | sed -e 's/.jpg$/_thumb.jpg/' | xargs -n2 echo convert -geometry 200x Additionally, it's fully pa…

Why do that when a loop is clearer, safer, and shorter? find images -name '*.jpg' | while read jpg; do convert -geometry 200x "$jpg" "$(echo "$jpg" | sed 's/.jpg$/_thumb.jpg/')"; done This version works even when there are spaces in a filename, whereas yours will break.

Why use sed here?

    find images -name "*.jpg" | while read -r jpg; do
        convert -geometry 200x "$jpg" "${jpg%.jpg}_thumb.jpg"
    done
This correctly handles spaces in file names and uses built in shell string replacement.

Re: Unix Commands I Abuse Every Day

#94
post #92
post #72

Earlier quoted context omitted.

Sorry, what do you think vim $(grep -lr foo | xargs) is doing? Assuming a missing directory after the `foo' why can't it just be vim $(grep -lr foo .) I don't see that xargs's default behaviour is adding anything. For find . -name foo_fn exec sed -i s/foo_fn/bar_fn/g '{}' \; you may find -exec's + of use. The above has the fork/execve overhead for each file found. find -name '*.[ch]' -exec sed -i 's/\ /bar/g' {} +

You are correct on both counts regarding the vim and grep example - I guess I just assumed I would have to have all the files on a single line before handing them off to vim. Thanks for the suggestion about -exec +; I will have to remember it in the future.

Backticks, ``, and $(), use IFS to parse the command's output into words which then become the $()'s replacement and IFS normally contains \n.

Re: Unix Commands I Abuse Every Day

#95

I use the "grep with color for lines plus the empty string" so frequently that I have a function for it: function highlight() { local args=( "$@" ) for (( i=0; i This is only to be used as a filter, since it mangles filenames. I'm curious, does ack support a highlight-only mode?

Yes, ack --passthu does what you want. I have highlighting turned no in my .profile (export ACK_COLOR_MATCH="bold red").

Re: Unix Commands I Abuse Every Day

#96
post #2

My #1 abuse is xargs -n1. A lot of people like writing bash for loops, I will try and avoid that as much as possible, xargs -n1 is the bash equivalent of a call to 'map' in a functional language. For instance, let's say you want to create thumbnails of a bunch of jpegs: find images -name "*.jpg" | xargs -n1 -IF echo F F | sed -e 's/.jpg$/_thumb.jpg/' | xargs -n2 echo convert -geometry 200x Additionally, it's fully pa…

Why do that when a loop is clearer, safer, and shorter? find images -name '*.jpg' | while read jpg; do convert -geometry 200x "$jpg" "$(echo "$jpg" | sed 's/.jpg$/_thumb.jpg/')"; done This version works even when there are spaces in a filename, whereas yours will break.

You can parallelize this operation with xargs simply by adding a -P. You could add a & to your convert here but that would run all the jobs at the same time. xargs allows you to only run n at a time. That would be a lot harder to replicate in bash.

Re: Unix Commands I Abuse Every Day

#97
post #70

Earlier quoted context omitted.

Do you remap ESC to another key?

No, but for me acts like and the actual keybinding is M-. or M-_. The command you are looking for is yank-last-arg for bash and insert-last-word for zsh.

You have that backwards--Meta actually means "prepend Escape" (or 8th bit on, but that's another discussion).

Re: Unix Commands I Abuse Every Day

#98
For all the pipe lovers in this thread, here is a Perl utility I wrote to help debug shell pipelines. I call it `echoin`, and whatever it takes on stdin, it prints to stdout (presumably the terminal) while also treating its arguments as a command (sort of like xargs) and repeating its input for that command's stdin. So I can do:

    foo | echoin bar
This is like `foo | bar`, but I can see what's passing between them. It's a bit like `tee`, but reversed. It's what I irrationally want `foo | tee - | bar` to do.

    my $args = join ' ', @ARGV;

    open OUT, "|$args"  or die "Can't run $args: $!\n";
    while () {
      print $_;
      print OUT $_;
    }

Re: Unix Commands I Abuse Every Day

#99
post #82
post #67

Earlier quoted context omitted.

I've sometimes heard it's "catenate" not "concatenate". Do they mean the same thing? As for cat, the utility, I'm afraid we'll never stop seeing people doing cat file|prog1|prog2 even when it makes absolutely no sense whatsoever. If it did, I might as well do cat file|cat|prog|cat|prog2|cat - I mean, why not? It "looks nicer" than prog file|prog2 or prog Programmers love their cats.

It's most definitely catenate. I understand catenate to mean chain and concatenate to be to chain together. Since "cat foo bar xyzzy" doesn't modify the files to join them in any way I don't think they're chained together. Besides, ken & Co. aren't daft. con would be short for concatenate. :-)

According to the 1st Edition and 6th Edition manual pages: NAME cat -- concatenate and print

See: http://man.cat-v.org/unix-1st/1/cat http://man.cat-v.org/unix-6th/1/cat

I'm pretty sure based on the timeline of pipe (~1973, roughly V4) that the cat command (~1971 V1) predates pipes.

Re: Unix Commands I Abuse Every Day

#100
post #74

Earlier quoted context omitted.

find ./ -name '*.log' -print0 | xargs -0 rm Fixes that issue and xargs is far more efficient, it doesn't launch a new process for each line like exec does, but far more importantly, xargs is generalizable to all commands so you only have to learn it once; exec is just an ugly hack on find, you can't generalize it across all commands; xargs is much more unixy.

Wanting to remove the files was so common that some finds have it built-in. Avoids even the overhead of -exec rm {} +. find -name '*.log' -delete

True, but the issue isn't removing files, the issue is generalizing a command for mapping output of a command to another command. rm was just a simple example. xargs is far more useful than simply deleting files.
Post reply on HN