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?
Asked
Active
Viewed 3.4k times
9

Ronan Boiteau
- 9,608
- 6
- 34
- 56

Cameron Shirley
- 127
- 1
- 1
- 8
-
This probably belongs here: https://unix.stackexchange.com/ – Christian Gibbons Apr 23 '18 at 19:10
-
2I suspect some of those characters are disallowed in filenames: https://stackoverflow.com/questions/4814040/allowed-characters-in-filename?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – yano Apr 23 '18 at 19:12
-
I don't think they are disallowed, this is a question on a exercise that I am trying to do so there has to be an answer to it right? I doubt they'd ask a question that has no actual answer. – Cameron Shirley Apr 23 '18 at 19:19
-
Is is very unlikely that a fully specified filename will be allowed to contain the characters `?` or `*`. – Weather Vane Apr 23 '18 at 19:23
-
5The only [characters that are disallowed in filenames](https://unix.stackexchange.com/questions/230291/what-characters-are-valid-to-use-in-filenames) are `\0` (NUL) and `/`. `?` and `*` are fine, as are spaces, newlines, quotes, backslashes, and anything else. – John Kugelman Apr 23 '18 at 19:45
1 Answers
14
You need to escape special characters with the backslash symbol (\
).
This command will create a file named "\?$*'KwaMe'*$?\"
:
touch \"\\\?\$\*\'KwaMe\'\*\$\?\\\"
Explanation
- Double your
\
, like this:\\
, so that your shell does not interpret the backslashes from your filename as escape characters. - Escape
"
and'
, like this:\"
,\'
, so that your shell interprets the double quotes as part of the filename. - Escape
$
, like this:\$
, otherwise your shell will think you're using a variable. - Escape
?
and*
, like this:\?
,\*
, to prevent filename expansion.

Ronan Boiteau
- 9,608
- 6
- 34
- 56
-
How to create file with `ulr` like filenames from the script? I mean I have the list of urls `https://www.target.com` and iterate it `touch ./reports/$line.html` – storenth Jul 31 '20 at 10:17
-
2@kirill.z This is a little too different from the original question. You should ask a new question, this way it will be easier to find for people who will have the same issue in the future. But feel free to share the link to your question here in the comments if you want me to have a look :) – Ronan Boiteau Aug 05 '20 at 19:53
-
Can you add additional explanation on how to delete and list these file in `ls` and `rm` command. I found that most of infected file has auto create file with this kind of special char file name. – Mr Hery May 27 '23 at 10:55