简体   繁体   中英

Create infinite number of nested folders using infinite loop

I am working on a small project. The task is to create a bash script that will create infinite number of folders when clicked, and inside each folder, it creates infinite number of folders in each folder and so on. For example,

  • Folder1
    • SubFolder1 SubFolder2 SubFolder3... SubFolder∞
      • SubSubFolder1 SubSubFolder2 SubSubFolder3... SubSubFolder∞
        • SubSubSubFolder1 SubSubSubFolder2 SubSubSubFolder3... SubSubSubFolder∞
  • ...
  • Folder∞
    • SubFolder1 SubFolder2 SubFolder3... SubFolder∞
      • SubSubFolder1 SubSubFolder2 SubSubFolder3... SubSubFolder∞
        • SubSubSubFolder1 SubSubSubFolder2 SubSubSubFolder3... SubSubSubFolder∞

The sequence is like this:

Folder 1 has infinite folders (SubFolder1, SubFolder2, ..., SubFolder∞). The first folder(SubFolder1) has further sub folders (SubSubFolder1, ..., SubSubFolder∞). The SubSubFolder1 has further subfolders (SubSubSubFolder1, ... SubSubSubFolder∞) and so on. Similar for Folder2 and its sub folders.

I tested the script for 10 folders. It created 10 top-level folders. In each folder, it created 10 subfolders each. But it stopped there, I want it to run continuously (infinite loop). Also, it works sequentially (it will create 10 subfolders in Folder1, then comeout and create Folder2, and its sub folders and so on), I want it continuous (make Folder1 and its subfolders continuously with Folder2 and its subfolders and so on). The code:

#!/bin/bash

# Set the number of outer and inner directories to create
num_outer=10
num_inner=10

# Create the outer loop
for ((i=0; i<num_outer; i++))
do
  # Create a new outer directory with a unique name
  mkdir "outer_folder_$i"

  # Navigate into the new outer directory
  cd "outer_folder_$i"

  # Create the inner loop
  for ((j=0; j<num_inner; j++))
  do
    # Create a new inner directory with a unique name
    mkdir "inner_folder_$j"
  done

  # Navigate back to the parent directory
  cd ..
done

This rekfolder.sh script does not produce an infinite number of directories, because the constant flow of new harddrives would be too expensive for me, but about x, (x fac) numbers of directories. where x is is given as a command line parameter and counted downwards in each recursive step.

Start it like this:

timeout -k 5 3s ./rekfolder.sh f 8

which stops the script after 3s and - if things go wrong - kills it after 5. f will be the name part of the starting dir.

#!/bin/bash

name=$1
count=$2

# save my harddrive!
if (( ${#name} > 25 ))
then
    echo err namelength
    exit 0
elif (( count == 0 ))
then
    echo err count
    exit 0
else
    # Set the number of directories to create
    num=3
    name=$name

    for ((i=0; i<count; i++))
    do
        # save my harddrive again
        sleep 0.3
        echo mkdir ${name}$i
        mkdir ${name}$i
        ./rekfolder.sh ${name}$i/ $((count-1)) &
    done
fi

This produced 109600 directories. The sleep is in there, to allow to interrupt the process of exponential growth with a killall rekfolder.sh in a second terminal, but it's getting hard, if you don't interrupt early.

You may delete all those folders, if they are created in a fresh dir, with

  find -type d -delete

(took me about a second (SSD)).

Note that there is much trailing output, long after finishing all the./rekfolder.sh scripts, which makes it look, as if the timeout does not work. You may observe the processes in a second terminal

for i in {1..10}
do 
    ps -C rekfolder.sh
    sleep 1
done 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM