Save only last x MB of audio stream downloaded with curl

Viewed 192

I want to download an audio stream (curl STREAM > out.mp3 ) and analyse it. However, I don't want to store everything, but only the last x MB of this stream, after this everything can be deleted. So the out.mp3 file's maximal size is x MB and it contains the currently last x MB of the stream.

Can I do this with bash standard tools and so don't analyse the file size myself and using if-else statements?

1 Answers

tail displays "the last part of a file". By default it displays the last 10 lines, you can use the -c number parameter to make it display the last number bytes. And as a good UNIX citizen, instead of working with files, you can pipe a stream of data to it.

Long story short, you can store the last 1 megabyte of a stream like this:

curl -s URL | tail -c 1048576 > last1mb.mp3
Related