How to create hundred thousand (100,000) files in a directory with extension of each .jpg, .c, .sh?

The size of each file will be 5kb and each extension will have 33,333 files.

  • yeah, i have searched quite a lot – Hamza Farrukh Mar 6 '16 at 3:09
  • Also your question is not very clear..you want 100000 copies of each extension? what about the varying sizes? – heemayl Mar 6 '16 at 3:10
  • i want a total 100,000 files, with extensions of .jpg, .c, .sh – Hamza Farrukh Mar 6 '16 at 3:11
  • 1
    What would be proportion then? also what about the sizes? – heemayl Mar 6 '16 at 3:12
  • all sizes between 1kb and 10 kb. proportion may be 33,333 – Hamza Farrukh Mar 6 '16 at 3:14

There are many ways to do this:

  • Using head with a simple for construct:

    for ext in jpg c sh; do head -c 5K /dev/zero >{1..33333}."$ext"; done

    Similarly tail -c 5K would work also.

  • Using dd:

    for ext in jpg c sh; do dd if=/dev/zero bs=1K count=5 >{1..33333}."$ext"; done
  • Using truncate (this would create sparse files):

    for ext in jpg c sh; do truncate -s 5K {1..33333}."$ext"; done

All of the above will create files with extensions .sh, .c and .jpg. Each file will be of 5KB and each extension will have 33,333 files.

  • +1 for truncate - another quick method would be fallocate. See askubuntu.com/questions/47963/… – Takkat Mar 6 '16 at 9:18
  • @Takkat Thought about fallocate but it does not take multiple filenames as argument so will be very very slow in this case..hence did not bother to mention it.. – heemayl Mar 6 '16 at 9:21

This would help :

mkdir my100000files
cd my100000files/
touch aa

Now write anything in aa to make it 1KB to 10KB

for FILE in `seq 00000 33333`; do cp aa $FILE.c; done

Do the same for .sh and jpg

Not the answer you're looking for? Browse other questions tagged or ask your own question.