2

I am trying to reverse the order of multiple text files (for plotting purposes) which are essentially rows of numbers. I tried to do it with tac and combined it with find and -exec as

find ./dir1/dir2/ -name foo.txt -type f -exec tac {} \;

but this only gives the output on the screen and does not modify the files intended.

Am I missing something here?

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
Thangam
  • 169
  • 3
  • 11

1 Answers1

2

You're almost there - tac writes to stdout so you can simply redirect the output somewhere handy:

find .... \; > newfoo.txt

If you want each file reversed and written to the same location, something like this will do:

find . -type f -exec sh -c 'tac "$1" > "$1"-new' -- {} \;

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • Actually it works but not the way I want it. This creates newfoo.txt in the current location of the terminal but not all the instances/locations where the files are found. – Thangam May 18 '15 at 12:03
  • It seems to work but using sed or vim is much faster. The solution will help who are non-users of vim and sed! – Thangam May 21 '15 at 05:16