{"id":5764,"date":"2024-03-13T10:10:41","date_gmt":"2024-03-13T15:10:41","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5764"},"modified":"2024-03-13T10:10:55","modified_gmt":"2024-03-13T15:10:55","slug":"handling-spaces-in-file-names-on-linux","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/handling-spaces-in-file-names-on-linux\/","title":{"rendered":"Handling Spaces in File Names on Linux"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Using <code>ls<\/code> to parse file names is not recommended for multiple reasons<\/p>\n\n\n\n<p><a href=\"https:\/\/mywiki.wooledge.org\/ParsingLs\">https:\/\/mywiki.wooledge.org\/ParsingLs<\/a><\/p>\n\n\n\n<p>Let&#8217;s say we have a directory with two files in it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Helloworld.txt<br>Hello, world.txt<\/pre>\n\n\n\n<p>Now we want to loop over the files.  If we use <code>ls<\/code> in our for loop,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for file in $(ls); do echo \"$file\" ; done<\/pre>\n\n\n\n<p>We receive the following output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Hello,<br>world.txt<br>Helloworld.txt<\/pre>\n\n\n\n<p>The space in &#8220;Hello, world.txt&#8221; is translated as a new line.  This could break our script.<\/p>\n\n\n\n<p>Here is a better way<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for file in * ; do echo \"$file\" ; done<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Helpful links<\/p>\n\n\n\n<p><a href=\"https:\/\/mywiki.wooledge.org\/BashPitfalls\">https:\/\/mywiki.wooledge.org\/BashPitfalls<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using ls to parse file names is not recommended for multiple reasons https:\/\/mywiki.wooledge.org\/ParsingLs Let&#8217;s say we have a directory with two files in it. Helloworld.txtHello, world.txt Now we want to loop over the files. If we use ls in our &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/handling-spaces-in-file-names-on-linux\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[211,7,1663,405],"class_list":["post-5764","post","type-post","status-publish","format-standard","hentry","category-command-line","category-linux","tag-bash","tag-linux-2","tag-ls","tag-scripting"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5764"}],"version-history":[{"count":1,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5764\/revisions"}],"predecessor-version":[{"id":5765,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5764\/revisions\/5765"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}