Handling files/directories with spaces in `for’-loops

So I have one or the other file, that needs to be extracted to a directory. And why not name it as the archive itself .. Only problem with it is the handling of variables with bash …

Try it yourself, stuff some directories with a space in inside a variables, and use something like this:

And now take a look at the output of that ..

Means, the mkdir' created a directory for every entry in the ls‘ output that was separated by a space char … and I’ve no frickin clue on how to get that thing right … 😡

Thanks to Roy I know how one handles such things … 😳 It’s rather simple *g*

That should give you the desired effect ❗

2 thoughts to “Handling files/directories with spaces in `for’-loops”

  1. The way I learned to handle spaces in file names in shell loops is to use read like:

    ls -1 | while read i; do mkdir “${i/.archive/}”; done

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

This site uses Akismet to reduce spam. Learn how your comment data is processed.