热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

重命名文件和目录(添加前缀)-RenameFilesandDirectories(AddPrefix)

Iwouldliketoaddprefixonallfoldersanddirectories.我想在所有文件夹和目录中添加前缀。Example:例子:Ihave我

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 ''  *

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

推荐阅读
  • 正则表达式及其范例
    为什么80%的码农都做不了架构师?一、前言部分控制台输入的字符串,编译成java字符串之后才送进内存,比如控制台打\, ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 本文介绍了使用C++Builder实现获取USB优盘序列号的方法,包括相关的代码和说明。通过该方法,可以获取指定盘符的USB优盘序列号,并将其存放在缓冲中。该方法可以在Windows系统中有效地获取USB优盘序列号,并且适用于C++Builder开发环境。 ... [详细]
  • angular.element使用方法及总结
    2019独角兽企业重金招聘Python工程师标准在线查询:http:each.sinaapp.comangularapielement.html使用方法 ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • C语言的经典程序有哪些
    本篇内容介绍了“C语言的经典程序有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何 ... [详细]
author-avatar
直拍之横打
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有