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

    • Just pull a 4Chan and ignore the UK gov, or better troll them. It's not like they can enforce the fine across border.
    • It has NEVER been shown that all these overreaching creepy methods of surveillance have ever saved a child or prevented a terrorist attack. Not a single one. It's the kind of people like you who just wave it away as "paranoid conspiracy" that makes big tech and governments this creepy mass data hoarding entities. Not only that, 3/4 of these surveillance ideas undermine the very foundations of safe online communication because they always want to have a backdoor in everything "just in case" they might need it to... checks the notes "save the children". If you put a backdoor into encryption chain there is no encryption chain anymore. You know what encryption keeps safe? Your medical records, your online shopping and credit card during payment, your photos in the cloud, your emails, your passwords, everything. There is ZERO guarantee only the good guys will use it. And if you think police suddenly can't apprehend child abusers because of encryption, Epstein was running his entire sex trafficking ring using GMail which is not even encrypted end to end. Or to make matters even worse, USA has a **** and a good buddy of Epstein as a president. Absolutely NOTHING has been done to address it. Maxwell just got a better "hotel" room as a reward. This clearly shows how they absolutely don't really care about the children but they care about the absolute control over all of us. And you're defending them here. Good grief. On top of constant attempts to insert backdoors into encryption chain, the entire age verification nonsense is again entirely over reaching, creepy, invades everyone's privacy with premise of yet again "protecting the children" instead of demanding device makers to provide simple and powerful tools for PARENTS to control how their children use devices and what they do on them. THIS would be the way, not the stupid age verification for everyone. Imagine if government would be dictating companies how their phones work and not the company's IT department. The parents should be the IT department to their children. And for everyone excusing "they are not knowledgeable enough" buuuuuulsheat. We live in a digital age, if you have children now, you absolutely are well versed in digital everything at least to basic extent. If you're not, how do you even function in these times then? Reality is that parents are just lazy and don't want to deal with this. They want government to raise their kids because they are too busy scrolling stupid Instagram and Tiktok or some bs.
    • You could make the argument that K should not be included, but FC, the fried chicken, is not the framework, it's the product. It's the Paint in Paint.NET. A closer analogy is if KFC included the name of the deep fryer they used. HennyPennyFC.
    • Flying as the central point eh... As a massive Spyro fan who has replayed the Reignited Trilogy three times and the originals 4 times... I have some doubts, but maybe...
    • Apple is expanding Private Cloud Compute beyond its own data centers by Pradeep Viswanathan At WWDC 2026, as part of the improved Apple Intelligence capabilities, Apple today announced that it is expanding Private Cloud Compute (PCC), its privacy-focused cloud infrastructure for Apple Intelligence, beyond its own data centers for the first time. Private Cloud Compute was designed to handle Apple Intelligence requests that are too complex to run fully on-device. The PCC system does not store user data and does not allow Apple or anyone else to access user requests. Last year, Apple also expanded its Security Bounty program with rewards of up to $1 million for researchers who could find serious vulnerabilities in PCC. Until now, Apple's PCC data centers were using Apple's own silicon. As part of the expansion, Apple is working with Google and NVIDIA to run new Apple Intelligence workloads on Google Cloud systems powered by NVIDIA GPUs. Apple will be using this new infrastructure to execute more demanding AI tasks while maintaining the same privacy and security guarantees of PCC. The new implementation uses NVIDIA Confidential Computing with NVIDIA GPUs, Intel CPUs with TDX, and Google’s Titan chip. Apple says it has worked with Google to build additional protections beyond a traditional confidential computing deployment. Despite the expansion to third-party data centers, Apple claims that its core PCC requirements remain unchanged, including stateless computation, no privileged runtime access, non-targetability, and verifiable transparency. The company highlighted that it will continue to control the PCC software stack, and Apple devices will only trust PCC software that has been cryptographically approved by Apple. To take security to the next level, Apple mentioned that it is maintaining an append-only ledger of Google Cloud hardware that is part of the PCC fleet. The company claims this will help reduce the risk of supply chain attacks. In addition to AI infrastructure, Apple also worked with Google to use technologies behind the Gemini family of models to build the next generation of Apple Foundation Models to power Apple Intelligence features across on-device and cloud workloads. As expected, for more demanding AI tasks like agentic tool use and complex reasoning, Apple will rely on the expanded PCC infrastructure running on Google Cloud. The expansion of PCC on Google Cloud will gradually ramp toward the full set of protections during the summer preview period. As before, Apple will also publish binaries for public inspection, provide research tooling, and give researchers access to live PCC nodes in research mode through the Apple Security Bounty Program.
  • Recent Achievements

    • Very Popular
      Captain_Eric earned a badge
      Very Popular
    • One Month Later
      amusc earned a badge
      One Month Later
    • One Month Later
      DJC50PLUS earned a badge
      One Month Later
    • Week One Done
      DJC50PLUS earned a badge
      Week One Done
    • Proficient
      Eric Biran went up a rank
      Proficient
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      PsYcHoKiLLa
      222
    3. 3
      ATLien_0
      92
    4. 4
      +Edouard
      86
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!