{"id":10439,"date":"2021-09-28T10:01:06","date_gmt":"2021-09-28T10:01:06","guid":{"rendered":"https:\/\/linuxways.net\/?p=10439"},"modified":"2021-09-28T10:01:06","modified_gmt":"2021-09-28T10:01:06","slug":"how-to-read-a-file-with-spaces-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-read-a-file-with-spaces-in-linux\/","title":{"rendered":"How to Read a File with Spaces in Linux"},"content":{"rendered":"<p>In Linux and UNIX systems, it\u2019s always recommended against having directories and files with spaces. Handling directories and files with spaces can be annoying and the risk with spaces in files is that they can easily be misinterpreted by applications. Some scripts and programs can break when presented with filenames with spaces in them. It\u2019s for this reason that there are naming conventions such as in web files and downloadable files which are hosted on a web server.<\/p>\n<p>Regardless, you are still bound to come across some files with spaces between them, for example, <strong>docker tutorials<\/strong> instead of <strong>docker-tutorials<\/strong>. In this guide, we will walk you through how you can read a filename with spaces in a Linux system.<\/p>\n<h2><strong>How to create and read a file with spaces<\/strong><\/h2>\n<p>Before we can explore how to read a file that contains spaces, let us see how we can create one to begin with.<\/p>\n<p>To create a file with spaces, enclose the filename inside double single quotes as shown.<\/p>\n<pre><strong>$ touch \u2018name of the file\u2019<\/strong><\/pre>\n<p>For example, we shall create a file called <strong>linuxways docs 2021<\/strong> as follows:<\/p>\n<pre><strong>$ touch \u2018linuxways docs 2021\u2019<\/strong><\/pre>\n<p>Next, we will add some content to the file.<\/p>\n<pre><strong>$ echo \u201cWelcome to Linuxways\u201d &gt;&gt; \u2018linuxways docs 2021\u2019<\/strong><\/pre>\n<p>To read the file, use the <strong>cat<\/strong> command to view the file while adding a backslash at the end of every name and a space before the next word.<\/p>\n<pre><strong>$ cat linuxways\\ docs\\ 2021<\/strong><\/pre>\n<p>An easy way is to invoke the <strong>cat<\/strong> command, type the first few letters and press TAB to autocomplete. Your Linux system is smart enough to detect the filename and place the backslashes and spaces between the file names.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"689\" height=\"226\" class=\"wp-image-10440\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-491.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-491.png 689w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-491-300x98.png 300w\" sizes=\"auto, (max-width: 689px) 100vw, 689px\" \/><\/p>\n<h2><strong>How to create and access a directory with spaces<\/strong><\/h2>\n<p>To create a directory with spaces, use the <strong>mkdir <\/strong>command with a backslash at the end of every name with a space separating it and the next name. In the example below, we have created a directory called <strong>marketing reports 2021<\/strong>.<\/p>\n<pre><strong>$ mkdir marketing\\ reports\\ 2021<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"688\" height=\"196\" class=\"wp-image-10441\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-492.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-492.png 688w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-492-300x85.png 300w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/p>\n<p>To navigate into the directory, use the <strong>cd<\/strong> command and type the directory name with the same format as demonstrated before.<\/p>\n<pre><strong>$ cd marketing\\ reports\\ 2021<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"701\" height=\"121\" class=\"wp-image-10442\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-493.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-493.png 701w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-493-300x52.png 300w\" sizes=\"auto, (max-width: 701px) 100vw, 701px\" \/><\/p>\n<p>Any other operation involving the directory will require you to use the same format of inserting backslashes after every word.<\/p>\n<p>For example, to copy the directory to another directory, for example, the <strong>\/opt <\/strong>directory, run the command:<\/p>\n<pre><strong>$ sudo cp -R marketing\\ reports\\ 2021 \/opt\/<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"664\" height=\"149\" class=\"wp-image-10443\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-494.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-494.png 664w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-494-300x67.png 300w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>While it\u2019s generally a bad idea to create files with spaces between them, you will occasionally bump into them and this may present unique challenges. In this walkthrough, we have presented ways that you can read such files and also handle directories with filenames that contain spaces.<\/p>","protected":false},"excerpt":{"rendered":"<p>In Linux and UNIX systems, it\u2019s always recommended against having directories and files with spaces. Handling directories and files with spaces can be annoying and the risk with&hellip;<\/p>","protected":false},"author":1,"featured_media":10620,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[135,35],"class_list":["post-10439","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-files-2","tag-linux"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/10439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=10439"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/10439\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/10620"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=10439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=10439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=10439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}