根据模式在Linux中重命名许多文件 [英] Renaming lots of files in Linux according to a pattern

查看:51
本文介绍了根据模式在Linux中重命名许多文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mv命令执行三件事,但不确定是否可行?可能需要一个脚本.不知道怎么写.所有文件都在同一文件夹中.

1)以v9.zip结尾的文件应仅为.zip(已删除v9)

2)包含_的文件应为-

3) 小写字母旁边的大写字母(或大写旁边的小写字母)的文件之间应该有一个空格.因此MoveOverNow将变为Now Move Now,而ruNaway将变为ruN[A-Z] [a-z]或[a-z] [A-Z]变为[A-Z] [a-z]和[a-z] [A-Z]

解决方案

我最喜欢的解决方案是我自己的重命名脚本.映射到您的问题的最简单示例是:

 %重命名为's/_/-/g'*%重命名's/(\ p {Lower})(\ p {Upper})/$ 1 $ 2/g'* 

尽管我真的很讨厌文件名中的空格,尤其是垂直空格:

 %重命名's/\ s//g'*% 重命名 's/\v//g' * 

等.它基于The Larry Wall的脚本,但扩展了选项,如下所示:

 用法:/home/tchrist/scripts/rename [-ifqI0vnml] [-F文件] perlexpr [文件]-i询问是否破坏现有文件-f强制询问而不询问-q无需询问就悄悄地跳过clobbers-我询问所有变化-0 读取以空字符结尾的文件名-v详细说出它在做什么-V详细说明其功能,但在新旧文件名之间使用换行符-n真的不做-m始终重命名-l总是符号链接-F路径读取文件列表以从魔术路径更改 

如您所见,它不仅可以更改文件名,而且可以更改符号链接指向使用相同模式的位置.您不必使用 s///模式,尽管通常是这样.

该目录中的其他工具主要用于 Unicode 工作,其中有一些非常有用的工具.>

I'm trying to do three things with the mv command, but not sure it's possible? Probably need a script. not sure how to write it. All files are in same folder.

1) Files ending with v9.zip should just be .zip (the v9 removed)

2) Files containing _ should be -

3) Files with Uppercase letter next to a lowercase letter (or lowercase next to an Uppercase) should have a space between them. So MoveOverNow would be Move Over Now and ruNaway would be ruN away [A-Z][a-z] or [a-z][A-Z] becomes [A-Z] [a-z] and [a-z] [A-Z]

解决方案

My favorite solution is my own rename script. The simplest example that maps to your problems are these:

% rename 's/_/-/g' *
% rename 's/(\p{Lower})(\p{Upper})/$1 $2/g' *

Although I really hate whitespace in my filenames, especially vertical whitespace:

 % rename 's/\s//g' *
 % rename 's/\v//g' *

et cetera. It’s based on a script by The Larry Wall, but extended with options, as in:

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)

As you see, it can change not just the names of files, but where symbolic links are pointing to using the same pattern. You don’t have to use a s/// pattern, although often one does.

The other tools in that directory are mostly for Unicode work, of which there are some super-useful ones.

这篇关于根据模式在Linux中重命名许多文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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