bash linux批量重命名目录并从名称中删除#字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2417339/
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
linux batch rename directories and strip # character from name
提问by Gabriel Solomon
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
关于这个的任何想法。谢谢
回答by Diego Torres Milano
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
使用 -n 只是为了检查您认为会发生的事情是否真的发生了。在你的例子中,你有关于错误消息中使用的错误引号(反引号)的线索
-bash: s/#//g: No such file or directory
bash is trying to execute a command named s/#//g.
bash 正在尝试执行名为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.
不,使用g(全局)而不是锚定正则表达式,您将替换 any #,而不仅仅是第一个位置的那个。
回答by Pointy
I don't know whether it's just a typo when you typed it here, but that "rename" command should work if:
我不知道您在这里输入时是否只是打字错误,但是如果出现以下情况,“重命名”命令应该可以工作:
- you leave off the "-n" and
- you quote the substitution with regular single-quotes and not back-quotes
- 你离开了“-n”和
- 您用常规单引号而不是反引号引用替换
The "-n" tells it to not really do anything. The back-quotes are just wrong (they mean something but not what you want here).
“-n”告诉它不要真正做任何事情。反引号是错误的(它们的意思是什么,但不是你在这里想要的)。
回答by Johannes Weiss
The problem is that you use backticks (`). You should use normal quotes:
问题是您使用了反引号 (`)。您应该使用普通引号:
rename -n 's/#//g' *
回答by ghostdog74
for DIR in \#*/
do
echo mv "$DIR" "${DIR/#\#/}"
done
回答by Nadir Latif
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/(.+)//' *
重命名 's/(.+)//' *

