有没有办法重命名目录中的所有文件?

ste*_*lla 2 linux bash rename

Ubuntu 16.04

我对 linux 很陌生,并且在目录中有大量文件dir。这些文件有后缀_uploaded

有没有办法重命名所有这些文件并将它们设置为后缀_handled而不是_uploaded

hee*_*ayl 5

Ubuntu 有rename( prename),来自目录dir

rename -n 's/_uploaded$/_handled/g' -- *_uploaded
Run Code Online (Sandbox Code Playgroud)
  • -n 是为了 --dry-run

在获得要进行的潜在更改后,删除n以进行实际操作:

rename 's/_uploaded$/_handled/g' -- *_uploaded
Run Code Online (Sandbox Code Playgroud)

您还可以利用bash参数扩展,在for包含_uploaded末尾字符串的文件名的循环内,来自目录dir

for f in *_uploaded; do new=${f%_uploaded}; echo mv -- "$f" "${new}_handled"; done
Run Code Online (Sandbox Code Playgroud)

这将向您显示要进行的更改,删除echo以进行实际操作。