如何输入名称中包含特殊字符的文件或目录?

Pom*_*rio 48 command-line filename cd-command

我想在终端中输入以下文件夹:

Milano, Torino (Jan)-Compressed
Run Code Online (Sandbox Code Playgroud)

我应该如何编写cd进入这个目录的命令?

空间和其他一些特殊字符,例如\*)(?事业问题,当我尝试在命令行或脚本,例如使用它们:

Milano, Torino (Jan)-Compressed
Run Code Online (Sandbox Code Playgroud)

通常如何在终端中输入包含特殊字符的文件或目录名称?

小智 51

该命令是不明确的,因为空格通常用于分隔参数。cd 不知道你想做什么,但你有两种可能来解决它​​:

要么“屏蔽”空格(和所有其他特殊字符),以便终端知道您将空格表示为字符而不是分隔符:

cd Milano\,\ Torino\ \(Jan\)-Compressed
Run Code Online (Sandbox Code Playgroud)

或者您将文件夹名称或路径放入引号中

cd "Milano, Torino (Jan)-Compressed"
Run Code Online (Sandbox Code Playgroud)


Ach*_*chu 28

一个小提示:标签完成;-)

  1. 只需输入第一个字母 eg cd Mi(或更多字母,如果需要)并按Tab。终端将通过完成其余单词来帮助您。

另一种方法:下降

  1. 如果您可以看到该目录并且您想使用终端访问它,只需键入:cdfirst 然后将目录拖放到终端上并点击enter


Pra*_*eek 24

写成:

cd 'Milano, Torino (Jan)-Compressed'
Run Code Online (Sandbox Code Playgroud)

否则,它被视为Milano,文件夹名称。这是因为文件夹名称中有空格。或者转义一些特殊字符:

cd Milano\,\ Torino\ \(Jan\)-Compressed/
Run Code Online (Sandbox Code Playgroud)


des*_*ert 22

tl;dr:要引用特殊字符,请使用反斜杠对其进行转义\或将其括在双" "引号或单引号中' 'Tab ?完成需要正确引用。


你要求的是Quoting

引用用于删除某些字符或单词对 shell 的特殊含义。(...) 共有三种引用机制: 转义字符单引号双引号[引自man bash]

用转义字符引用 \

未加引号的反斜杠 ( \) 是转义字符。它保留了下一个字符的字面值,除了<newline>.

因此,要输入具有特殊字符的目录或文件,请使用 将后者转义\,例如:

cd space\ dir      # change into directory called “space dir”
cat space\ file    # print the content of file “space file”
echo content > \\  # print “content” into file “\”
cat \(             # print the content of file “(”
ls -l \?           # list file “?”
Run Code Online (Sandbox Code Playgroud)

bashProgrammable Completion(又名Tab ?Completion)自动使用转义字符转义特殊字符\

用双引号引用 " "

用双引号将字符括起来会保留引号内所有字符的字面值,但$, `, \, 和当启用历史扩展时,!.

因此,要输入带有特殊字符的目录或文件,请使用双引号将文件名或路径的至少后者或大部分转义,例如:

cd space" "dir     # change into directory called “space dir”
cd spac"e di"r     # equally
cd "space dir"     # equally
cat "space file"   # print the content of file “space file”
cat "("            # print the content of file “(”
ls -l "?"          # list file “?”
Run Code Online (Sandbox Code Playgroud)

作为$`!在双引号内保留它们的特殊含义,对双引号字符串执行参数扩展命令替换算术扩展历史扩展

用单引号引用 ' '

将字符括在单引号中会保留引号内每个字符的字面值。单引号之间不能出现单引号,即使前面有反斜杠。

因此,要输入带有特殊字符的目录或文件,请使用双引号将文件名或路径的至少后者或大部分转义,例如:

cd space' 'dir     # change into directory called “space dir”
cd spac'e di'r     # equal
cd 'space dir'     # equal
cat 'space file'   # print the content of file “space file”
cat '('            # print the content of file “(”
ls -l '?'          # list file “?”
echo content > '\' # print “content” into file “\”
Run Code Online (Sandbox Code Playgroud)

你可以找到更多关于引用man bash/ QUOTING,在wiki.bash-hackers.orgtldp.org


Ser*_*nyy 10

类似 C 的字符串和 $'string'

除其他外,您可以使用$'...'引用类型来使用 ANSI-C 反斜杠字符,例如\n\t,包括您提到的那些。从 bash 4.3 手册:

$'string' 形式的词被特殊处理。单词扩展为字符串,并按照 ANSI C 标准的规定替换反斜杠转义字符。

这对于包含换行符、制表符的文件特别有用,当您编写复杂的 awk 行时,您需要使用不同的方法来区分单引号和双引号,当文件名本身包含单/双引号混杂等时。

例如,创建和列出这样的文件:

$ touch a$'*'b  c$'\n'd                                                     
$ ls  a$'*'b  c$'\n'd                                                       
a*b  c?d
Run Code Online (Sandbox Code Playgroud)

您可以使用字符十六进制值,例如:

$ touch 'file(name'
$ ls file$'\x28'name
file(name
Run Code Online (Sandbox Code Playgroud)

打印输出

与以前相同的想法 - 利用转义字符:

$ ls "$(printf "file\x28name")"                                             
file(name
$ echo "Hello World"  >  c$'\n'd                                            
$ cat "$(printf "c\nd")"                                                    
Hello World
Run Code Online (Sandbox Code Playgroud)

使用索引节点:

每个文件或目录都有与之关联的特殊数据结构,称为 inode,由特定的十进制数引用。因此,您可以使用它通过find命令间接定位具有特定 inode 的文件,并对其进行处理:

$ echo "This is a test" > file$'('name1
$ ls -i
5898996 file(name1  5898997 file?name2
$ find -type f -inum "5898996" -exec cat {} \;
This is a test
Run Code Online (Sandbox Code Playgroud)

可以使用 glob 时避免处理单个文件

当您不必处理单个文件时,只需*在将它们传递给其他命令时利用shell 中的glob 字符和引用变量即可。它使处理困难的文件名更容易:

$ for f in ./*; do echo "$f" ; done
file    name2
file(name1
Run Code Online (Sandbox Code Playgroud)

请注意使用./- 防止文件名中可能包含前导的保护措施-


Cor*_*ker 6

要打开包含空格的文件夹,请用引号将其括起来,cd "Some Directory"或使用反斜杠将空格转义,例如:cd /home/kudic/Radna\ površina

  • 或者用反斜杠转义空格,例如:`cd /home/kudic/Radna\ površina` (3认同)