我正在尝试使用mv命令做三件事,但不确定它是否可能?可能需要一个脚本.不知道怎么写.所有文件都在同一个文件夹中.
1)以v9.zip结尾的文件应该是.zip(已删除v9)
2)包含_的文件应该是 -
3)小写字母旁边的大写字母(或大写旁边的小写)旁边的文件应该有一个空格.所以MoveOverNow将会立即移动并且ruNaway将会消失[AZ] [az]或[az] [AZ]变为[AZ] [az]和[az] [AZ]
Ant*_*ook 20
rename大多数Debian/Ubuntu发行版提供了一个命令,由Robin Barker根据Larry Wall 1998年左右的原始代码编写(!).
以下是文档的摘录:
Run Code Online (Sandbox Code Playgroud)"rename" renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input. For example, to rename all files matching "*.bak" to strip the extension, you might say rename 's/\.bak$//' *.bak To translate uppercase names to lower, you'd use rename 'y/A-Z/a-z/' *
它使用perl,因此你可以使用perl表达式来匹配模式,事实上我相信它的工作方式与tchrist的脚本非常相似.
另一个非常有用的批量文件重命名工具是Oskar Liljeblad的renameutils集合.源代码由自由软件基金会托管.此外,许多发行版(尤其是基于Debian/Ubuntu的发行版)都有一个renameutils包含这些工具的软件包.
在其中一个发行版中,您可以使用以下命令安装它:
$ sudo apt-get install renameutils
Run Code Online (Sandbox Code Playgroud)
然后重命名文件只需运行以下命令:
$ qmv
Run Code Online (Sandbox Code Playgroud)
它将弹出一个包含文件列表的文本编辑器,您可以使用编辑器的搜索和替换功能来操作它们.
我没有测试过这些,所以我放在echo命令的前面,这样你就可以在删除回声之前尝试它们来实现它们.
1)
for f in *v9.zip; do echo mv "${f}" "${f%v9.zip}.zip"; done
Run Code Online (Sandbox Code Playgroud)
2)
for f in *_*; do echo mv "${f}" "${f//_/-}"; done
Run Code Online (Sandbox Code Playgroud)
关于你的第三个问题,我确信它也可以做到,但可能比原始shell单行提供更复杂的方法,正如@tchrist所提到的那样.
我最喜欢的解决方案是我自己的重命名脚本.映射到您的问题的最简单的例子是:
% rename 's/_/-/g' *
% rename 's/(\p{Lower})(\p{Upper})/$1 $2/g' *
Run Code Online (Sandbox Code Playgroud)
虽然我真的讨厌我的文件名中的空格,特别是垂直空格:
% rename 's/\s//g' *
% rename 's/\v//g' *
Run Code Online (Sandbox Code Playgroud)
等等.它基于The Larry Wall的脚本,但扩展了选项,如:
usage: /home/tchrist/scripts/rename [-ifqI0vnml] [-F file] perlexpr [files]
-i ask about clobbering existent files
-f force clobbers without inquiring
-q quietly skip clobbers without inquiring
-I ask about all changes
-0 read null-terminated filenames
-v verbosely says what its doing
-V verbosely says what its doing but with newlines between old and new filenames
-n don't really do it
-m to always rename
-l to always symlink
-F path read filelist to change from magic path(s)
Run Code Online (Sandbox Code Playgroud)
如您所见,它不仅可以更改文件的名称,还可以更改符号链接指向使用相同模式的位置.您不必使用s///模式,尽管通常会使用模式.
该目录中的其他工具主要用于Unicode工作,其中有一些非常有用的工具.
| 归档时间: |
|
| 查看次数: |
25425 次 |
| 最近记录: |