The Computer Oracle

How to cut a file to a given size under Linux?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------


Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping

--

Chapters
00:00 How To Cut A File To A Given Size Under Linux?
00:37 Accepted Answer Score 87
00:56 Answer 2 Score 21
01:49 Answer 3 Score 1
02:15 Answer 4 Score 15
02:38 Thank you

--

Full question
https://superuser.com/questions/629521/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #ubuntu #shell #perl

#avk47



ACCEPTED ANSWER

Score 87


You may want to use the truncate command:

truncate --size=1G test.txt

SIZE can be specified as bytes, KB, K, MB, M, etc. I assume you can calculate the desired size by hand; if not, you could probably use the stat command to get information about the file's current size.




ANSWER 2

Score 21


perl -we 'open( FILE, "< ./test.txt" ) && truncate( FILE, 8 ) && close(FILE);'

opens the file for reading. However, to truncate the file you need to modify it, so a read-only file handle isn't going to work. You need to use the "modify" mode ("+>").

As a side issue, it always amazes me when people let system calls fail silently and then ask what went wrong. An essential part of diagnosing a problem is looking at the error message produced; even if you don't understand it, it makes life much easier for those you ask for help.

The following would have been somewhat more helpful:

perl -we 'open(FILE, "<", "./test.txt") or die "open: $!";
          truncate(FILE, 8) or die "truncate: $!";
          close(FILE);'

although admittedly that would only have reported "invalid argument". Still, that is useful information and might well have led you to the conclusion that the open mode was wrong (as it did for me).




ANSWER 3

Score 15


You can use tail to cut last 1000 bytes, example:

tail -c 1000 file > file2

the -c outputs final 1000 bytes of the file, for more options:

man tail

To replace original file with the file you just generated:

mv file2 file




ANSWER 4

Score 1


there is a completely different way to do this, with bash, using the ed program. the following script will retain only the last 5000 lines of all files sitting in the specified directory. this can easily be modified to loop over several directories, change the number of lines, etc.

#!/bin/bash

LOGDIR=/opt/log
SAVELINES=5000

dirs="$LOGDIR"
for dir in $dirs ; do
    files=${dir}/*
    for f in $files ; do
        echo -e "1,-${SAVELINES}d\nwq" | ed $f 1>/dev/null 2>&1
    done
done