How to create multiple files(100 files) and they can contain 30 random characters in each of them using script

I need to create 100 files using script Linux which contain 30 random characters password and that password contain only strings, small letters and big letters. And the name of each file will be " Business.txt "

#!/bin/bash
for n in {1..100}; do
{ < /dev/urandom tr -dc A-Za-z0-9 | head -c${1:-30};echo; } > /mnt/mymnt/passwords/$n done
14

3 Answers

It’s not a good practice to open a file one hundred times when it’s actually needed just once, so how about this:

#!/bin/bash
random=$(</dev/urandom tr -dc A-Za-z0-9 | head -c3000)
for i in {1..100}; do echo ${random:(i-1)*30:30} >/mnt/mymnt/passwords/Business$i.txt
done

This first saves 3000 random characters matching A-Za-z0-9 in the variable $random and then loops over the numbers 1–100 cutting parts of 30 characters out and saving them.

When writing this answer, I thought you can get what you want the following way, but it is not yet clear how you want to store the passwords.

  1. Change directory to where you want to write the file Business.txt.

  2. Test that you can create the file manually,

    echo 'testing' > Business.txt
    cat Business.txt
  3. Modify ownership and permissions if necessary. The method depends on the file system.

  4. Create the passwords,

    for n in {1..100}; do { < /dev/urandom tr -dc A-Za-z0-9 | head -c${1:-30};echo; } ;done > Business.txt
  5. Check the passwords in Business.txt

    $ cat Business.txt
    7rmJFCq2CZ9azpuxywFLwbjhmL2dD4
    dtLSVAEtDmyLUglkYFgUeGc9PDKBPb
    E3bnJ8WF4qoyS1Tokp6reAcpIkuLUt
    Y5whhtbJn1KfAccp85547gNDji2xLY
    ...

    There should be 100 lines

    $ wc -l Business.txt
    100 Business.txt

Edit:

You want 100 files with different names (and one password in each file). So the steps can be modified according to this following list.

2.1. Remove the test file.

 rm Business.txt
  1. Modified command line: write to the target files inside the loop.

    for n in {1..100}; do { < /dev/urandom tr -dc A-Za-z0-9 | head -c${1:-30};echo; } > Business"$n".txt; done
  2. Check that the files were written

    $ ls
    Business100.txt Business28.txt Business46.txt Business64.txt Business82.txt
    Business10.txt Business29.txt Business47.txt Business65.txt Business83.txt
    Business11.txt Business2.txt Business48.txt Business66.txt Business84.txt
    Business12.txt Business30.txt Business49.txt Business67.txt Business85.txt
    Business13.txt Business31.txt Business4.txt Business68.txt Business86.txt
    Business14.txt Business32.txt Business50.txt Business69.txt Business87.txt
    Business15.txt Business33.txt Business51.txt Business6.txt Business88.txt
    Business16.txt Business34.txt Business52.txt Business70.txt Business89.txt
    Business17.txt Business35.txt Business53.txt Business71.txt Business8.txt
    Business18.txt Business36.txt Business54.txt Business72.txt Business90.txt
    Business19.txt Business37.txt Business55.txt Business73.txt Business91.txt
    Business1.txt Business38.txt Business56.txt Business74.txt Business92.txt
    Business20.txt Business39.txt Business57.txt Business75.txt Business93.txt
    Business21.txt Business3.txt Business58.txt Business76.txt Business94.txt
    Business22.txt Business40.txt Business59.txt Business77.txt Business95.txt
    Business23.txt Business41.txt Business5.txt Business78.txt Business96.txt
    Business24.txt Business42.txt Business60.txt Business79.txt Business97.txt
    Business25.txt Business43.txt Business61.txt Business7.txt Business98.txt
    Business26.txt Business44.txt Business62.txt Business80.txt Business99.txt
    Business27.txt Business45.txt Business63.txt Business81.txt Business9.txt

    There should be 100 files (with one password in each file)

    $ ls -1 Business*|wc -l
    100
  3. Check the passwords in Business1.txt, Business2.txt ...

    $ cat Business*
    43xx3zUEJ5wCPLzhagmQJcWHP2cvW1
    GCRZ8uJdxQEKXRBc2hoZREpiWseFll
    CKwYTghXjJOcBuufODKWnFohG1TKel
    ZMXPyNTxIENfoWlF7cfkPxCBkQpLt1
    ...

The only way I could readily find to get something from /dev/urandom for what you want is to use the dd command. Since /dev/urandom outputs raw bits, using your tr command will get printable characters, but if you want 30 printable characters, you need to get more than 30 bytes from dd. So, I went with 300. You can the use the cut command to get the first 30 printable characters.

Here is the script:

#!/bin/bash
for n in {1..100}; do p=$(dd if=/dev/urandom bs=1 count=300 2>/dev/null | tr -dc A-Za-z0-9 | cut -c 1-30) echo "${p}" >${n}
done
exit 0

Hope this helps.

6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like