Monday, April 29, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 829  / 1 Year ago, thu, april 6, 2023, 10:01:46

Recently, in university, we started working with Linux and Bash. We have a task where we have to create 5 directories with the names Directory1, Directory2, and so on. In each directory we have to create 10 files with one random number inside.


I started like this, but I don't know what to do next:


#!/bin/bash 
for i in {1..5}
do
mkdir "Directory${i}"
done

Can you please help me?


More From » command-line

 Answers
6

Actually, I think that you should do your own homework, and your question includes some of the knowledge you need.



  • You understand loops.

  • You must do a loop inside your first loop.

  • You should investigate how to get a random number.


Despite this, here you have a oneliner that fits your requirements, if I haven't misunderstood you:


for i in {1..5}; do mkdir "Directory${i}"; for j in {1..10}; do echo $RANDOM > "Directory${i}/File${j}"; done; done

[#2300] Friday, April 7, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tigehanc

Total Points: 162
Total Questions: 113
Total Answers: 122

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
tigehanc questions
Thu, Dec 22, 22, 01:47, 1 Year ago
Sat, Aug 28, 21, 03:26, 3 Years ago
Fri, Feb 10, 23, 05:50, 1 Year ago
;