0

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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
schoon
  • 2,858
  • 3
  • 46
  • 78

2 Answers2

3

You'll have to create a loop instead:

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

This loops through all the .txt files performing a head -n 100 in all of them and outputting into a new file in the small/ directory.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
1

Try

for f in *.txt; do
  head -n 100 $f > small/$f
done
Yam Marcovic
  • 7,953
  • 1
  • 28
  • 38