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?

1 Answer 1

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,

2
  • 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, 2015 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, 2015 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.