How to Create Files with Special Characters in Linux

How do I create files with special characters in Linux?

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.

Create UNIX special character file

Yes, you'd need to create a driver for that special character device.

For linux, I'd suggest you read Linux Device Drivers by Jonathan Corbet, Alessandro Rubini, and Greg Kroah-Hartman. (Chapter 3 talks about char drivers, but do read at least the first two chapters also.)

How to create a directory with / in its name in Linux/Unix?

Without having any specific knowledge, I don't think that's possible. '/' marks a subdirectory in a file path. If the subdirectory name contained '/' then the file path would be ambiguous.
E.g. 'myNew/Folder' would be equal to the folder 'myNew' containing the subFolder 'Folder' --> '/myNew/Folder'.
And to add - '/' in this case wouldn't be just be a special character (like ä,ö,ü in German) but a command character with a specific purpose.

Handling special characters in bash script

Please do not post so much in one question. Please one problem per question. One script per question, etc.

Make sure to check your scripts with shellcheck. It will catch your mistakes. See https://mywiki.wooledge.org/Quotes .

  1. When run without arguments:

filename=("'" name "'") inside awk script is a invalid way to pass anything with ' quotes to system() call, so you are getting unterminated ' error, as expected, because there will be 3 ' characters. Fix the AWS script, or better rewrite it in Bash, no need for awk. Maybe rewrite it all in Python or Perl.

Moreover, tmp=(name " " $i); deletes tabs and multiple spaces from filenames. It's all meant to work with only nice filenames.

The script will break on newlines in filenames anyway.


  1. When run with arguments:

$@ undergoes word splitting and filename expansion (topics you should research). Word splitting splits the input into words on spaces. Use "$@". Quote the expansions.


  1. What I want:

You'll be doing that with "$@"


  1. What I tried:

The variable content is irrelevant. You have to change the way you use the variable, not it's content. I.e. use quotes around the use of the variable. Not the content.


  1. Extra:

You did not quote the expansion. Use "$i" not $i. It's "$i"/*. $1 undergoes word splitting.


And finally, after that all, your script may look like, with GNU tools:

if (($# == 0)); then
set -- *
fi
du -hs0 "$@" |
sort -zh |
sed -z 's/\t/\x00/' |
while IFS= read -r -d '' size && IFS= read -r -d '' file; do
printf "%s " "$size";
ls -d "$file"
done

Also see How can I find and safely handle file names containing newlines, spaces or both? https://mywiki.wooledge.org/BashFAQ/001 .

Also, you can chain any statements:

if stuff; then
stuff1
else
stuff2
fi |
sort -h |
awk -f yourscriptrt

And also don't repeat yourself - use bash arrays:

args=()
if stuff; then
args=(*)
else
args=("$@")
fi
du -hs "${args[@]}" | stuff...

And so that sort has less work to do, I would put it right after du, not after parsing.

Find and replace words and special characters from specific files from linux

You have extra backslashes to escape special characters. Please try:

# find . -type f -name "gnuplot_RTre_*.gnplt" -exec sed -i 's/set xrange \[0:20]/set xrange \[0:3]/g' {} +
# The backslash in the REPLACEMENT above is unnecessary, although harmless.
# Please adopt the following instead.
find . -type f -name "gnuplot_RTre_*.gnplt" -exec sed -i 's/set xrange \[0:20]/set xrange [0:3]/g' {} +

In sed, the characters $.*[\^ need to be escaped with a backslash to be treated as literal.

[EDIT]

The right square bracket "]" is usually not a special character in regex and
you do not have to esacape it:

echo "[abc]" | sed 's/]/*/g'
=> [abc*

But "]" behaves as a metacharacter if preceded by an unescaped left square bracket "[" to compose a character class.

echo "[abc]abc" | sed 's/[abc]/*/g'
=> [***]***

In order to make "[" to be literal, we need to escape it.

echo "[abc]abc" | sed 's/\[abc]/*/g'
=> *abc

"]" can be also escaped just for visual symmetricity.

echo "[abc]abc" | sed 's/\[abc\]/*/g'
=> *abc

Linux cat inline file with special characters

After thinking more about this issue, I realized that quoting the word/delimiter (as suggested by the other answer) should not affect the output of the script (parameter expansion, command substitution, and arithmetic expansion are not being performed in my script).

So, \000 indeed disappears since you cannot use NULL character in either variable, or an argument of the command line.

But, \001 disappears due to a bash bug. That value is used internally by bash as an escape character. The bug occurs in function expand_word_internal which does not properly handle that escape character (see bug report here).

The bug is already fixed in the bash devel branch.

Bash script adding special characters to the end of any file I create

the ^M (CR) characters in your cat -A output are the problem: the shell treats only LF as the "end of line" marker, and the preceding CR character becomes part of the previous word. only '/' and '\0' characters are forbidden in pathnames in POSIX(ish) systems.

you can fix your script with dos2unix or with

vim yourfile.sh
:set ff=unix
:wq


Related Topics



Leave a reply



Submit