无法安全地将点文件转换为非dotfiles

时间:2021-07-20 10:40:51

I run unsuccessfully in Mac

我在Mac上运行不成功

mv .* *

and

mv .* ./*

My files disappeared into thin air.

我的文件消失得无影无踪。

How can you convert dot-files to non-dotfiles safely?

如何安全地将点文件转换为非dotfiles?

6 个解决方案

#1


for i in `ls -d .*`; do mv $i "`echo $i | sed 's/^.//'`"; done

or, much easier,

或者,更容易,

rename 's/^.//' `ls -d .*`

if your system have got it.

如果你的系统有它。

In zsh, you could just use .* safely, but in bash you'll have to use ls -d .*

在zsh中,你可以安全地使用。*但是在bash中你必须使用ls -d。*

#2


You can't use mv to rename multiple files like that. What you want is mmv (get it here).

您不能使用mv重命名这样的多个文件。你想要的是mmv(在这里得到)。

mmv .\* \#1

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.

你必须逃避星号以防止bash扩展它。使用-n标志进行测试运行,以确保将要发生的是您想要的。

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.

您也可以在shell脚本中执行此操作,但我更喜欢mmv,因为-n标志显示它将执行的操作。你必须改变你的脚本而不是mv,这似乎比删除-n标志更危险(特别是当你变得更复杂时)。

#3


The tricky part about this is selecting dotfiles without selecting "." and "..".

关于这个的棘手部分是选择dotfiles而不选择“。”和“......”。

  • 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。?? *有时用于此,因为它强制文件名长度为三个或更多字符。但是,忽略一个短名称的点文件,例如“.x”存在风险

  • ls -d .* prevents directories from being expanded, but it doesn't filter out "." or ".."
  • ls -d。*会阻止扩展目录,但不会过滤掉“。”要么 ”..”

  • The find command could be used, as in find . -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.
  • 可以使用find命令,如find中所示。 -maxdepth 1-type f -name'。*'。 maxdepth将其限制为当前目录而不是子目录。 -type f将其限制为文件,消除了诸如“。”之类的目录。和“......”。然后,也许你想将.ssh目录重命名为ssh。

Here's an alternative that selects dotfiles while avoiding "." and "..".

这是一个选择dotfiles同时避免“。”的替代方案。和“......”。

ls -A | sed -n 's/^\.\(.*\)/mv ".\1" "\1"/p' | bash

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.

-A列出了所有文件和dotfiles,但却删除了“。”和“..”对我们来说。然后sed命令只选择那些带有“。”的行。作为第一个字符,打印出适当的“mv”命令,如果你有一个带有空格的奇怪的dotfilename,则用引号括起来。

Run it without the "| bash" first, to see what mv commands are generated.

首先运行它而不使用“| bash”,以查看生成的mv命令。

#4


i don't know what type of system you're on, but it looks unix like, i would do

我不知道你在使用什么类型的系统,但它看起来像unix,我愿意

ls -1 .?* | cut -b1- | xargs -i{} mv .{} {}

ls -1。?* |切-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

这列出了一切以。开头,但不是。或..,然后关闭第一列,然后将该列表管道移动命令

#5


In Linux, there is usually a rename utility available (a perl script, if I am not mistaken):

在Linux中,通常有一个重命名实用程序(perl脚本,如果我没有记错的话):

rename 's/^.//' .*

It is available on a Mac. You can install it by following tips at here.

它可以在Mac上使用。您可以按照此处的提示进行安装。

#6


Even simpler:


for x in .*; do mv $x ${x/./}; done    

#1


for i in `ls -d .*`; do mv $i "`echo $i | sed 's/^.//'`"; done

or, much easier,

或者,更容易,

rename 's/^.//' `ls -d .*`

if your system have got it.

如果你的系统有它。

In zsh, you could just use .* safely, but in bash you'll have to use ls -d .*

在zsh中,你可以安全地使用。*但是在bash中你必须使用ls -d。*

#2


You can't use mv to rename multiple files like that. What you want is mmv (get it here).

您不能使用mv重命名这样的多个文件。你想要的是mmv(在这里得到)。

mmv .\* \#1

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.

你必须逃避星号以防止bash扩展它。使用-n标志进行测试运行,以确保将要发生的是您想要的。

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.

您也可以在shell脚本中执行此操作,但我更喜欢mmv,因为-n标志显示它将执行的操作。你必须改变你的脚本而不是mv,这似乎比删除-n标志更危险(特别是当你变得更复杂时)。

#3


The tricky part about this is selecting dotfiles without selecting "." and "..".

关于这个的棘手部分是选择dotfiles而不选择“。”和“......”。

  • 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。?? *有时用于此,因为它强制文件名长度为三个或更多字符。但是,忽略一个短名称的点文件,例如“.x”存在风险

  • ls -d .* prevents directories from being expanded, but it doesn't filter out "." or ".."
  • ls -d。*会阻止扩展目录,但不会过滤掉“。”要么 ”..”

  • The find command could be used, as in find . -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.
  • 可以使用find命令,如find中所示。 -maxdepth 1-type f -name'。*'。 maxdepth将其限制为当前目录而不是子目录。 -type f将其限制为文件,消除了诸如“。”之类的目录。和“......”。然后,也许你想将.ssh目录重命名为ssh。

Here's an alternative that selects dotfiles while avoiding "." and "..".

这是一个选择dotfiles同时避免“。”的替代方案。和“......”。

ls -A | sed -n 's/^\.\(.*\)/mv ".\1" "\1"/p' | bash

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.

-A列出了所有文件和dotfiles,但却删除了“。”和“..”对我们来说。然后sed命令只选择那些带有“。”的行。作为第一个字符,打印出适当的“mv”命令,如果你有一个带有空格的奇怪的dotfilename,则用引号括起来。

Run it without the "| bash" first, to see what mv commands are generated.

首先运行它而不使用“| bash”,以查看生成的mv命令。

#4


i don't know what type of system you're on, but it looks unix like, i would do

我不知道你在使用什么类型的系统,但它看起来像unix,我愿意

ls -1 .?* | cut -b1- | xargs -i{} mv .{} {}

ls -1。?* |切-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

这列出了一切以。开头,但不是。或..,然后关闭第一列,然后将该列表管道移动命令

#5


In Linux, there is usually a rename utility available (a perl script, if I am not mistaken):

在Linux中,通常有一个重命名实用程序(perl脚本,如果我没有记错的话):

rename 's/^.//' .*

It is available on a Mac. You can install it by following tips at here.

它可以在Mac上使用。您可以按照此处的提示进行安装。

#6


Even simpler:


for x in .*; do mv $x ${x/./}; done