How to create a command sequence for this?


Recommended Posts

A. Write a command sequence, to create a directory structure like follows:

1. level 1 - 4 directories with random alphanumeric name of 8 characters each directory

2. level 2 - each level 1 directory will have 3 directories - yes, no and yesorno

3. level 3 - each level 3 directory will have 9 directories with directory named in series 1-9 e.g. [1, 2, 3...9]

4. level 4 - each level 4 directory will have 3 directories - yes, no and yesorno

5. all directories at each level will have a text file named abc.txt with random alpha numeric text of 200 characters

Can someone shed a light in a right direction that will help me to achieve this?

Thanks !!

I have a background in C#/VB.NET, so that will influence my answer:

You'll need a few nested For-Next loops to create the exact number of directories you need. You'll also need a pseudo-random number generator to generate the random alphanumeric characters, and to make the "yes, no, or yesorno" directories. The 1-9 directories are easy.

If you're using .NET, use a TextWriter to write the text files.

Have fun!

It could be done in pretty much any language, scripting or otherwise, but assuming you want it in bash:

#!/bin/bash

function random_chars ()
{
	tmp=$( < /dev/urandom tr -dc A-Za-z1-9 | head -c $1 )
	eval $2=$tmp
}

function create_abc ()
{
	local rand=''
	local file=$1'/abc.txt'

	random_chars 200 rand
	echo $rand > $file
	echo $file
}

function create_1to9 ()
{
	for i in 1 2 3 4 5 6 7 8 9
	do

		wd=$1'/'$i
		mkdir $wd
		echo $wd

		create_abc $wd
		create_yesno $wd 0

	done
}

function create_answer ()
{
	local answer=$1'/'$2

	mkdir $answer
	echo $answer

	create_abc $answer  

	if [ $3 == 1 ]
	then
		create_1to9 $answer
	fi
}

function create_yesno ()
{
	create_answer $1 'yes' $2
	create_answer $1 'no'  $2
	create_answer $1 'yesorno' $2
}

function create_dirs ()
{
	local ret=''

	for i in 1 2 3 4
	do
		random_chars 8 ret

		dir=$1'/'$ret
		mkdir $dir
		echo $dir

		create_abc $dir
		create_yesno $dir 1	  

	done

}

function main ()
{
	create_dirs $1
}

main $1

Copy/echo it into a file, chmod +x it.

Then to run:

./script targetdir

targetdir is where you want it to create the directory structure. I just used '.' ( current directory ) for testing.

My bash script knowledge is pretty awful, so I'm sure someone else could do better.

I have a background in C#/VB.NET, so that will influence my answer:

You'll need a few nested For-Next loops to create the exact number of directories you need. You'll also need a pseudo-random number generator to generate the random alphanumeric characters, and to make the "yes, no, or yesorno" directories. The 1-9 directories are easy.

If you're using .NET, use a TextWriter to write the text files.

Have fun!

That's about as efficient as using ice cream for a fire guard ;)

There was a slight bug in random_chars () that caused it to output the abc.txt file incorrectly. Change it to:

Edit: Actually, the tmp variable is superfluous. Just remove it entirely.

function random_chars ()
{
	eval $2=$( < /dev/urandom tr -dc A-Za-z1-9 | head -c $1 )
}

Thanks for your responses.. . .

I came up with following command sequence :::::::::::::


bash$ for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`; done

bash$ for i in `ls`; do `mkdir -p $i/{yes,no,yesorno}/{1..9} `; done

bash$ find . -type d -exec touch {}/abc.txt \;
[/CODE]

Now I am stuck at creating "yes,no,yesorno" directory in directories with name 1 - 9

This is what I tried ::

bash$ for i in $(find . -type d -name [1-9]); do `mkdir -p /{yes,no,yesorno}`;done

but its prompting with error "permission denied"

What am I missing?

Thanks for your responses.. . .

I came up with following command sequence :::::::::::::


bash$ for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`; done

bash$ for i in `ls`; do `mkdir -p $i/{yes,no,yesorno}/{1..9} `; done

bash$ find . -type d -exec touch {}/abc.txt \;
[/CODE]

Now I am stuck at creating "yes,no,yesorno" directory in directories with name 1 - 9

This is what I tried ::

bash$ for i in $(find . -type d -name [1-9]); do `mkdir -p /{yes,no,yesorno}`;done

but its prompting with error "permission denied"

What am I missing?

What's happening is you are trying to create the folders /yes, /no, and /yesorno, which are in the [i]root directory[/i] of your filesystem. What you want is to modify that line to be

[CODE]
for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno}`; done
[/CODE]

Basically, you're missing that $i which would make the {yes,no,yesorno} a subdirectory of your random folders as opposed to a subdirectory of the root directory.

  • Like 2

What's happening is you are trying to create the folders /yes, /no, and /yesorno, which are in the root directory of your filesystem. What you want is to modify that line to be


for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno}`; done
[/CODE]

Basically, you're missing that $i which would make the {yes,no,yesorno} a subdirectory of your random folders as opposed to a subdirectory of the root directory.

Thats what I needed. .. . .Thanks.. .

But now there are some other complications I am facing,,, :(

I need to redirect the output to [b]output.txt[/b] and error to [b]error.txt[/b]

[b]I came up with this:[/b]

[CODE]
for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`<&- 2>$HOME/error.txt; done

for i in `ls` ; do `mkdir -p $i/{yes,no,yesorno}/{1..9}` <&- 1>$HOME/output.txt 2>>$HOME/error.txt; done

for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno} <&- 1>$HOME/output.txt 2>>$HOME/error.txt `;done

for i in $(find . -type d); do `< /dev/urandom tr -dc A-Z0-9 | head -c200 > $i/abc.txt <&- 2>>$HOME/error.txt `; done
[/CODE]

After that, I need to join all the command sequences into one single command.

When I tried joining the commands and placing [b]done [/b]at the end, it seems to be something is missing.. please help

'done' indicates the end of a loop, not the end of a command sequence. 'joining' the command should be as simple as placing them all on one line with semicolons between them. Based on the four commands you quoted in your last post, your final command should look something like this:


for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`<&- 2>$HOME/error.txt; done; for i in `ls` ; do `mkdir -p $i/{yes,no,yesorno}/{1..9}` <&- 1>$HOME/output.txt 2>>$HOME/error.txt; done; for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno} <&- 1>$HOME/output.txt 2>>$HOME/error.txt `;done; for i in $(find . -type d); do `< /dev/urandom tr -dc A-Z0-9 | head -c200 > $i/abc.txt <&- 2>>$HOME/error.txt `; done
[/CODE]

  • Like 2

'done' indicates the end of a loop, not the end of a command sequence. 'joining' the command should be as simple as placing them all on one line with semicolons between them. Based on the four commands you quoted in your last post, your final command should look something like this:


for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`<&- 2>$HOME/error.txt; done; for i in `ls` ; do `mkdir -p $i/{yes,no,yesorno}/{1..9}` <&- 1>$HOME/output.txt 2>>$HOME/error.txt; done; for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno} <&- 1>$HOME/output.txt 2>>$HOME/error.txt `;done; for i in $(find . -type d); do `< /dev/urandom tr -dc A-Z0-9 | head -c200 > $i/abc.txt <&- 2>>$HOME/error.txt `; done
[/CODE]

Thank you. . .. Finally. .. :)

[CODE]
for i in $(seq 4); do mkdir `< /dev/urandom tr -dc A-Z0-9 | head -c8`<&- 2>$HOME/error.txt; done;for i in `ls`; do `mkdir -p $i/{yes,no,yesorno}/{1..9}` <&- 1>$HOME/output.txt 2>>$HOME/error.txt; done;for i in $(find . -type d -name [1-9]); do `mkdir -p $i/{yes,no,yesorno}` <&- 1>$HOME/output.txt 2>>$HOME/error.txt; done;for i in $(find . -type d); do `< /dev/urandom tr -dc A-Z0-9 | head -c200 > $i/abc.txt` <&- 2>>$HOME/error.txt; done
[/CODE]

I tried to edit the above post but can't see the edit button, ,, :(

btw, can we redirect the output of mkdir command to a file? i.e output.txt . . .coz its always empty.

And when I execute the command twice, I get an error "Unable to create a directory abc.txt"

How can I make this command to be executed "n" number of times?. . .I don't want to loop it . .. I need to execute the same command on the previous output it generated.

Now I can see the edit button. .

I think your problem is that mkdir doesn't actually output anything to screen unless it encounters an error; so redirecting its output to a file is virtually useless. If you really want it to print a message, try passing it the -v switch. As for the second part of your question about executing the command 'n' times, I have no idea what you are talking about. My best guess is that you want to create a directory inside of a directory that does not yet exist. To create any intermediate directories you could pass mkdir the -p switch. Putting all of the above together your command may look something like the following:


mkdir -pv 'rar' 1>output.txt 2>&1
[/CODE]

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Dragon's Dogma 2: Dark Arisen expansion to bring snowy region, new updates also coming by Pulasthi Ariyasinghe Capcom had a surprise waiting for Dragon's Dogma fans today in the Nintendo Direct presentation. The company revealed an expansion for the second installment with a name that should be familiar to series veterans. Coming later this year, Dragon's Dogma 2: Dark Arisen is promising a massive new region to explore, new monsters, fresh skills to learn, and more. The studio says players will be heading to the Northern region of the world, named Norgan, to find new secrets about an undying "Fallen Dragon." There will be forgotten relics that the protagonist can find to unlock fresh weapons and skills the expansion is introducing. Players will also be able to find mysterious equipment from a previous Arisen as a part of the expansion, all part of 12 Lost Rites Dungeon Challenges they must complete to gain access. In Neowin's own review, I found Dragon's Dogma 2 to be an impressive RPG when it launched back in 2024, giving the title an 8.5/10 for its class variants, companion system, and immersive exploration. "Once a prosperous region of the kingdom of Vermund, it was abandoned many years ago for reasons unknown," says Capcom about the new region. "Long has it been since any soul traveled its paths. Blanketed in heavy snow, these frigid lands are home to savage hordes and creatures of unbelievable power. Those who are capable of vanquishing such fearsome foes, or those who possess a keen eye for exploration, will find themselves rewarded with powerful relics." Dragon’s Dogma 2: Dark Arisen expansion launches on October 9, 2026, with a $29.99 price tag. Ahead of the expansion release, Capcom is also planning to release two free updates to the base game. The first will land tomorrow, June 10, bringing more accessible fast travel with an Eternal Ferrystone and other quality-of-life adjustments. The second update will land sometime in August, aiming to improve frame rates, add more save slots, and bring even more community-requested adjustments. This expanded Dark Arisen edition is also launching on the Nintendo Switch 2 on the same day the content comes to PC, Xbox Series X|S, and PlayStation 5.
    • Classic themes are just the colors on the bar like the olden days, if you use the image themes, it does fancy transparent backgrounds and it makes the elements of the app look like they are transparent bubbles. This sample image shows what it looks like.  
    • Good point, unfortunately. NextDNS has far more filters and workarounds than uBlock, and it's easy to implement.
    • Windows 10 KB5094127 Patch Tuesday improves File Explorer search and more by Taras Buria The June 2026 Patch Tuesday updates are here, bringing mandatory patches to users with PCs enrolled in the Extended Security Update program for Windows 10. Microsoft is rolling out KB5094127, with build numbers 19045.7417 and 19044.7417. Changelog includes the following: [File Explorer] This update improves File Explorer search, including support for Chinese text, and UTF 8–encoded files without a byte order mark (BOM). Text now displays more clearly and consistently across search results, Content view, and tooltips. [Secure Boot] This update enables dynamic status reporting for Secure Boot states in Windows Security App. This update adds a new policy setting, LimitSecureBootRequiredServiceData, under Computer Configuration > Administrative Templates > Windows Components > Secure Boot. When this setting is enabled, Windows limits the Secure Boot service data it sends by suppressing the event normally sent to Microsoft. This policy is also included in the Windows Restricted Traffic Limited Functionality Baseline package. For information about the policy, see Manage connections from Windows 10 and Windows 11 operating system components to Microsoft services. With this update, Windows quality updates include additional high confidence device targeting data, increasing coverage of devices eligible to automatically receive new Secure Boot certificates. Devices receive the new certificates only after demonstrating sufficient successful update signals, maintaining a controlled and phased rollout. As for known bugs, Microsoft has the following to say: A workaround is available in the official documentation. Today's updates are available for PCs enrolled in the Extended Security Updates program only. If your PC is eligible, you can download the update from Settings > Windows Update or from the Microsoft Update Catalog here.
    • Then the solution is to not let children have easy access to smart phones or internet until they are older, not mass surveillance. Only this would require parents to do actual parenting, most likely, as with any good solution to the problem.
  • Recent Achievements

    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      523
    2. 2
      PsYcHoKiLLa
      231
    3. 3
      +Edouard
      124
    4. 4
      ATLien_0
      87
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!