I have a .txt file that contains a text like this
A1/B1/C1
A2/B2/C2
A3/B3/C3
I want a script that reads the .txt file for each line then create a directory based on the first word (A1, A2, A3)
I have created script like this:
file="test.txt"
while IFS='' read -r line
do
name="line"
mkdir -p $line
done <"$file"
While I run it, it creates directory A1 then it also create sub-directories B1 and C1. the same happens for another line (A2* and A3*)
What should I do to create only A1, A2, A3 directories?
I don't want to make the name like A1/B1/C1 with '/' character in it.
I just want to take the word before '/' character and make it directory name.
Just "A1" "A2" "A3".
Best Answer
You can just
cut
the1
st slash-d
elimited field of each line and give the list tomkdir
:Example run
You may run into ARG_MAX problems if your list holds a huge number of directory names, in this case use GNU
parallel
orxargs
as follows:While
parallel
has it covered, thexargs
approach won’t work if the directory names contain spaces – you can either use\0
as the line delimiter or simply instructxargs
to only split the input on newline characters (as proposed by Martin Bonner) instead:In case any of the fields contains a newline character one would need to identify the “true” line endings and replace only those newline characters with e.g.
\0
. That would be a case forawk
, but I feel it’s too far fetched here.