Reputation: 13
I have several files in one directory in Linux that look like:
John Smith-data.txt
Peter Walker-address.txt
...
I want a script that reads these filenames and creates one directory for each name in the filename, that is
John Smith
Peter Walker
If I do
for name in `ls | awk -F - '{print $1}'`; do mkdir $name; done
the following directories are created
John
Smith
Peter
Walker
by doing
for name in `ls | awk -F - '{print $1}'`; do mkdir "$name"; done
I get the same result
Then, doing
for name in "`ls | awk -F - '{print $1}'"`; do mkdir "$name"; done
creeates only one dir named:
John Smith Peter Walker
Finaly, if I enter
for name in "`ls | awk -F - '{print $1}'"`; do mkdir $name; done
again I get
John
Smith
Peter
Walker
Any ideas ? How to use the quotes ""
?
Thanks in advance
Xavier
Upvotes: 1
Views: 1556
Reputation: 212238
It's not entirely clear what you want, but perhaps:
for i in *.txt; do mkdir "${i%-*}"; done
Upvotes: 4