Search code examples
linuxbashshellspecial-charactersfilenames

How do I create files with special characters in Linux?


I am using the touch command to try and create a file with the name "\?$*'KwaMe'*$?\" (quotation marks included as part of the file name). However when I type touch "\?$*'KwaMe'*$?\" in the Terminal, it doesn't give me the result I am expecting. How can I create this file?


Solution

  • You need to escape special characters with the backslash symbol (\).


    This command will create a file named "\?$*'KwaMe'*$?\":

    touch \"\\\?\$\*\'KwaMe\'\*\$\?\\\"
    

    Explanation

    1. Double your \, like this: \\, so that your shell does not interpret the backslashes from your filename as escape characters.
    2. Escape " and ', like this: \", \', so that your shell interprets the double quotes as part of the filename.
    3. Escape $, like this: \$, otherwise your shell will think you're using a variable.
    4. Escape ? and *, like this: \?, \*, to prevent filename expansion.