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".
xargs -a<(....)
rather than<dirlist.txt cut -d/ -f1 | xargs
? – Martin Bonner supports Monica May 24 '18 at 16:25tr
approach – just realized thatparallel
has it covered by default. – dessert May 24 '18 at 20:48xargs -a<(....)
rather than a pipe – because I like it this way, as simple as that. :) – dessert May 24 '18 at 20:51<(...)
helps with spaces as well, so it’s definitely the better choice – I don’t think anybody was using this file descriptor anyway. – dessert May 24 '18 at 20:55cut <dirlist.txt
instead of justcut dirlist.txt
? – dessert May 24 '18 at 21:06tr "\n" "\0"
to thattr \\{n,0}
? – ilkkachu May 25 '18 at 09:43tr "\n" "\0"
ortr \\n \\0
shows two arguments next to each other, space separated. In exactly the usual way to write command line arguments. Brace expansion may be common, yes, but it still brings up questions from time to time so it's not like it's immediately obvious to everyone in every case. And it only saves one character here. In the same vein, you could have writtencut -{d/,f1,z}
. It would also show the flags comma-separated next to each other. – ilkkachu May 25 '18 at 10:32-d/
obfuscated as well and advertise-d "/"
or-d '/'
instead, or frown upon run-together-options and always typels -a -l
. But if you feel this way, let me add an explanatory comment… I like to throw in unorthodox things like that in my answers just to show what’s possible. I always answer comments asking for explanations, normally extending my answer. – dessert May 25 '18 at 11:37