简体   繁体   English

批量删除Bash中文件名中的前导破折号

[英]Batch removing leading dash in file names in Bash

$ ls -l
-rw-r--r--@ 1 ywang  Users  6156 Aug 16 14:38 -STEST.20140728.151116.pgp
-rw-r--r--@ 1 ywang  Users  2756 Aug 16 14:38 -STEST.20140728.152042.pgp
-rw-r--r--@ 1 ywang  Users  3424 Aug 16 14:38 -STEST.20140729.141735.pgp
-rw-r--r--@ 1 ywang  Users  2439 Aug 16 14:38 -STEST.20140729.142515.pgp
-rw-r--r--@ 1 ywang  Users  2672 Aug 16 14:38 -STEST.20140730.125115.pgp
-rw-r--r--@ 1 ywang  Users  2391 Aug 16 14:38 -STEST.20140730.125556.pgp

Hi, I've tried multiple ways, eg looping through the file and do mv one by one. 嗨,我已经尝试了多种方法,例如遍历文件并一个接一个地执行mv However, I wasn't successful as the caveat was that mv interprets the leading dash as a parameter to itself and backslash escaping doesn't seem to work when combining with the wildcard * . 但是,我没有成功,因为警告是mv将前划线作为其自身的参数,并且与通配符*结合使用时,反斜杠转义似乎不起作用。

Any ideas how it can be done in a oneliner in Bash? 有什么想法可以在Bash的oneliner中完成吗? Thanks! 谢谢!

You can use: 您可以使用:

for i in ./-*; do mv "$i" "${i#*-}"; done

It is important to use ./-* for globbing so that shell doesn't interpret - as command option. 使用./-*进行./-*很重要,这样外壳程序就不会将-解释为命令选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM