{"id":526958,"date":"2022-12-14T20:00:00","date_gmt":"2022-12-14T17:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-deal-with-spaces-in-filenames-on-linux\/"},"modified":"2022-12-14T20:00:00","modified_gmt":"2022-12-14T17:00:00","slug":"how-to-deal-with-spaces-in-filenames-on-linux","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-deal-with-spaces-in-filenames-on-linux\/","title":{"rendered":"#How to Deal With Spaces in Filenames on Linux"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a27e2b613daa\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a27e2b613daa\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-deal-with-spaces-in-filenames-on-linux\/#%E2%80%9CHow_to_Deal_With_Spaces_in_Filenames_on_Linux%E2%80%9D\" >&#8220;How to Deal With Spaces in Filenames on Linux&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-deal-with-spaces-in-filenames-on-linux\/#The_Humble_Filename\" >The Humble Filename<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-deal-with-spaces-in-filenames-on-linux\/#Why_Spaces_in_Linux_File_Names_Are_a_Pain\" >Why Spaces in Linux File Names Are a Pain<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-deal-with-spaces-in-filenames-on-linux\/#How_to_Quote_and_Escape_Spaces\" >How to Quote and Escape Spaces<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-deal-with-spaces-in-filenames-on-linux\/#How_to_Fix_the_Space_Problem_at_Its_Source\" >How to Fix the Space Problem at Its Source<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-deal-with-spaces-in-filenames-on-linux\/#Tab_Expansion_Makes_Dealing_With_Spaces_Easy\" >Tab Expansion Makes Dealing With Spaces Easy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-deal-with-spaces-in-filenames-on-linux\/#How_to_Use_Filenames_With_Spaces_in_Bash_Scripts\" >How to Use Filenames With Spaces in Bash Scripts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-deal-with-spaces-in-filenames-on-linux\/#Spaced_Out_But_Not_Flaky\" >Spaced Out, But Not Flaky<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Deal_With_Spaces_in_Filenames_on_Linux%E2%80%9D\"><\/span>&#8220;How to Deal With Spaces in Filenames on Linux&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-791996\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/03\/shutterstock_321627716.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Linux laptop showing a bash prompt\" width=\"1200\" height=\"675\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/linux-interface-screen-notebook-world-map-321627716\">fatmawati achmad zaenuri\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>If you have a filename with spaces on a Linux system, wr<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ing your filename in quote marks lets Bash treat it correctly. Tab completion makes entering filenames on the command line easy, even if they contain spaces.<\/p>\n<p>Like most operating systems, Linux supports filenames with spaces in them. But using these filenames on the command line isn\u2019t always straightforward. Here are several ways you can handle filenames containing spaces.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"The_Humble_Filename\"><\/span>The Humble Filename<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Everything stored on your computer\u2019s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/820962\/best-internal-hard-drives\/\">hard drive needs to have a name. Without a name, no files would exist. All of the applications and daemons that are launched when your computer boots up, and all of the software that you use, has to be identified and stored in a file system.\u00a0That identification is the name of the file.\u2019<\/p>\n<p>The same thing applies to the files that you create or install. All your documents, images, and music need filenames. Without filenames, none of your digital assets could exist.\u00a0Because filenames are so important, Linux tries hard to impose as few rules about their composition as it can.<\/p>\n<p>On Linux, a filename may contain any character apart from the forward slash \u201c<code>\/<\/code>\u201d and the null character, <code>0x00<\/code>. The null character is used to mark the end of a string, so it can\u2019t be present in the string itself, or Linux would truncate the filename at the position of the null character. The \u201c<code>\/<\/code>\u201d forward slash is used as the separator in directory paths.<\/p>\n<p>Filenames are case-sensitive,\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/torvalds\/linux\/blob\/master\/include\/uapi\/linux\/limits.h\">and can be up to 255 bytes long<\/a>, including the null character. Directory paths can be up to 4096 bytes long, including the null character. Note that this is their length in\u00a0<em>bytes<\/em>, which might not equate directly to <em>characters<\/em>. 16-bit Unicode characters, for example, take two bytes each.<\/p>\n<p>Retro-computing enthusiasts and those with long memories will know that in the early days of personal computers, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/MS-DOS\">Microsoft\u2019s Disk Operating System<\/a>, DOS, was case-insensitive and had a filename limit of eight characters, plus a three-character extension.<\/p>\n<p>You had to be very thoughtful and sometimes creative when you named files. By comparison, the freedom we have today means we can name files whatever we want, with little thought to anything other than the description we\u2019re creating for that file.<\/p>\n<p>But with filenames, what <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/trip-and-travel\/\" data-internallinksmanager029f6b8e52c=\"10\" title=\"Trip &amp; Travel\" target=\"_blank\" rel=\"noopener\">trip<\/a>s us up most often isn\u2019t the characters we type, it\u2019s the spaces between them.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Why_Spaces_in_Linux_File_Names_Are_a_Pain\"><\/span>Why Spaces in Linux File Names Are a Pain<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Shells such as Bash will interpret a space-separated string of words as individual command arguments, not a single argument. Here\u2019s an example, using <code>touch<\/code> to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/199687\/how-to-quickly-create-a-text-file-using-the-command-line-in-linux\/\">create a new file called \u201cmy new file.txt.\u201d<\/p>\n<pre>touch my new file.txt<\/pre>\n<pre>ls<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850205\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/1-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Trying to use touch to create a file with spaces in its name\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>As we can see, <code>ls<\/code> shows us that there are three files created, one called \u201cmy\u201d, another called \u201cnew\u201d, and one more called \u201cfile.txt.\u201d<\/p>\n<p>Note that <code>touch<\/code> didn\u2019t complain or throw an error. It carries out what it thinks we\u2019re asking it to do. So it silently returns us to the command line. If we\u2019re not motivated to check, we won\u2019t know things haven\u2019t gone according to plan.<\/p>\n<p>To create the file we wanted, we\u2019ve got to quote or escape.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_to_Quote_and_Escape_Spaces\"><\/span>How to Quote and Escape Spaces<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If we quote the entire filename, <code>touch<\/code> knows it needs to treat the quoted text as a single argument.<\/p>\n<pre>touch 'my new file.txt'<\/pre>\n<pre>ls<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850207\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/2-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using touch to create file with spaces in its name, with the whole filename enclosed in quotes\" width=\"644\" height=\"150\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This time we get the single file that we expect.<\/p>\n<p>We can get the same result if we use the backslash character \u201c<code>\\<\/code>\u201d to escape the spaces. By \u201cescaping\u201d the spaces they\u2019re not treated as special characters\u2014that is, argument separators\u2014they\u2019re considered to be plain old spaces.<\/p>\n<pre>touch my\\ second\\ new\\ file.txt<\/pre>\n<pre>ls<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850209\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/3-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using touch to a create a file with spaces in its name escaped with backslashes\" width=\"644\" height=\"150\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>That works, but escaping spaces makes typing filenames slower and error-prone. Things can get really ugly if you have directory names with spaces in them too.<\/p>\n<pre>cp dir\u00a0one\/my\\ text\\ file.txt dir\\ two\/my\\ text\u00a0file.bak<\/pre>\n<pre>ls<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850212\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/4-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using escaped spaces in a command to copy a file from one directory to another\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>That command copies a single text file from a directory called \u201cdir one\u201d to a directory called \u201cdir two\u201d, and saves the copy as a BAK file. And it\u2019s a fairly simple example.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_to_Fix_the_Space_Problem_at_Its_Source\"><\/span><a rel=\"nofollow noopener\" target=\"_blank\" name=\"autotoc_anchor_3\">How to Fix the Space Problem at Its Source<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If they\u2019re your own files, you could take the policy decision to never use spaces, and create (or <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/207503\/how-to-quickly-batch-rename-files-on-windows-mac-os-x-or-linux\/\">bulk rename) filenames like this.<\/p>\n<pre>mynewtextfile.txt<\/pre>\n<p>Admittedly, that\u2019s a robust solution but it\u2019s still ugly. There are better options, such as using dashes \u201c<code>-<\/code>\u201d or underscores \u201c<code>_<\/code>\u201d to separate your words.<\/p>\n<pre>my-new-text-file.txt<\/pre>\n<pre>my_new_text_file.txt<\/pre>\n<p>Both of these will sidestep the problem, and they\u2019re readable. If you don\u2019t want to add extra characters to your filenames, you can use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Camel_case\">CamelCase<\/a> to make your filenames readable, like this:<\/p>\n<pre>MyNewTextFile.txt<\/pre>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Tab_Expansion_Makes_Dealing_With_Spaces_Easy\"><\/span>Tab Expansion Makes Dealing With Spaces Easy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Of course, adopting a naming convention and sticking to it will only help when you\u2019re dealing with your own files. Files that come from anywhere else are unlikely to follow your adopted naming convention.<\/p>\n<p>You can use tab expansion to help you accurately \u201cfill out\u201d filenames for us. Lets say we want to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/409115\/how-to-delete-files-and-directories-in-the-linux-terminal\/\">delete the BAK file we created in \u201cdir two\u201d, using <code>rm<\/code>.<\/p>\n<p>We start by typing \u201crm dir\u201d because we\u2019re using the <code>rm<\/code> command and we know the directory name starts with \u201cdir.\u201d<\/p>\n<pre>rm dir<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850218\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/5-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Typing the start of a command, before pressing tab to use tab completion\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Pressing the \u201cTab\u201d key causes Bash to scan for matches in the current directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850219\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/6-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Pressing tab has added a backslash and a space to the partial directory path\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>There are two directories that start with \u201cdir\u201d, and in both cases the next character is a space. So Bash adds the backslash character \u201c<code>\\<\/code>\u201d and a space. Bash then waits for us to provide the next character. It needs the next character to differentiate between the two possible matches in this directory.<\/p>\n<p>We\u2019ll type a \u201ct\u201d, for \u201ctwo\u201d, and then press \u201cTab\u201d once more.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850221\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/7-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The directory path after typing &quot;t&quot; and pressing tab\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Bash completes the directory name for us and waits for us to type the start of the filename.<\/p>\n<p>We only have one file in this directory, so typing the first letter of the filename, \u201cm\u201d, is enough to let Bash know which file we want to use. Typing \u201cm\u201d and pressing \u201cTab\u201d completes the filename for us, and \u201cEnter\u201d executes the entire command.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850222\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/8-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The entire filename completed by pressing &quot;m&quot; and pressing Tab.\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Tab expansion makes it easy to ensure you get filenames right, and it also speeds up navigating and typing on the command line in general.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>Use Tab Completion to Type Commands Faster on Any Operating System<\/em><\/strong><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_to_Use_Filenames_With_Spaces_in_Bash_Scripts\"><\/span>How to Use Filenames With Spaces in Bash Scripts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>It\u2019s no surprise that <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/808593\/bash-script-examples\/\">scripts have exactly the same issues with spaces in filenames as the command line does. If you are passing a filename as a variable make sure you quote the variable name.<\/p>\n<p>This little script checks the current directory for files that match the file pattern \u201c*.txt\u201d, and stores them in a variable called <code>file_list<\/code>. A <code>for<\/code> loop is used to perform a simple action on each one.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\nfile_list=*.txt&#13;\n&#13;\nfor file in $file_list&#13;\ndo&#13;\n\u00a0 ls -hl $file&#13;\ndone<\/pre>\n<p>Copy this text into an editor and save it to a file called \u201cfiles.sh.\u201d Then use the <code>chmod<\/code> command\u00a0to make it executable.<\/p>\n<pre>chmod +x files.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850224\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/9-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using chmod to make the script executable\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We\u2019ve got some files in this directory. One has a simple file name, and the other two use underscores \u201c<code>_<\/code>\u201d or dashes \u201c<code>-<\/code>\u201d instead of spaces. This is what we see when we run the script.<\/p>\n<pre>.\/files.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850225\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/10-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running the files.sh script with filenames containing no spaces\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>That seems to work nicely. But let\u2019s change the files in the directory for files that contain spaces in their names.<\/p>\n<pre>.\/files.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850226\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/11-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Running the files.sh script with filenames containing spaces\" width=\"644\" height=\"335\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Every word in each filename is handled as though it was a filename on its own, and so the script fails. But all we need to do to make the script handle spaces in filenames is to quote the <code>$file<\/code> variable inside the <code>for<\/code> loop.<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\nfile_list=*.txt&#13;\n&#13;\nfor file in $file_list&#13;\ndo&#13;\nls -hl \"$file\"&#13;\ndone<\/pre>\n<p>Note that the dollar sign \u201c<code>$<\/code>\u201d is inside the quotes. We made that change and saved it to the \u201cfiles.sh\u201d script file. This time, the filenames are handled correctly.<\/p>\n<pre>.\/files.sh<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-850228\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/11\/12-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The modified files.sh script correctly handling files with spaces in their filenames\" width=\"644\" height=\"149\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Process a File Line by Line in a Linux Bash Script<\/em><\/strong><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Spaced_Out_But_Not_Flaky\"><\/span><a rel=\"nofollow noopener\" target=\"_blank\" name=\"autotoc_anchor_6\">Spaced Out, But Not Flaky<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Avoiding spaces in your own filenames will only take you so far. It\u2019s inevitable that you\u2019ll encounter files from other sources with names that contain spaces. Thankfully, if you need to handle those files on the command line or in scripts, there are easy ways to do so.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/850124\/spaces-in-filenames-on-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Deal With Spaces in Filenames on Linux&#8221; fatmawati achmad zaenuri\/Shutterstock.com If you have a filename with spaces on a Linux system, wrapping your filename in quote marks lets Bash treat it correctly. Tab completion makes entering filenames on the command line easy, even if they contain spaces. Like most operating systems, Linux supports&#8230;<\/p>\n","protected":false},"author":1,"featured_media":526959,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/03\/shutterstock_321627716.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-526958","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/526958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=526958"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/526958\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/526959"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=526958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=526958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=526958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}