1

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
  • 68,918
  • 10
  • 117
  • 136
Reut Sharabani
  • 29,003
  • 5
  • 68
  • 85

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
  • 740,318
  • 145
  • 1,296
  • 1,325
1

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
  • 509
  • 4
  • 7