Take the "Head" of every file in a directory?

bash, linux, python-3.x, ubuntu

Solution

Try this using shell :

for i in *; do
    cp "$i" "$i.tail"
    sed -i '10001,$d' "$i.tail"
done

or simply :

for i in *; do
    sed '10001,$d' "$i" > "$i.tail"
done

or :

for i in *; do
    head -n 1000 "$i" > "$i.tail"
done

For python, see http://docs.python.org/2/library/subprocess.html if you would like to use the shell code.

Problem

I am working with large files, and my question here is two-fold. Bash - For testing purposes, I would like to iterate over every file in a given directory, taking the `Head` of each file (say `Head 10000`), and be left with a cut-down version of each. Either in the same directory or another it doesn't matter a whole lot, though I suppose the same would be preferred. Python3 - How can I do this programmatically? I imagine I need to use the os module?

Original source