<story><title>Unix Commands I Abuse Every Day</title><url>http://everythingsysadmin.com/2012/09/unorthodoxunix.html</url></story><parent_chain><item><author>andrewvc</author><text>My #1 abuse is xargs -n1.&lt;p&gt;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 &apos;map&apos; in a functional language.&lt;p&gt;For instance, let&apos;s say you want to create thumbnails of a bunch of jpegs:&lt;p&gt;find images -name &quot;*.jpg&quot; | xargs -n1 -IF echo F F | sed -e &apos;s/.jpg$/_thumb.jpg/&apos; | xargs -n2 echo convert -geometry 200x&lt;p&gt;Additionally, it&apos;s fully parallelizable as xargs supports something akin to pmap.</text></item></parent_chain><comment><author>gnosis</author><text>You might be interested in &quot;zargs&quot; in zsh, which would save you the call to find.&lt;p&gt;Furthermore, instead of the pipe to sed and extra xargs, it might be clearer and simpler to do something like:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  zargs -n 1 **/*.jpg -- make-thumb
&lt;/code&gt;&lt;/pre&gt;
Where &quot;make-thumb&quot; is a short script (or even a zsh function, if you care about saving a fork for each input file) containing:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  convert -geometry 200x $1 ${1%%.jpg}_thumb.jpg
&lt;/code&gt;&lt;/pre&gt;
But, in real life, instead of writing such a script or function, what I&apos;d probably do instead is:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  zargs -n 1 **/*.jpg | vipe &amp;#62; myscript
&lt;/code&gt;&lt;/pre&gt;
and do some quick editing in vim to modify the zargs output by hand to do whatever I need -- and then I&apos;d run the resulting &quot;myscript&quot;.  Just fyi, &quot;vipe&quot; is part of the &quot;moreutils&quot; package [1] and lets you use your editor in the middle of a pipe.&lt;p&gt;One final trick is for when you need to do in-place image manipulation.  Instead of using &quot;convert&quot;, you can use another ImageMagick command: &quot;mogrify&quot;.  It will overwrite the original file with the modified file.  Of course, you should be very careful with it.&lt;p&gt;[1] - &lt;a href=&quot;http://joeyh.name/code/moreutils/&quot; rel=&quot;nofollow&quot;&gt;http://joeyh.name/code/moreutils/&lt;/a&gt;</text></comment>
<story><title>Unix Commands I Abuse Every Day</title><url>http://everythingsysadmin.com/2012/09/unorthodoxunix.html</url></story><parent_chain><item><author>andrewvc</author><text>My #1 abuse is xargs -n1.&lt;p&gt;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 &apos;map&apos; in a functional language.&lt;p&gt;For instance, let&apos;s say you want to create thumbnails of a bunch of jpegs:&lt;p&gt;find images -name &quot;*.jpg&quot; | xargs -n1 -IF echo F F | sed -e &apos;s/.jpg$/_thumb.jpg/&apos; | xargs -n2 echo convert -geometry 200x&lt;p&gt;Additionally, it&apos;s fully parallelizable as xargs supports something akin to pmap.</text></item></parent_chain><comment><author>sonnym</author><text>I use xargs a lot for refactoring work when I cannot simply use sed, e.g.&lt;p&gt;vim $(grep -lr foo | xargs)&lt;p&gt;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:&lt;p&gt;find . -name foo_fn exec sed -i s/foo_fn/bar_fn/g &apos;{}&apos; \;&lt;p&gt;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 either of these two options:&lt;p&gt;find -name foo_bar -exec cp &quot;&apos;{}&apos;&quot;{,.bak} \;&lt;p&gt;and&lt;p&gt;for file in $(find -name foo_bar); do cp &quot;$file&quot;{,.bak}; done&lt;p&gt;Ultimately, the great thing is, if you learn CTRL-R, you can always search for these types of commands and modify them as necessary for the particular task at hand and not necessarily remember them.  One I use all the time, to push git branches upstream is the following:&lt;p&gt;CTRL-R --set-&lt;p&gt;which gives me:&lt;p&gt;git push -f --set-upstream origin `git rev-parse --abbrev-ref HEAD`&lt;p&gt;This is entirely unique in my history, a common part of my workflow, and trivially searchable.&lt;p&gt;I also enjoy being able to perform something along the lines of:&lt;p&gt;vim $(bundle show foo-gem)</text></comment>