bash脚本来替换文件名中的空格 [英] Bash script to replace spaces in file names

查看:588
本文介绍了bash脚本来替换文件名中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能推荐一个安全的解决方案,以递归替换文件下划线和目录名从给定根目录开始的空间?例如,

Can anyone recommend a safe solution to recursively replace spaces with underscores in file and directory names starting from a given root directory? For example,


$ tree
.
|-- a dir
|   `-- file with spaces.txt
`-- b dir
    |-- another file with spaces.txt
    `-- yet another file with spaces.pdf

变为


$ tree
.
|-- a_dir
|   `-- file_with_spaces.txt
`-- b_dir
    |-- another_file_with_spaces.txt
    `-- yet_another_file_with_spaces.pdf

修改

感谢您的答案;他们似乎都工作。我挑了一个由丹尼斯为主要答案,因为在我看来,最简单的,即使它需要两个步骤。

Thanks for the answers; they all seem to work. I picked the one by Dennis as the main answer because it seems to me the simplest, even though it takes two steps.

推荐答案

使用改名(又名 preNAME ),这是一个Perl脚本,这可能是您的系统上了。做它在两个步骤:

Use rename (aka prename) which is a Perl script which may be on your system already. Do it in two steps:

find -name "* *" -type d | rename 's/ /_/g'    # do the directories first
find -name "* *" -type f | rename 's/ /_/g'

编辑:

根据于尔根的答案,并能够处理的文件和目录的多层纵身使用<$ C的修订版1.5 1998年12月18日16时16分31秒1元的版本$ C>的/ usr / bin中/重命名(Perl脚本):

Based on Jürgen's answer and able to handle multiple layers of files and directories in a single bound using the "Revision 1.5 1998/12/18 16:16:31 rmb1" version of /usr/bin/rename (a Perl script):

find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;

这篇关于bash脚本来替换文件名中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆