Bash secret powers

This entry is part 2 of 2 in the series Unix swissknife

Bash scripting has evolved a lot in the last ten years.

I get used to relay on bash for all normal “data domination” tasks (like file system refactorings, database extraction and reporting and so on) and switching to python/ruby/perl when the complexity gets bigger. I didn’t imagine bash is even more powerful :) This article by Robert Muth so you new nice tricks: I  add here some other like mapfile and stress the most important one.

Update2016:Last but not least,if you do now know about bash history, this link will teach you a huge set of tricks

Continue reading “Bash secret powers”

better unix life

This entry is part 1 of 2 in the series Unix swissknife

File mass replace

Use the sed -i command

sed -i.BAK 's|foo|bar|g' files # -i does in-place replacement
perl -pi.bak -e 's|foo|bar|g' files                # or
perl -pi.bak -e 's|foo|bar|g' `find /pathname -name "filespec"`

 

Perl is the preferred way, but on some production system you must use the old good sed. For some tips on sed take a look to its faq.

 

Another very powerful command is xargs. Xargs is a rapid  way to process files containing spaces, using  a  combo with find:

find . -print0 -type f   | xargs -0 ls  | grep " "

Xargs is normally fastest then relaying on backtick substitution. For instance:

ls $(find . -type f)

is  slower then

find . -type f | xargs ls

because this second form create two process which works at the same time, using the unix pipe.