Linux 重命名文件和目录(添加前缀)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4787413/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:12:54  来源:igfitidea点击:

Rename Files and Directories (Add Prefix)

linuxperlshell

提问by Rafael

I would like to add prefix on all folders and directories.

我想在所有文件夹和目录上添加前缀。

Example:

例子:

I have

我有

Hi.jpg
1.txt
folder/
this.file_is.here.png
another_folder.ok/

I would like to add prefix "PRE_"

我想添加前缀“PRE_”

PRE_Hi.jpg
PRE_1.txt
PRE_folder/
PRE_this.file_is.here.png
PRE_another_folder.ok/

Regards,

问候,

回答by CanSpice

Thanks to Peter van der Heijden, here's one that'll work for filenames with spaces in them:

感谢Peter van der Heijden,这里有一个适用于其中包含空格的文件名:

for f in * ; do mv -- "$f" "PRE_$f" ; done

("--" is needed to succeed with files that begin with dashes, whose names would otherwise be interpreted as switches for the mv command)

(需要“--”才能成功处理以破折号开头的文件,否则其名称将被解释为 mv 命令的开关)

回答by tchrist

Use the renamescriptthis way:

这种方式使用重命名脚本

$ rename 's/^/PRE_/' *

There are no problems with metacharacters or whitespace in filenames.

文件名中的元字符或空格没有问题。

回答by kurumi

If you have Ruby(1.9+)

如果你有 Ruby(1.9+)

ruby -e 'Dir["*"].each{|x| File.rename(x,"PRE_"+x) }'

回答by tadmc

with Perl:

使用 Perl:

perl -e 'rename $_, "PRE_$_" for <*>'

回答by Joel Berger

Here is a simple script that you can use. I like using the non-standard module File::chdirto handle managing cdoperations, so to use this script as-is you will need to install it (sudo cpan File::chdir).

这是您可以使用的简单脚本。我喜欢使用非标准模块File::chdir来处理管理cd操作,因此要按原样使用此脚本,您需要安装它 ( sudo cpan File::chdir)。

#!/usr/bin/perl

use strict;
use warnings;

use File::Copy;
use File::chdir; # allows cd-ing by use of $CWD, much easier but needs CPAN module

die "Usage: 
ls | xargs -I {} mv {} PRE_{}

ls | xargs -I {} mv {} {}_SUF
dir prefix" unless (@ARGV >= 2); my ($dir, $pre) = @ARGV; opendir(my $dir_handle, $dir) or die "Cannot open directory $dir"; my @files = readdir($dir_handle); close($dir_handle); $CWD = $dir; # cd to the directory, needs File::chdir foreach my $file (@files) { next if ($file =~ /^\.+$/); # avoid folders . and .. next if (
for f in $(find . -name '*.html'); do mv "$f" "$(dirname "$f")/prefix_$(basename "$f")"; done
=~ /$file/); # avoid moving this script if it is in the directory move($file, $pre . $file) or warn "Cannot rename file $file: $!"; }

回答by Zheng Qsin

For adding prefix or suffixfor files(directories), you could use the simple and powerful way by xargs:

要为文件(目录)添加前缀或后缀,您可以使用简单而强大的方法xargs

find * -maxdepth 0 -exec mv {} PRE_{} \;

It is using the paramerter-replacing option of xargs: -I. And you can get more detail from the man page.

它使用 xargs 的参数替换选项:-I。您可以从手册页获得更多详细信息。

回答by conradkleinespel

On my system, I don't have the renamecommand. Here is a simple one liner. It finds all the HTML files recursively and adds prefix_in front of their names:

在我的系统上,我没有rename命令。这是一个简单的单衬。它递归地查找所有 HTML 文件并prefix_在它们的名称前添加:

rename '' <prefix> *

回答by Cyclonecode

This could be done running a simple findcommand:

这可以通过运行一个简单的find命令来完成:

for f in $(find /directory/ -type f); do 
  mv -v $f ${f%/*}/$(date +%Y%m%d)_Prefix_${f##*/}
done

The above command will prefix all files and folders in the current directory with PRE_.

上面的命令将在当前目录中的所有文件和文件夹前加上PRE_.

回答by koyae

To add a prefix to all files and folders in the current directory using util-linux's rename(as opposed to prename, the perl variant from Debian and certain other systems), you can do:

要使用 util-linux 为当前目录中的所有文件和文件夹添加前缀renameprename与 Debian 和某些其他系统的 perl 变体相反),您可以执行以下操作:

##代码##

This finds the first occurrence of the empty string (which is found immediately) and then replaces that occurrence with your prefix, then glues on the rest of the file name to the end of that. Done.

这将找到空字符串的第一次出现(立即找到),然后用您的前缀替换该出现,然后将文件名的其余部分粘在其末尾。完毕。

For suffixes, you need to use the perl versionor use find.

对于后缀,您需要使用 perl 版本使用 find

回答by SteinAir

This will prefix your files in their directory.

这将为您的文件在其目录中添加前缀。

The ${f%/*}is the path till the last slash /-> the directory

${f%/*}是路径,直到最后一个斜线/- >目录

The ${f##*/}is the text without anything before last slash /-> filename without the path

${f##*/}是无最后一个斜线之前的任何文本/- >文件名不带路径

So that's how it goes:

事情是这样的:

##代码##