bash 如何使用前缀/后缀重命名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/208181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to rename with prefix/suffix?
提问by Peter Boughton
How do I do mv original.filename new.original.filename
without retyping the original filename?
如何mv original.filename new.original.filename
不重新输入原始文件名?
I would imagine being able to do something like mv -p=new. original.filename
or perhaps mv original.filename new.~
or whatever - but I can't see anything like this after looking at man mv
/ info mv
pages.
我会想象能够做类似的事情,mv -p=new. original.filename
或者可能mv original.filename new.~
或者其他什么 - 但在查看man mv
/info mv
页面后我看不到这样的东西。
Of course, I could write a shell script to do this, but isn't there an existing command/flag for it?
当然,我可以编写一个 shell 脚本来执行此操作,但是没有现有的命令/标志吗?
回答by Simon Lehmann
You could use the rename(1)
command:
您可以使用以下rename(1)
命令:
rename 's/(.*)$/new./' original.filename
Edit:If rename
isn't available and you have to rename more than one file, shell scripting can really be short and simple for this. For example, to rename all *.jpg
to prefix_*.jpg
in the current directory:
编辑:如果rename
不可用并且您必须重命名多个文件,shell 脚本可能真的很短而且很简单。例如,所有重命名*.jpg
到prefix_*.jpg
当前目录:
for filename in *.jpg; do mv "$filename" "prefix_$filename"; done;
回答by Dave Webb
In Bash and zsh you can do this with Brace Expansion. This simply expands a list of items in braces. For example:
在 Bash 和 zsh 中,您可以使用Brace Expansion做到这一点。这只是扩展大括号中的项目列表。例如:
# echo {vanilla,chocolate,strawberry}-ice-cream
vanilla-ice-cream chocolate-ice-cream strawberry-ice-cream
So you can do your rename as follows:
因此,您可以按如下方式进行重命名:
mv {,new.}original.filename
as this expands to:
随着这扩展到:
mv original.filename new.original.filename
回答by A Muguro
You can achieve a unix compatible multiple file rename (using wildcards) by creating a for loop:
您可以通过创建 for 循环来实现兼容 Unix 的多文件重命名(使用通配符):
for file in *; do
mv $file new.${file%%}
done
回答by Jonathan Leffler
I've seen people mention a rename
command, but it is not routinely available on Unix systems (as opposed to Linux systems, say, or Cygwin - on both of which, rename is an executable rather than a script). That version of rename
has a fairly limited functionality:
我见过有人提到过一个rename
命令,但它在 Unix 系统上通常不可用(与 Linux 系统或 Cygwin 相对——在这两个系统上,重命名是可执行文件而不是脚本)。该版本的rename
功能相当有限:
rename from to file ...
It replaces the frompart of the file names with the to, and the example given in the man page is:
它将文件名的from部分替换为to,手册页中给出的示例是:
rename foo foo0 foo? foo??
This renames foo1 to foo01, and foo10 to foo010, etc.
这会将 foo1 重命名为 foo01,将 foo10 重命名为 foo010,等等。
I use a Perl script called rename
, which I originally dug out from the first edition Camel book, circa 1992, and then extended, to rename files.
我使用了一个名为 的 Perl 脚本rename
,它最初是从大约 1992 年的第一版 Camel 书中挖掘出来的,然后进行了扩展,以重命名文件。
#!/bin/perl -w
#
# @(#)$Id: rename.pl,v 1.7 2008/02/16 07:53:08 jleffler Exp $
#
# Rename files using a Perl substitute or transliterate command
use strict;
use Getopt::Std;
my(%opts);
my($usage) = "Usage: rename 's/^/new./' original.filename
[-fnxV] perlexpr [filenames]\n";
my($force) = 0;
my($noexc) = 0;
my($trace) = 0;
die $usage unless getopts('fnxV', \%opts);
if ($opts{V})
{
printf "%s\n", q'RENAME Version $Revision: 1.7 $ ($Date: 2008/02/16 07:53:08 $)';
exit 0;
}
$force = 1 if ($opts{f});
$noexc = 1 if ($opts{n});
$trace = 1 if ($opts{x});
my($op) = shift;
die $usage unless defined $op;
if (!@ARGV) {
@ARGV = <STDIN>;
chop(@ARGV);
}
for (@ARGV)
{
if (-e $_ || -l $_)
{
my($was) = $_;
eval $op;
die $@ if $@;
next if ($was eq $_);
if ($force == 0 && -f $_)
{
print STDERR "rename failed: $was - $_ exists\n";
}
else
{
print "+ $was --> $_\n" if $trace;
print STDERR "rename failed: $was - $!\n"
unless ($noexc || rename($was, $_));
}
}
else
{
print STDERR "$_ - $!\n";
}
}
This allows you to write any Perl substitute or transliterate command to map file names. In the specific example requested, you'd use:
这允许您编写任何 Perl 替代或音译命令来映射文件名。在请求的特定示例中,您将使用:
ls | xargs -I fileName mv fileName fileName.suffix
回答by Brajan Elektro
The easiest way to bulk rename files in directory is:
批量重命名目录中文件的最简单方法是:
f=`ls *canctn[0-9]*` ; mv $f CNLC.$f
f=`ls *acustb[0-9]*` ; mv $f CATB.$f
f=`ls *accusgtb[0-9]*` ; mv $f CATB.$f
f=`ls *acus[0-9]*` ; mv $f CAUS.$f
回答by workmad3
If it's open to a modification, you could use a suffix instead of a prefix. Then you could use tab-completion to get the original filename and add the suffix.
如果它可以修改,您可以使用后缀而不是前缀。然后您可以使用制表符补全来获取原始文件名并添加后缀。
Otherwise, no this isn't something that is supported by the mv command. A simple shell script could cope though.
否则,这不是 mv 命令支持的。不过,一个简单的 shell 脚本就可以应付。
回答by aprodan
In my case I have a group of files which needs to be renamed before I can work with them. Each file has its own role in group and has its own pattern.
就我而言,我有一组文件需要重命名才能使用它们。每个文件在组中都有自己的角色,并有自己的模式。
As result I have a list of rename commands like this:
结果我有一个像这样的重命名命令列表:
f=MyFileName; mv $f {pref1,pref2}$f{suf1,suf2}
Try this also :
也试试这个:
pref1.MyFileName.suf1
...
pref2.MyFileName.suf2
This will produce all combinations with prefixes and suffixes:
这将产生带有前缀和后缀的所有组合:
#!/bin/bash
unset masks
typeset -A masks
masks[ip[0-9]]=ip
masks[iaf_usg[0-9]]=ip_usg
masks[ipusg[0-9]]=ip_usg
...
for fileMask in ${!masks[*]};
do
registryEntry="${masks[$fileMask]}";
fileName=*${fileMask}*
[ -e ${fileName} ] && mv ${fileName} ${registryEntry}.${fileName}
done
Another way to solve same problem is to create mapping array and add corespondent prefix for each file type as shown below:
解决相同问题的另一种方法是创建映射数组并为每种文件类型添加对应前缀,如下所示:
#!/bin/bash
# USAGE: cd FILESDIRECTORY; RENAMERFILEPATH/MultipleFileRenamer.sh FILENAMEPREFIX INITNUMBER
# USAGE EXAMPLE: cd PHOTOS; /home/Desktop/MultipleFileRenamer.sh 2016_
# VERSION: 2016.03.05.
# COPYRIGHT: Harkály Gerg? | mangoRDI (https://wwww.mangordi.com/)
# check isset INITNUMBER argument, if not, set 1 | INITNUMBER is the first number after renaming
if [ -z "" ]
then i=1;
else
i=;
fi
# counts the files to set leading zeros before number | max 1000 files
count=$(ls -l * | wc -l)
if [ $count -lt 10 ]
then zeros=1;
else
if [ $count -lt 100 ]
then zeros=2;
else
zeros=3
fi
fi
# rename script
for file in *
do
mv $file _$(printf %0"$zeros"d.%s ${i%.*} ${file##*.})
let i="$i+1"
done
回答by Harkály Gerg?
Bulk rename files bash script
批量重命名文件bash脚本
##代码##