Welcome to the 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

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Comments

  • 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:
    1. 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 {}
  • 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:
    1. cat test.txt | xargs -I {} mkdir -p {}

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

    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
  • Posts: 2
    Or for a script that's easier to understand:
    1. #!/bin/bash
    2. dirname=`cat test.txt`
    3. 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:
    1. #!/bin/bash
    2. for i in `cat test.txt`; do
    3. mkdir $i
    4. done

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training