linuxfileubuntuunix-head

How do I copy the beginning of multiple files in Linux?


I want to copy a bunch of files (*.txt) from one directory to another in Ubuntu. I want to reduce them in size, so I am using head to get the first 100 lines of each.

I want the new files to keep their original names but be in the subdirectory small/. I have tried:

head -n 100 *.txt > small/*.txt

but this creates one file called *.txt. I have also tried:

head -n 100 *.txt > small/

but this gives Is a directory error.

It's got to be easy right, but I am pretty bad at Linux. Any help is much appreciated.


Solution

  • Try

    for f in *.txt; do
      head -n 100 $f > small/$f
    done