无法安全地将点文件转换为非点文件
我在 Mac 中运行失败
mv .* *
,
mv .* ./*
我的文件消失得无影无踪。
如何安全地将点文件转换为非点文件?
I run unsuccessfully in Mac
mv .* *
and
mv .* ./*
My files disappeared into thin air.
How can you convert dot-files to non-dotfiles safely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
或者,
如果您的系统已经支持的话,就容易多了。
在 zsh 中,您可以安全地使用 .*,但在 bash 中您必须使用
ls -d .*
or, much easier,
if your system have got it.
In zsh, you could just use .* safely, but in bash you'll have to use
ls -d .*
您不能使用
mv
来重命名多个文件。 你想要的是 mmv (得到它此处)。您必须转义星号以防止 bash 扩展它。 使用 -n 标志进行测试运行以确保将发生的情况是您想要的。
您也可以在 shell 脚本中执行此操作,但我更喜欢
mmv
因为 -n 标志显示了它会做什么。 您必须更改脚本以 echo 而不是 mv,这似乎比删除 -n 标志更危险(特别是当您变得更复杂时。You can't use
mv
to rename multiple files like that. What you want is mmv (get it here).You have to escape the asterisk to prevent bash from expanding it. Use the -n flag to do a test run to make sure what will happen is what you want.
You could also do this in shell scripting but I much prefer
mmv
because the -n flag shows what it would do. You'd have to alter your script to echo instead of mv, which seems more dangerous than dropping the -n flag (especially when you get more complicated.棘手的部分是选择点文件而不选择“.”。 和 ”..”。
ls .??*
有时用于此目的,因为它强制文件名长度为三个或更多字符。 不过,存在忽略短名称(例如“.x”)的点文件的风险。ls -d .*
会阻止目录扩展,但不会过滤掉“.”。 或“..”find
命令,如find 中所示。 -maxdepth 1 -type f -name '.*'
。 最大深度将其限制为当前目录而不是子目录。 -type f 将其限制为文件,消除诸如“.”之类的目录。 和 ”..”。 话又说回来,也许您想将 .ssh 目录重命名为 ssh。这是选择点文件同时避免“.”的替代方案。 和 ”..”。
-A 列出所有文件和点文件,但删除“.”。 和我们的“..”。 然后 sed 命令仅选择那些带有“.”的行。 作为第一个字符,并打印出适当的“mv”命令,并加上引号,以防您有一个奇怪的带有空格的点文件名。
首先在不带“| bash”的情况下运行它,以查看生成了哪些 mv 命令。
The tricky part about this is selecting dotfiles without selecting "." and "..".
ls .??*
is sometimes used for this, since it forces the filenames to be three or more characters long. There is a risk though, of overlooking a dotfile with a short name, such as ".x"ls -d .*
prevents directories from being expanded, but it doesn't filter out "." or ".."find
command could be used, as infind . -maxdepth 1 -type f -name '.*'
. The maxdepth limits it to the current directory and not subdirectories. The -type f limits it to files, eliminating directories such as "." and "..". Then again, maybe you want to rename the .ssh directory to ssh.Here's an alternative that selects dotfiles while avoiding "." and "..".
The -A lists all files and dotfiles, yet eliminates "." and ".." for us. Then the sed command selects only those lines with "." as the first character, and prints out appropriate "mv" commands, complete with quotes in case you have a bizarre dotfilename with a space in it.
Run it without the "| bash" first, to see what mv commands are generated.
我不知道你使用的是什么类型的系统,但它看起来像unix,我会做
ls -1 .?* | 切-b1- | xargs -i{} mv .{} {}
此列表列出以 . 开头但不是 . 的所有内容。 或 ..,然后切掉第一列,然后将该列表通过管道传递给移动命令
i don't know what type of system you're on, but it looks unix like, i would do
ls -1 .?* | cut -b1- | xargs -i{} mv .{} {}
this lists, everything that starts with a ., but isn't . or .., then cut the first column off, then pipe that list to a move command
在 Linux 中,通常有一个
rename
实用程序可用(一个 perl 脚本,如果我没记错的话):它在 Mac 上可用。 您可以按照此处的提示进行安装。
In Linux, there is usually a
rename
utility available (a perl script, if I am not mistaken):It is available on a Mac. You can install it by following tips at here.
更简单:
Even simpler: