LinuxGem
此处为老的 LinuxGem,新版 LinuxGem 请移步 www.linuxgem.org

在 Bash 下批量更改文件扩展名

galeki posted @ 2007年10月03日 08:24 in Bash 和它的朋友们 with tags bash 扩展名 批量 , 6164 阅读

如果我想把当前目录下的所有扩展名为 cpp 的文件,改成扩展名为 c 的文件,要怎么办? 用 shell 编程可以很容易的解决这个问题:

  1. #!/bin/bash
  2. for old in *.cpp
  3. do
  4.     mv $old `basename $old .cpp`.c
  5. done

特别解释一下第4行的 basename 这个命令,basename 返回文件完整路径中的文件名部分,例如: 

# basename /home/galeki/html/index.html
index.html

你也可以在参数中给出文件的扩展名,这样 basename 只会返回文件名除去扩展名的部分:

# basename /home/galeki/html/index.html  .html
index

 也可以把上面的程序修改一下,让它可以根据参数处理不同的扩展名:

  1. #!/bin/bash
  2. for old in *.$1
  3. do
  4.     mv $old `basename $old .$1`.$2
  5. done

 把上面这段代码存成 rn ( rename 的缩写 ),如果你想把当前目录下的所有文件的扩展名从 jpg 改成 png ,那么,只要执行: rn  jpg  png ,即可。

Turkish citizenship 说:
2021年6月23日 17:39

i am until the call off of time greatly surprised by means of the amount of recommend understandable in financial credit score to this situation. What you presented became ably researched and adroitly worded in an effort to collect your stand within the location of this across to every and the entire single one one your readers.

Harry 说:
2021年8月25日 02:18

I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me https://linksubmit.org/guest-posting-rules/

SEO 说:
2022年4月10日 18:49

Great survey, I'm sure you're getting a great response. 인천출장안마

SEO 说:
2022年4月16日 20:33

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. prototype manufacturing

ali 说:
2023年8月28日 23:47

thank you so much thank you so much. <a href="https://www.dw.com">school</a>

school 说:
2023年8月28日 23:49

thank you so much thank you so much.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter