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

    • Well Statcounter doesn't count Edge users as Chrome. It uses the user agent and my understanding is the Brave browser users the standard Chrome user agent for privacy/tracking reasons and compatibility so that would help the Chrome numbers. Some Firefox users change the user agent for again compatibility reasons. I am going to totally guess the Chrome numbers are inflated some 5 or 6%.
    • BATorrent 4.0.0 by Razvan Serea BATorrent is a lightweight, open-source BitTorrent client built with modern C++ and Qt 6, offering a clean, fast, and privacy-focused alternative to traditional torrent apps. It supports magnet links, .torrent files, resume data, sequential downloading, per-file priorities, and even imports from qBittorrent. Power users benefit from integrated RSS auto-download with regex filtering, duplicate detection, and automatic tracker lists from Stremio. Streaming is seamless thanks to auto-detected players like VLC and IINA. BATorrent includes robust VPN tools—interface binding, auto-detection for WireGuard-based services like Mullvad and NordLynx, kill switch, proxy support, and IP filtering. A full WebUI enables remote control, while integrations with Plex, Jellyfin, and Emby automate library updates. With themes, speed scheduling, system-tray alerts, and cross-platform support for Windows, Linux, and macOS, BATorrent delivers a polished, high-performance torrenting experience. BATorrent features: Core .torrent file and magnet link support Resume data — picks up where you left off after restart Import torrents from qBittorrent Create .torrent files from any file or folder Sequential download mode Per-file priority control (skip, low, normal, high) Seed ratio limits with auto-pause DHT, PEX, UPnP, NAT-PMP RSS Auto-Download Subscribe to RSS feeds — automatically download new torrents as they appear Regex filters — match only what you want (e.g. 1080p|720p, S01E\d+) Per-feed settings — custom save path, check interval (5–1440 min), enable/disable Auto-download — matched items are downloaded automatically in the background Supports magnet links, .torrent URLs, and tags Tray notifications when items are auto-downloaded Duplicate detection — never downloads the same item twice Stremio Stremio Addon System pre-installed — works out of the box Auto tracker list from ngosang/trackerslist Streaming Play while downloading — stream video files before the download is complete Supports mp4, mkv, avi, mov, wmv, flv, webm, m4v, ts Auto-detects installed players (VLC, IINA, system default) VPN & Privacy Interface binding — lock torrent traffic to a specific network interface (e.g. tun0) Auto VPN detection — identifies VPN interfaces (tun, tap, WireGuard, Mullvad, NordLynx, ProtonVPN) Kill switch — automatically pauses all torrents if the VPN interface drops Auto-resume — resumes only the torrents paused by the kill switch when VPN reconnects Proxy support — SOCKS5 and HTTP proxy with optional authentication IP filtering — load P2P blocklists to block unwanted IP ranges Protocol encryption (enabled / forced / disabled) WebUI Remote management — control torrents from any browser at http://localhost:8080 REST API with JSON responses Add torrents via magnet link or .torrent upload Pause, resume, remove torrents remotely View peers and files per torrent Dark theme matching the desktop app HTTP Basic Auth with SHA-256 password hashing Configurable port and remote access (localhost vs 0.0.0.0) Interface 3 themes: Dark, Light, Midnight (bat/vampire aesthetic) Real-time speed graph Detailed panel with tabs: General, Peers, Files, Trackers Filter bar: search by name, filter by state (Active, Downloading, Seeding, Paused, Finished) Drag & drop .torrent files and magnet links Drag & drop reorder in torrent list System tray with notifications (download complete, kill switch events, RSS auto-downloads) Splash screen with bat animation Bilingual: English and Portuguese (BR), auto-detected from system locale Bandwidth Scheduler Alternative speed limits — set different download/upload limits on a schedule Time range — configure active hours (e.g. 01:00 to 07:00), supports overnight ranges Per-day control — choose which days of the week the schedule applies Automatically switches between normal and alternative speeds Media Server Integration Plex — automatically trigger library scan when a download completes Jellyfin / Emby — same automatic library refresh via API Configure server URL and authentication token/key in Settings System Cross-platform: Windows, Linux, macOS Auto-shutdown — automatically shut down PC when all downloads complete (60s cancellable countdown) Auto-update system (AppImage on Linux, installer on Windows, DMG on macOS) CLI arguments: pass .torrent files or magnet: URIs directly Keyboard shortcuts: Space to toggle pause, Ctrl+A to select all, Ctrl+O to open Download: BATorrent 4.0.0 | 37.4 MB (Open Source) Download: BATorrent Portable | 51.7 MB Links: BATorrent Website | Screenshot | Changelog Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • Week One Done
      D0nn13 earned a badge
      Week One Done
    • Reacting Well
      lamborghiniv10 earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      517
    2. 2
      PsYcHoKiLLa
      231
    3. 3
      +Edouard
      108
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!