linux batch rename directories and strip # charact

2019-07-05 03:11发布

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

5条回答
趁早两清
2楼-- · 2019-07-05 03:26

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/(.+)//' *

查看更多
Melony?
3楼-- · 2019-07-05 03:32

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).

查看更多
该账号已被封号
4楼-- · 2019-07-05 03:41
for DIR in \#*/
do
     echo mv "$DIR" "${DIR/#\#/}"
done
查看更多
戒情不戒烟
5楼-- · 2019-07-05 03:53

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.

查看更多
迷人小祖宗
6楼-- · 2019-07-05 03:53

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

rename -n 's/#//g' *
查看更多
登录 后发表回答