bash 命令“rm *~”有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4572809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the bash command "rm *~" do?
提问by Mike
Does the bash command rm *~just remove files ending in tilde or is there a more advanced bash or gnu make pattern here? Google does not seem able to search for this two symbol combination. I found this in a Makefile clean: target.
bash 命令rm *~只是删除以波浪号结尾的文件还是这里有更高级的 bash 或 gnu make 模式?谷歌似乎无法搜索这两个符号组合。我在 Makefile clean: target 中发现了这个。
Would gnu make ever create files with trailing ~'s using only the implicit rules?
gnu 是否会只使用隐式规则创建带有尾随 ~ 的文件?
采纳答案by PleaseStand
The ~(tilde) character has a special meaning in a path in two cases:
~在两种情况下,(波浪号)字符在路径中具有特殊含义:
~user # the home directory of user
~/folder # folder inside your home directory
For the most part, that's it. The command you refer to does exactly what it looks like it does: removes files whose names end in a tilde. Text editors such as emacs save backup copies of files under filenames ending in tildes.
大多数情况下,就是这样。您引用的命令与它看起来的完全一样:删除名称以波浪号结尾的文件。诸如 emacs 之类的文本编辑器将文件的备份副本保存在以波浪号结尾的文件名下。
So, this command is probably used to remove these backup copies from the current directory (but not subdirectories). One reason why one would want to do so is if the directory will be copied to a web server, as server-side code (e.g. PHP files) can contain sensitive information such as passwords.
因此,此命令可能用于从当前目录(但不是子目录)中删除这些备份副本。这样做的原因之一是如果目录将被复制到 Web 服务器,因为服务器端代码(例如 PHP 文件)可能包含敏感信息,例如密码。
回答by camh
As you guessed, rm *~just removes file with names ending with a tilde (~). Filenames ending with a tilde are usually backup files created by editors (in particular, emacs was one of the earlier editors to use this convention). After editing source code, it is common to have a number of these files left behind. This is why the cleantarget in the Makefile removes these.
如您所料,rm *~只需删除名称以波浪号 (~) 结尾的文件。以波浪号结尾的文件名通常是由编辑器创建的备份文件(特别是,emacs 是早期使用此约定的编辑器之一)。编辑源代码后,通常会留下许多这样的文件。这就是cleanMakefile 中的目标删除这些的原因。
Whether *~is some special bash pattern is not relevant for most makefiles, as /bin/sh is used by default to execute make recipes. Only if SHELL is set in the makefile will a different shell be used.
是否*~有一些特殊的 bash 模式与大多数 makefile 无关,因为 /bin/sh 默认用于执行 make recipes。只有在 makefile 中设置了 SHELL 时,才会使用不同的 shell。
An easy way to see make's implicit rules is to run make -pin a directory without a makefile. You will get an error saying no targets specified, but make will also print out the implicit rules it is using. If you grep this output for a tilde, you'll see there are no implicit rules that name files with it.
查看 make 的隐式规则的一种简单方法是make -p在没有 makefile 的目录中运行。你会得到一个错误,说没有指定目标,但 make 也会打印出它正在使用的隐式规则。如果您为波浪号搜索此输出,您将看到没有用它命名文件的隐式规则。
回答by slezica
Nope, just what you said. Removes files ending with ~.
没有,就是你说的。删除以~.
Edit -> the only special meaning the ~character may have, is as short-hand for the the current user's home directory (as $HOME), but only in the beginning of a path.
编辑 ->~字符可能具有的唯一特殊含义,是当前用户主目录的简写(如$HOME),但仅位于路径的开头。
回答by DigitalRoss
Yes to both
是的
Actually, both of your possibilities are somewhat true.
实际上,您的两种可能性都有些成立。
There is no wildcard or special filename syntax associated with ~, unless it occurs at the beginning of a word.
没有与 关联的通配符或特殊文件名语法~,除非它出现在单词的开头。
But the filename pattern ending in tilde isproduced automatically by the mv(1)and cp(1)programs on most linux distros1if the -b(backup)option is specified and the target file exists. A make rule on such a system might contain a mv -b ...or cp -b ...command.
但是在波浪结束文件名模式是自动生成的mv(1),并cp(1)计划在大多数Linux发行版1,如果-b(备份)指定的选项和目标文件存在。此类系统上的 make 规则可能包含mv -b ...orcp -b ...命令。
1. But not on the Mac or BSD.
1. 但不适用于 Mac 或 BSD。
回答by Fernando Miguélez
I have used that command to erase files ending in "~". I think that there is no special escape character associated with the tilde symbol.
我已使用该命令擦除以“~”结尾的文件。我认为没有与波浪符号相关的特殊转义字符。

