views:

69

answers:

1

I'm trying to use something along the lines of

unexpand -t 4 *.php

but am unsure how to write this command to do what I want.

Weirdly,

unexpand -t 4 file.php > file.php

gives me an empty file. (i.e. overwriting file.php with nothing)

I can specify multiple files okay, but don't know how to then overwrite each file.

I could use my IDE, but there are ~67000 instances of to be replaced over 200 files, and this will take a while.

I expect that the answers to my question(s) will be standard unix fare, but I'm still learning...

+5  A: 

You can very seldom use output redirection to replace the input. Replacing works with commands that support it internally (since they then do the basic steps themselves). From the shell level, it's far better to work in two steps, like so:

  1. Do the operation on foo, creating foo.tmp
  2. Move (rename) foo.tmp to foo, overwriting the original

This will be fast. It will require a bit more disk space, but if you do both steps before continuing to the next file, you will only need as much extra space as the largest single file, this should not be a problem.

Sketch script:

for a in *.php
do
  unexpand -t 4 $a >$a-notab
  mv $a-notab $a
done

You could do better (error-checking, and so on), but that is the basic outline.

unwind
ijw
Thanks to you both, very helpful.Is there an easy way to make the file match recursive?
jezmck