我想编写一个linux脚本,将所有这些文件的文件名(但扩展名不同)的所有文件移动或复制到一个新的文件名,同时保持不同的扩展名。 换一种说法:
如果我从一个目录列表开始:
file1.txt,file1.jpg,file1.doc,file12.txt,file12.jpg,file12.doc
我想编写一个脚本来更改所有文件名而不更改扩展名。 对于同样的例子,selectfile2作为新文件名的结果是:
在bash脚本中获取grandparent目录 – 重命名文件path中的目录
Bashrecursion地replace名字上的许多空格
file2.txt,file2.jpg and file2.doc,file12.doc
最好的祝愿,
乔治
命令行(或batch file)重命名文件夹中的所有文件(Windows)
首字母大写的文件名
将文件重命名为md5 sum + extension(BASH)
使用batch file进行条件重命名
注意:如果变量i有file1.doc ,则表达式${i##*.}在这种情况下提取扩展名即doc 。
一行解决方案:
for i in file1.*; do mv "$i" "file2.${i##*.}"; done
脚本:
#!/bin/sh # first argument - basename of files to be moved # second arguments - basename of destination files if [ $# -ne 2 ]; then echo "Two arguments required." exit; fi for i in $1.*; do if [ -e "$i" ]; then mv "$i" "$2.${i##*.}" echo "$i to $2.${i##*.}"; fi done
util-linux-ng软件包(大多数Linux版本都默认安装了)包含命令“rename”。 请参阅man rename使用说明。 使用它你的任务可以做到这一点
rename file1 file2 file1.*
要处理其基名包含特殊字符的输入文件,我将修改plesiv的脚本为以下内容:
if [ $# -ne 2 ]; then echo "Two arguments required." exit; fi for i in "$1".*; do if [ -e "$i" ]; then mv "$i" "$2.${i##*.}" echo "$i to $2.${i##*.}"; fi done
请注意$ 1附近的额外引号。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。