Forum: PC-Programmierung Linux: whitespace im Dateinamen: Wie kann ich das lösen?


von Daniel (Gast)


Lesenswert?

hello peoples,

Assume a file containing a list of filenames.

$cat list
ein langer sommerabend.txt
some other file

Now, I want to copy them all at once and write therefor a loop like this
$for i in $(cat list); do cp $i newdir; done

Obviuosly I am running into the "whitespace in a filename" problem
which I don't know how to solve. Does have somebody an advice
or a working solution for me?

Best regards, Daniel

von Chris S. (schris)


Lesenswert?

Enclose the $i in double quotes and you don´t have the problem anymore.

von Stefan E. (sternst)


Lesenswert?

Chris S. schrieb:
> Enclose the $i in double quotes and you don´t have the problem anymore.

Won't help because the input is already split into separate words by the 
"for i in $(cat list)".

(while read i; do cp "$i" newdir; done) <list

von Chris S. (schris)


Lesenswert?

Then use the command apply, or use sed instead.
Apply is more simple and useful, check the man page.

sed would be something like this, remove the echo after testing.

sed -e 's/^[ \t]*/echo cp "/' -e 's,[ \t]*$," newdir,' list |sh

von Stefan E. (sternst)


Lesenswert?

Chris S. schrieb:
> Then use the command apply, or use sed instead.
> Apply is more simple and useful, check the man page.
>
> sed would be something like this, remove the echo after testing.
>
> sed -e 's/^[ \t]*/echo cp "/' -e 's,[ \t]*$," newdir,' list |sh

Should this have any advantage over my solution?

von Fiedel (Gast)


Lesenswert?

You have to set the InternalFieldSeperator-Variable to the newline char. 
Then bash will cut the input in lines, instead of token.

http://wi-fizzle.com/article/276

von (prx) A. K. (prx)


Lesenswert?

xargs -d\\n cp -t newdir < list

von Stefan B. (stefan) Benutzerseite


Lesenswert?

[offtopic]
Usefull for filetransfers Windows <=> Linux...

detox
http://detox.sourceforge.net/
[/offtopic]

von P. S. (Gast)


Lesenswert?

cat list | while read FILE; do cp "$FILE" newdir; done

von Daniel (Gast)


Lesenswert?

Thank you.

(while read i; do cp "$i" newdir; done) <list

this one I like the most

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.