【问题标题】:Rename foldername to filename with bash使用 bash 将文件夹名重命名为文件名
【发布时间】:2017-12-12 00:59:46
【问题描述】:

我想使用文件夹名称重命名文件。

这个:

├── foo01
│   └── bar.png
│   └── bar.txt
├── foo02
│   └── bar.png
│   └── bar.txt
└── foo03
    └── bar.png
    └── bar.txt

应该是这样的:

├── foo01
│   └── foo01.png
│   └── foo01.txt
├── foo02
│   └── foo02.png
│   └── foo02.txt
└── foo03
    └── foo03.png
    └── foo03.txt

我找不到这样做的方法。有人可以帮忙吗?

【问题讨论】:

标签: linux bash rename


【解决方案1】:

使用 Perl 的独立重命名命令:

rename -n 's|([^/]*)/[^/]*\.(.*)$|$1/$1.$2|' */*

输出:

foo01/bar.png 重命名为 foo01/foo01.png foo01/bar.txt 重命名为 foo01/foo01.txt foo02/bar.png 重命名为 foo02/foo02.png foo02/bar.txt 重命名为 foo02/foo02.txt foo03/bar.png 重命名为 foo03/foo03.png foo03/bar.txt 重命名为 foo03/foo03.txt

如果一切正常,请删除选项-n

【讨论】:

  • 我使用s|regex|string| 将路径和文件名的元素转换为新字符串。语法与seds 命令非常相似。
  • 谢谢,这有帮助。我必须使用相同的语法重命名(而不是前名),一切正常。 200 个文件被重命名:)
猜你喜欢
  • 2023-03-22
  • 2012-03-25
  • 2020-08-29
  • 2016-01-25
  • 2016-09-23
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多