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,
问候,
10 个解决方案
#1
145
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
#2
71
Use the rename script this way:
这样使用重命名脚本:
$ rename 's/^/PRE_/' *
There are no problems with metacharacters or whitespace in filenames.
元字符或文件名中的空格没有问题。
#3
47
For adding prefix or suffix for files(directories), you could use the simple and powerful way by xargs:
为文件(目录)添加前缀或后缀,可以使用xargs简单而强大的方式:
ls | xargs -I {} mv {} PRE_{}
ls | xargs -I {} mv {} {}_SUF
It is using the paramerter-replacing option of xargs: -I. And you can get more detail from the man page.
它正在使用xargs的paramerter替换选项:-I。你可以从手册页获得更多的细节。
#4
19
This could be done running a simple find
command:
这可以通过运行一个简单的find命令来完成:
find * -maxdepth 0 ! -path . -exec mv {} PRE_{} \;
The above command will prefix all files and folders in the current directory with PRE_
.
上面的命令将前缀为当前目录中的所有文件和文件夹。
#5
8
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的rename(与Debian和其他某些系统的perl变体prename不同)向当前目录中的所有文件和文件夹添加前缀,您可以这样做:
rename '' <prefix> *
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 version or use find.
对于后缀,需要使用perl版本或find。
#6
7
with Perl:
用Perl:
perl -e 'rename $_, "PRE_$_" for <*>'
#7
6
If you have Ruby(1.9+)
如果你有Ruby(1.9 +)
ruby -e 'Dir["*"].each{|x| File.rename(x,"PRE_"+x) }'
#8
2
Here is a simple script that you can use. I like using the non-standard module File::chdir
to handle managing cd
operations, so to use this script as-is you will need to install it (sudo cpan File::chdir
).
这里有一个您可以使用的简单脚本。我喜欢使用非标准的模块文件::chdir来处理cd操作,因此要按原样使用此脚本,您需要安装它(sudo cpan文件:::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: $0 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 ($0 =~ /$file/); # avoid moving this script if it is in the directory
move($file, $pre . $file) or warn "Cannot rename file $file: $!";
}
#9
1
On my system, I don't have the rename
command. Here is a simple one liner. It finds all the HTML files recursively and adds prefix_
in front of their names:
在我的系统上,我没有重命名命令。这是一个简单的内衬。它递归地查找所有HTML文件,并在它们的名字前面添加前缀:
for f in $(find . -name '*.html'); do mv "$f" "$(dirname "$f")/prefix_$(basename "$f")"; done
#10
0
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:
就是这样:
for f in $(find /directory/ -type f); do
mv -v $f ${f%/*}/$(date +%Y%m%d)_Prefix_${f##*/}
done
#1
145
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
#2
71
Use the rename script this way:
这样使用重命名脚本:
$ rename 's/^/PRE_/' *
There are no problems with metacharacters or whitespace in filenames.
元字符或文件名中的空格没有问题。
#3
47
For adding prefix or suffix for files(directories), you could use the simple and powerful way by xargs:
为文件(目录)添加前缀或后缀,可以使用xargs简单而强大的方式:
ls | xargs -I {} mv {} PRE_{}
ls | xargs -I {} mv {} {}_SUF
It is using the paramerter-replacing option of xargs: -I. And you can get more detail from the man page.
它正在使用xargs的paramerter替换选项:-I。你可以从手册页获得更多的细节。
#4
19
This could be done running a simple find
command:
这可以通过运行一个简单的find命令来完成:
find * -maxdepth 0 ! -path . -exec mv {} PRE_{} \;
The above command will prefix all files and folders in the current directory with PRE_
.
上面的命令将前缀为当前目录中的所有文件和文件夹。
#5
8
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的rename(与Debian和其他某些系统的perl变体prename不同)向当前目录中的所有文件和文件夹添加前缀,您可以这样做:
rename '' <prefix> *
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 version or use find.
对于后缀,需要使用perl版本或find。
#6
7
with Perl:
用Perl:
perl -e 'rename $_, "PRE_$_" for <*>'
#7
6
If you have Ruby(1.9+)
如果你有Ruby(1.9 +)
ruby -e 'Dir["*"].each{|x| File.rename(x,"PRE_"+x) }'
#8
2
Here is a simple script that you can use. I like using the non-standard module File::chdir
to handle managing cd
operations, so to use this script as-is you will need to install it (sudo cpan File::chdir
).
这里有一个您可以使用的简单脚本。我喜欢使用非标准的模块文件::chdir来处理cd操作,因此要按原样使用此脚本,您需要安装它(sudo cpan文件:::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: $0 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 ($0 =~ /$file/); # avoid moving this script if it is in the directory
move($file, $pre . $file) or warn "Cannot rename file $file: $!";
}
#9
1
On my system, I don't have the rename
command. Here is a simple one liner. It finds all the HTML files recursively and adds prefix_
in front of their names:
在我的系统上,我没有重命名命令。这是一个简单的内衬。它递归地查找所有HTML文件,并在它们的名字前面添加前缀:
for f in $(find . -name '*.html'); do mv "$f" "$(dirname "$f")/prefix_$(basename "$f")"; done
#10
0
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:
就是这样:
for f in $(find /directory/ -type f); do
mv -v $f ${f%/*}/$(date +%Y%m%d)_Prefix_${f##*/}
done