Wednesday, April 04, 2007

Common Bash Tasks

I don't use Bash for much scripting. Usually, if I have to open up a file to write a script, it will be done in Python. However, the less I have to do that for mundane tasks that can be accomplished on the command line with Bash, the better. This entry may not have much in it now, but every time I do something cool/useful/time-saving in Bash from now on, it'll go on here.

Common Tasks

  • Rename *.foo files to *.bar files: for i in *.foo; do mv $i ${i%.foo}.bar; done (BashFAQ)
  • Rename foo.* files to bar.* files: for i in foo.*; do mv $i bar.${i#foo.}; done (also check out the perl regex-based rename Linux utility)
  • Do something to multiple arguments: for i in arg1 arg2 arg3; do echo $i; done
  • Print 0 through 9 on separate lines: for i in {0..9}; do echo $i; done
  • Flatten output onto one line: <Multi-line output> | xargs (10 habits)
Links

No comments: