linuxshelldd

How can I generate random files with size 1mb each from 1-500 using linux shell script


How can I generate random files with size 1mb each from 1-500 using linux shell script.

I wanted to create 100 random files in 500 directory locations but I am not able to create random files. 1 file with 500 directory is working with below command in shell script:

for i in {001..500}; 
do 
  for j in {001..001}; 
   do dd if=/dev/urandom 
       of=/home/h2h/pickup/AGENT_CH_${j}/AG_${j}_File${i}.csv bs=1M count=1;
   done
done

My directory locations as in format :

/home/h2h/pickup/AGENT_CH_001/
/home/h2h/pickup/AGENT_CH_002/

It' working fine to create 1 file each with 500 directories. Now I am trying to generate 10 files for any random 100 directories.

Can someone please help on this?


Solution

  • If I understand correctly, you need to have 100 random files in each 500 directories.

    If so, try this:

    for d in {001..500}; do 
      for f in {001..100}; do 
        dir="/home/h2h/pickup/AGENT_CH_${d}"
        [ -d "$dir" ] || mkdir -p "$dir"
        dd if=/dev/urandom of="${dir}/AG_${d}_File${f}.csv" bs=1M count=1
      done
    done