create folders from a text file

ls -R $pwd  > text.txt    

The above command generates the below txt file:

engagement
groups
solo
text.txt

./engagement:
VAM01427.JPG
_VJR3208.JPG
_VJR3244.JPG
_VJR3281.JPG

./groups:
family
friends

./groups/family:
_VJR3358.JPG
_VJR3376.JPG
_VJR3379.JPG

./groups/friends:
_VJR3404-2.jpg
_VJR3416.jpg
_VJR3456.JPG

./solo:
VAM01381.JPG
VAM01385.JPG
VAM01386.JPG

Based on this text file I am trying to create the folders with mkdir command.

When I run the command: cat text.txt | xargs -L 1 mkdir
This command is generating duplicate folders

I am unable to frame the question properly..Could someone help?

Asked By: vamshi

||

From your text file, i’ve been able to do what you want (creating directory in $HOME Folder) with this cat text.txt | grep ./ | awk -F: '{ print $1}' | xargs -L 1 mkdir so we first filter the ./ lines and then remove the : at the end to create the folders.

Answered By: admstg

If you change the format of the text file to list only directories, the whole problem becomes much simpler:

find . -type d >directories.txt

Then

while IFS= read -r dir; do mkdir -p "$dir"; done <directories.txt

Here we assume that directory names might contain spaces, but definitely not newlines

Answered By: Chris Davies
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.