-2

I have a one column text file (dates.txt) and I want to create a stack of directories named after the dates elements.

How can I interpret it in bash?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Krsztr
  • 101
  • 2

3 Answers3

2

Read in the contents of the file, and make directories for each entry in your data file, like this:

while IFS= read -r dat; 
do 
   mkdir "$dat"
done < dates.txt
suspectus
  • 16,548
  • 8
  • 49
  • 57
2
xargs mkdir < dates.txt

xargs will read the lines of its stdin, and append the lines to the given command. This will minimize the number of times mkdir is invoked.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

With mapfile (Bash 4.0 or newer):

mapfile -t names < dates.txt && mkdir "${names[@]}"

This reads the lines into an array names and then calls mkdir with the expanded, properly quoted array elements as arguments.

The -t is required to remove the newline from the end of each element.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116