1

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

4

1 回答 1

4

您想要什么并不完全清楚,但也许:

for i in *.txt; do mkdir "${i%-*}"; done
于 2013-03-11T20:32:31.353 回答