linux批量重命名目录并从名称中删除#字符

发布于 2024-08-24 18:41:56 字数 263 浏览 5 评论 0原文

我有一个目录,其中有很多子目录,它们前面有一个 #:

#adhasdk
#ad18237

我想将它们全部重命名并删除 # 字符 我尝试这样做:

rename -n `s/#//g` *

但似乎没有成功。

-bash: s/#//g: No such file or directory

对此有任何想法。 谢谢

i have a directory with a lot of subdirectories with a # infront of them:

#adhasdk
#ad18237

I want to rename them all and remove the # caracter
I tried to do:

rename -n `s/#//g` *

but didn't seem to work.

-bash: s/#//g: No such file or directory

Any ideas on this.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

三生一梦 2024-08-31 18:41:57

只需使用

$ rename 's/^#//' *

use -n 来检查您认为会发生的事情是否真的发生了。
中使用了错误的引号(反引号)

 -bash: s/#//g: No such file or directory

在您的示例中,您可以了解到错误消息bash is attempts toexecute a command named s/#//g

。不,使用 g (全局)并且不锚定正则表达式,您将替换任何 #,而不仅仅是第一个位置的。

Just use

$ rename 's/^#//' *

use -n just to check that what you think it would happen really happens.
In you example you have the clue about the wrong quotes used (backticks) in the error message

 -bash: s/#//g: No such file or directory

bash is trying to execute a command named s/#//g.

No that using g (global) and not anchoring the regular expression you will replace any #, not just the one in the first position.

弄潮 2024-08-31 18:41:57

我不知道这是否只是您在此处键入时的拼写错误,但是“重命名”命令应该起作用,如果:

  1. 您省略“-n”并且
  2. 用常规单引号而不是反引号引用替换

内容“-n”告诉它不做任何事情。反引号是错误的(它们意味着一些东西,但不是你想要的)。

I don't know whether it's just a typo when you typed it here, but that "rename" command should work if:

  1. you leave off the "-n" and
  2. you quote the substitution with regular single-quotes and not back-quotes

The "-n" tells it to not really do anything. The back-quotes are just wrong (they mean something but not what you want here).

深白境迁sunset 2024-08-31 18:41:57

问题是您使用了反引号 (`)。您应该使用正常的引号:

rename -n 's/#//g' *

The problem is that you use backticks (`). You should use normal quotes:

rename -n 's/#//g' *
单身情人 2024-08-31 18:41:57
for DIR in \#*/
do
     echo mv "$DIR" "${DIR/#\#/}"
done
for DIR in \#*/
do
     echo mv "$DIR" "${DIR/#\#/}"
done
意犹 2024-08-31 18:41:57

我必须重命名给定文件夹内的所有文件夹。每个文件夹名称在圆括号内都有一些文本。以下命令删除了所有文件夹名称中的圆括号:

rename 's/(.+)//' *

I had to rename all folders inside a given folder. Each folder name had some text inside round braces. The following command removed the round braces from all folder names:

rename 's/(.+)//' *

烟织青萝梦 2024-08-31 18:41:57

某些发行版不支持 rename 中的正则表达式。您必须安装prename。更重要的是,有时您无法安装 prename,而必须安装 gprename 才能获得二进制 prename

如果您有“prename”,那么只需将反引号字符“`”更改为单引号,一切就应该可以工作。

所以解决方案应该是:

prename -n 's/#//g' *

或者

prename -n 'y/#//' *

Some distros doesn't support regexp in rename. You have to install prename. Even more, sometimes you can't install prename and you have to install gprename to have binary prename.

If you have 'prename' then just change backtick character " ` " to single quote and everything should work.

So the solution should be:

prename -n 's/#//g' *

or

prename -n 'y/#//' *
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文