Quantcast
Channel: How to cut a file to a given size under Linux? - Super User
Viewing all articles
Browse latest Browse all 6

Answer by richard for How to cut a file to a given size under Linux?

$
0
0

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/bashLOGDIR=/opt/logSAVELINES=5000dirs="$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    donedone

Viewing all articles
Browse latest Browse all 6

Trending Articles