Remove bytes from one end of a file

Sometimes – rarely – you need to remove a few bytes from one end of a file. ere’s a quick way of doing it with the UNIX tool dd, so you don’t have to write your own program for it.

I started this post because I spent a lot of time looking for ways to do this. There were tons of ways of cutting bytes off the end of a file, but hardly any resources on slicing them off the beginning. In the end, I settled for the following solution:

dd if=$FILE_TO_TRIM skip=$BYTES_TO_SKIP bs=1 | dd of=$OUTPUT_FILE

The first command splits the file into blocks of 1 byte and skips the first specified number of bytes. This is then piped to dd again, which writes the result to an output file. Normally, using a block size of 1 byte means that you write very slowly, but since the block size is only used for the pipe, you end up writing the file in your file system’s native block size.

Cutting bytes off the end of a file is even easier – there’s even a tool for it. It works like this:


This either truncates the file down to the specified size or extends it using zero bytes. Be aware that this works in place, so you might want to do something like this:


My name’s Daniel Lee. I’m an enthusiast for open source and sharing. I grew up in the United States and did my doctorate in Germany. I've founded a company for planning solar power. I've worked on analog space suit interfaces, drones and a bunch of other things in my free time. I'm also involved in standards work for meteorological data. I worked for a while German Weather Service on improving forecasts for weather and renewable power production. I later led the team for data ingest there before I started my current job, engineering software and data formats at EUMETSAT.

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

From the archive