在Linux中根据[重命名]模式重命名许多文件

如何解决在Linux中根据[重命名]模式重命名许多文件

||                                                                                                                   关闭。这个问题是题外话。它当前不接受答案。                                                      

解决方法

        我最喜欢的解决方案是我自己的重命名脚本。映射到您的问题的最简单示例是:
% rename \'s/_/-/g\' *
% rename \'s/(\\p{Lower})(\\p{Upper})/$1 $2/g\' *
尽管我确实讨厌文件名中的空格,尤其是垂直空格:
 % rename \'s/\\s//g\' *
 % rename \'s/\\v//g\' *
等等。它基于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)
如您所见,它不仅可以更改文件名,而且可以更改符号链接指向使用相同模式的位置。您不必使用
s///
模式,尽管通常会这样做。 该目录中的其他工具主要用于Unicode工作,其中有些超级有用。     ,        大多数基于Debian / Ubuntu的发行版提供了一个
rename
命令,该命令由Robin Barker根据Larry Wall在1998年左右的原始代码编写。 这是文档摘录:
  \"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的namenameutils集合。源代码由自由软件基金会托管。另外,许多发行版(尤其是基于Debian / Ubuntu的发行版)都带有这些工具的
renameutils
软件包。 在这些发行版之一中,您可以使用以下软件进行安装:
$ sudo apt-get install renameutils
然后要重命名文件,只需运行以下命令:
$ qmv
它将弹出一个带有文件列表的文本编辑器,您可以使用编辑器的搜索和替换功能来操作它们。     ,        我还没有测试它们,所以我在命令的前面放了“ 9”,这样您就可以在删除回显之前先尝试一下,然后再运行它们。 1)
for f in *v9.zip; do echo mv \"${f}\" \"${f%v9.zip}.zip\"; done
2)
for f in *_*; do echo mv \"${f}\" \"${f//_/-}\"; done
至于您的第三个问题,我敢肯定它也可以解决,但也许像@tchrist所提到的那样,比原始外壳单线更复杂的方法会有所帮助。     ,        以上答案适用于Debian,Ubuntu等 对于RHEL和co:将from_pattern重命名为_pattern文件     ,        我认为链接已断开,我无法在Webarchive中找到tchrist帖子中重命名脚本的页面,因此这是Perl中的另一个页面。
#!/usr/bin/perl
# -w switch is off bc HERE docs cause erroneous messages to be displayed under
# Cygwin
#From the Perl Cookbook,Ch. 9.9
# rename - Larry\'s filename fixer
$help = <<EOF;
Usage: rename expr [files]

This script\'s first argument is Perl code that alters the filename 
(stored in \\$_ ) to reflect how you want the file renamed. It can do 
this because it uses an eval to do the hard work. It also skips rename
calls when the filename is untouched. This lets you simply use 
wildcards like rename EXPR * instead of making long lists of filenames.

Here are five examples of calling the rename program from your shell:

% rename \'s/\\.orig$//\'  *.orig
% rename \'tr/A-Z/a-z/ unless /^Make/\'  *
% rename \'$_ .= \".bad\"\'  *.f
% rename \'print \"$_: \"; s/foo/bar/ if <STDIN> =~ /^y/i\'  *
% find /tmp -name \'*~\' -print | rename \'s/^(.+)~$/.#$1/\'

The first shell command removes a trailing \".orig\" from each filename.

The second converts uppercase to lowercase. Because a translation is
used rather than the lc function,this conversion won\'t be locale-
aware. To fix that,you\'d have to write:

% rename \'use locale; $_ = lc($_) unless /^Make/\' *

The third appends \".bad\" to each Fortran file ending in \".f\",something
a lot of us have wanted to do for a long time.

The fourth prompts the user for the change. Each file\'s name is printed
to standard output and a response is read from standard input. If the
user types something starting with a \"y\" or \"Y\",any \"foo\" in the 
filename is changed to \"bar\".

The fifth uses find to locate files in /tmp that end with a tilde. It 
renames these so that instead of ending with a tilde,they start with
a dot and a pound sign. In effect,this switches between two common 
conventions for backup files
EOF

$op = shift or die $help;
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-