Linux: whitespace im Dateinamen: Wie kann ich das lösen?

Gast #1427029
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
#1427047
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
#1427074
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
#1427077
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?

Antwort schreiben

Eine eigene Frage? Dieser Thread ist über 17 Jahre alt. Stell sie in einem eigenen Thread – dort lesen mehr Leute mit, und sie wird öfter und schneller beantwortet.

Neuen Thread starten

Eine Lösung, Ergänzung oder Rückmeldung zu diesem Thread? Dann antworte hier.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.