Find the answer to your Linux question:
Results 1 to 4 of 4
I need to write the following command sequence: A. Write a command sequence, to create a directory structure like follows: 1. level 1 - 4 directories with random alphanumeric name ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    35

    Joining multiple command sequence into one


    I need to write the following command sequence:


    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
    6. The output should be redirected to output.txt and error to error.txt


    I came up with following sequence::

    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

    The output is almost right when each command is executed in sequence.

    But when I join all the commands, I am not able to execute it.. . I guess something is missing.. . .can someone help?

    Do we need to add done at the end for every for loop?

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    700
    Hi there,

    To be able to help you properly, we'll need you to post your joined-up command and the error you are getting.

    Let us know how you get on.
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  3. #3
    Just Joined!
    Join Date
    Mar 2011
    Posts
    35
    Hey. ..thanks for reaching to help me... .

    I figured it out what was my mistake... . placing all done at the end

    Here is the command:

    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
    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" num

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    Quote Originally Posted by zsycho View Post
    Hey. ..thanks for reaching to help me... .

    I figured it out what was my mistake... . placing all done at the end

    Here is the command:

    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
    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" num
    done terminates a for loop, so without putting them at the end of each for loop you were just nesting all the loops, which is why it wasn't working.

    To pipe the output of mkdir you'll need to make the random string a variable and then make the directory and echo the variable to a txt file.
    Homework questions are against forum rules, but we can help you get the answer, just not do it for you.

    If you want the command to run a certain amount of times you could put the whole script within another for loop similar to your first one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •