0

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.

heemayl
  • 91,753

2 Answers2

9

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.

heemayl
  • 91,753
  • +1 for truncate - another quick method would be fallocate. See http://askubuntu.com/questions/47963/how-can-i-quickly-make-a-large-file – Takkat Mar 06 '16 at 09: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 06 '16 at 09:21
1

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

Severus Tux
  • 9,866