2

I have several big files (ranging from 1GB to 1TB) and I want to remove the first and last character in each.

What's a fast way to do it (preferably with a simple bash script)? I don't need to save the old file.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88

2 Answers2

5

There is no fast way to do it in a shell.

head -c -1 < in.txt | tail -c +1 > out.txt

If you don't mind dropping to C, calling sendfile(2) with a *offset of 1 and a count of the size less 2 will likely be the fastest possible way.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

To quickly truncate the last character of a file you can use: truncate -s-1 file This will modify the file directly and avoid creating a copy of your large file. This asks truncate to reduce the size of the file by 1 . See original answer: https://stackoverflow.com/a/40568723

Thad Guidry
  • 579
  • 4
  • 8