Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»Handling filenames with spaces in Linux

    Handling filenames with spaces in Linux

    RahulBy RahulSeptember 15, 20213 Mins Read

    It’s normal that we make files and directories (or we can say folders) in our machines to keep them organized, so when we need to, we can easily search for them. Sometimes we save them with the names having spaces, for example, we save a file with the name “my file” now in this case the Linux terminal will create an error. Can files not be saved with spaces in Linux? Yes! we can but they will be accessed differently in the terminal.

    This write-up is focussing on what errors we face while accessing files and directories with space in their names and how to avoid such errors.

    How to create a file with spaces in its name in Linux

    To understand how to reference a filename with spaces in Linux, we will consider an example. First, we will open the terminal.

    Then create a file with the name “my file” by using the touch command:

    touch my file 
    

    Now see the file is being created or not by using the “ls” command. We observed that instead of one, two files have been created, one with “my” and the second with the “file” name.

    To use spaces in the name we use either quotes (‘ ’) or escape sequence (\). Now we again make another file using (‘ ’) and another using (\).

    touch 'my file'  
    touch test\ file 
    

    Now again use the “ls” command to view files.

    Files have been created. Let’s check if we get the same errors in the creation of a directory using space or not. We will create a directory using space with the mkdir command.

    mkdir new collection 
    

    We will view whether the directory has been created or not using the “ls ” command. It created two directories instead of one.

    We can rectify this in the same way as we did in the file creation method by using (‘ ‘) or (\). Again make a directory using this (‘ ‘) or (\).

    mkdir my\ new\ collection 
    

    Now we will check out the results.

    How to reference filename with spaces in Linux

    So we can see that the directory has been created according to our requirements. Now if we want to view the contents of the file, we simply use cat and file name with spaces; it will give us an error that the directory is not available.

    cat test file 
    

    We should use “\”. For example, we want to view the contents of a test file by using the cat command.

    cat test\ file 
    

    The file is empty so it displays no results but the command runs successfully. We can also open files by using apostrophes (‘ ’) or quotations (“ ”) as:

    cat "test file" 
    [OR]
    cat 'test file' 
    

    Delete filenames with Spaces in Linux

    Similarly, you can also delete a file and directory with space in their name by using apostrophes ( ‘ ’ ), quotation marks (“ ”) or escape sequence ( \ ).

    rm -f 'test file' 
    

    Similarly, you can delete a directory with spaces in the name.

    rm -f 'my new collection' 
    

    Conclusion

    We create files and directories, without bothering to focus on the names that can have spaces. The Linux terminal treated files and folders differently that have spaces in their names. So this article solved the problem. If we want to name a file or directory with spaces we can do it by using apostrophes ( ‘ ’ ) , quotation marks (“ ”) or escape sequence ( \ ).

    command files
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Install PHP (8.1, 7.4 & 5.6) on Debian 11
    Next Article How to Install Redis on Debian 11 Linux

    Related Posts

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    (Resolved) -bash: /bin/mv: Argument list too long

    Updated:January 13, 20222 Mins Read

    Tee Command in Linux with Examples

    4 Mins Read

    How to Scan Open Ports with Nmap

    5 Mins Read

    How To Compare Two Files in Linux

    Updated:August 23, 20215 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.