Welcome to the new Linux Foundation Forum!

How to create folder name same as text file

1. I need guide on how to create a folder name from the text file with .txt format.

2. First, I call the function of reading the directory.

3. Then, I dont know how to do.

4. Finally, I close the directory

This is my source code in perl

## read text.txt file ##

open F, "from/$directory/text.txt";

read F, $buf, 9999;

close F;[/size]

This source code is used to print the folder with date format like "ddmm" d stands for day and m stands for month

if ($command =~ s/-O "(.*)"/-O "$websites\/$month\/$file"/)

Instead, I want to have the name from text.txt to be folder name

Comments

  • fedevfedev Posts: 9
    Let's see if I understand.

    You have a file, let's say test.txt which has many lines, each line is the name of a folder to be created. Then you would do something like:
    cat test.txt | xargs -I{} mkdir {}
    

    to create the folders? Is that what you want?
  • Thanks for reply, u are really helpful.

    1. For ur info, I only need to create one folder since I only gt one line there too.

    2. so is it, cat test.txt | mkdir {}
  • fedevfedev Posts: 9
    what I wrote will work if you are running the command from the terminal and you are in the same folder as your test.txt file. Obviously the code could be used in a script and then you could pass some parameters to it (such as the location of the test.txt file). Also the command I wrote is to create a single level directory. So if you want to create two or more levels, it should be like this:
    cat test.txt | xargs -I {} mkdir -p {}
    

    And if you want to make it a script, then it should be as:
    #!/bin/bash
    if [[ $# -ne 1 [[
    then
            echo "This command requires one argument, the location of the test.txt file"
            exit 1
    fi
    
    cat "$1" | xargs -I {} mkdir -p {}
    
    

    It could still be improved a lot but at least will give you an idea of where to start.

    And, talking about where to start, it would be a good idea to check the following link out.

    Advance Bash Scripting Guide
  • LimpalotLimpalot Posts: 2
    Or for a script that's easier to understand:
    #!/bin/bash
    dirname=`cat test.txt`
    mkdir $dirname
    
    This works if there is just one line to create one directory
    If you have multiple lines and want to create multiple directories:
    #!/bin/bash
    for i in `cat test.txt`; do
       mkdir $i
    done
    
Sign In or Register to comment.