Linux 在单个命令中使用前缀重命名文件夹中的所有文件

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

Rename all files in a folder with a prefix in a single command

linuxunixrename

提问by vasanthi

Rename all the files within a folder with prefix "Unix_"

使用前缀重命名文件夹中的所有文件 "Unix_"

Suppose a folder has two files

假设一个文件夹有两个文件

a.txt
b.pdf

then they both should be renamed from a single command to

那么它们都应该从一个命令重命名为

Unix_a.txt
Unix_b.pdf

回答by pavium

Try the renamecommand in the folder with the files:

尝试包含文件的文件夹中rename命令:

rename 's/^/Unix_/' *

The argument of rename (sed s command) indicates to replace the regex ^with Unix_. The caret (^) is a special character that means start of the line.

rename 的参数(sed s 命令)表示用Unix_替换正则表达式^。插入符号 (^) 是一个特殊字符,表示行的开头

回答by miku

If your filenames contain no whitepaceand you don't have any subdirectories, you can use a simple forloop:

如果您的文件名不包含空格并且您没有任何子目录,则可以使用一个简单的for循环:

$ for FILENAME in *; do mv $FILENAME Unix_$FILENAME; done 

Otherwise use the convenient renamecommand (which is a perl script) - although it might not be available out of the box on every Unix (e.g. OS X doesn't come with rename).

否则,请使用方便的rename命令(这是一个 perl 脚本)——尽管它可能无法在每个 Unix 上开箱即用(例如,OS X 没有附带rename)。

A short overview at debian-administration.org:

debian-administration.org 上的简短概述:

If your filenames contain whitespace it's easier to use find, on Linux the following should work:

如果您的文件名包含空格更容易使用find,在 Linux 上应该可以使用以下方法:

$ find . -type f -name '*' -printf "echo mv '%h/%f' '%h/Unix_%f\n'" | sh

On BSD systems, there is no -printfoption, unfortunately. But GNU findutilsshould be installable (on e.g. Mac OS X with brew install findutils).

-printf不幸的是,在 BSD 系统上,没有选择。但是GNU findutils应该是可安装的(例如在带有 的 Mac OS X 上brew install findutils)。

$ gfind . -type f -name '*' -printf "mv \"%h/%f\" \"%h/Unix_%f\"\n" | sh

回答by Zheng Qsin

I think this is just what you'er looking for:

我认为这正是您要寻找的:

ls | xargs -I {} mv {} Unix_{}

Yes, it is simple yet elegant and powerful, and also one-liner. You can get more detailed intro from me on the page:Rename Files and Directories (Add Prefix)

是的,它简单而优雅和强大,也是单线的。您可以在页面上从我这里获得更详细的介绍:重命名文件和目录(添加前缀)

回答by user645527

Also works for items with spaces and ignores directories

也适用于带有空格的项目并忽略目录

for f in *; do [[ -f "$f" ]] && mv "$f" "unix_$f"; done

回答by Tejas C

I recently faced this same situation and found an easier inbuilt solution. I am sharing it here so that it might help other people looking for solution.

我最近遇到了同样的情况,并找到了一个更简单的内置解决方案。我在这里分享它,以便它可以帮助其他人寻找解决方案。

With OS X Yosemite, Apple has integrated the batch renaming capabilities directly into Finder. Details information is available here. I have copied the steps below as well,

在 OS X Yosemite 中,Apple 已将批量重命名功能直接集成到 Finder 中。此处提供详细信息。我也复制了下面的步骤,

Rename multiple items

重命名多个项目

  1. Select the items, then Control-click one of them.

  2. In the shortcut menu, select Rename Items.

  3. In the pop-up menu below Rename Folder Items, choose to replace text in the names, add text to the names, or change the name format.

    • Replace text: Enter the text you want to remove in the Find field, then enter the text you want to add in the “Replace with” field.

    • Add text: Enter the text to you want to add in the field, then choose to add the text before or after the current name.

    • Format: Choose a name format for the files, then choose to put the index, counter, or date before or after the name. Enter a name in the Custom Format field, then enter the number you want to start with.

  4. Click Rename.

  1. 选择项目,然后按住 Control 键点按其中之一。

  2. 在快捷菜单中,选择重命名项目。

  3. 在“重命名文件夹项目”下方的弹出式菜单中,选取替换名称中的文本、向名称添加文本或更改名称格式。

    • 替换文本:在“查找”栏中输入要删除的文本,然后在“替换为”栏中输入要添加的文本。

    • 添加文本:在字段中输入要添加的文本,然后选择在当前名称之前或之后添加文本。

    • 格式:选择文件的名称格式,然后选择在名称之前或之后放置索引、计数器或日期。在“自定义格式”字段中输入名称,然后输入要开始的数字。

  4. 单击重命名。

If you have a common pattern in your files than you can use Replace text otherwise Add text would also do the job.

如果您的文件中有一个共同的模式,那么您可以使用替换文本,否则添加文本也可以完成这项工作。

回答by Jahid

With rnm(you will need to install it):

使用rnm(您需要安装它):

rnm -ns 'Unix_/fn/' *

Or

或者

rnm -rs '/^/Unix_/' *

P.S : I am the author of this tool.

PS:我是这个工具的作者。

回答by NirajW

You can just use -iinstead of -I {}

你可以使用-i代替-I {}

ls | xargs -i mv {} unix_{}

This also works perfectly.

这也很完美。

  • ls- lists all the files in the directory
  • xargs- accepts all files line by line due to the -ioption
  • {}is the placeholder for all files, necessary if xargsgets more than two arguments as input
  • ls- 列出目录中的所有文件
  • xargs- 由于该-i选项,一行一行地接受所有文件
  • {}是所有文件的占位符,如果xargs有两个以上的参数作为输入是必要的

Using awk:

使用 awk:

ls -lrt | grep '^-' | awk '{print "mv "" unix_"""}' | sh

回答by Khalil Gharbaoui

Situation:

情况:

We have certificate.keycertificate.crtinside /user/ssl/

我们certificate.keycertificate.crt里面有/user/ssl/

We want to rename anything that starts with certificateto certificate_OLD

我们想重命名任何以certificateto开头的东西certificate_OLD

We are now located inside /user

我们现在位于里面 /user

First, you do a dry run with -n:

首先,您使用以下命令进行试运行-n

rename -n "s/certificate/certificate_old/" ./ssl/*

rename -n "s/certificate/certificate_old/" ./ssl/*

Which returns:

返回:

rename(./ssl/certificate.crt, ./ssl/certificate_OLD.crt) rename(./ssl/certificate.key, ./ssl/certificate_OLD.key)

rename(./ssl/certificate.crt, ./ssl/certificate_OLD.crt) rename(./ssl/certificate.key, ./ssl/certificate_OLD.key)

Your files will be unchanged this is just a test run.

您的文件将保持不变,这只是一个测试运行。

Solution:

解决方案:

When your happy with the result of the test run it for real:

当您对测试结果感到满意时,请实际运行它:

rename "s/certificate/certificate_OLD/" ./ssl/*

rename "s/certificate/certificate_OLD/" ./ssl/*

What it means:

这是什么意思:

`rename "s/ SOMETHING / SOMETING_ELSE " PATH/FILES

`重命名“s/ SOMETING / SOMETING_ELSE”路径/文件

Tip:

提示:

If you are already on the path run it like this:

如果您已经在路径上,请像这样运行它:

rename "s/certificate/certificate_OLD/" *

rename "s/certificate/certificate_OLD/" *

Or if you want to do this in any sub-directory starting with ssdo:

或者,如果您想在以 do 开头的任何子目录中执行此ss操作:

rename -n "s/certificat/certificate_old/" ./ss*/*

rename -n "s/certificat/certificate_old/" ./ss*/*

You can also do:

你也可以这样做:

rename -n "s/certi*/certificate_old/" ./ss*/*

rename -n "s/certi*/certificate_old/" ./ss*/*

Which renames anything starting with certiin any sub-directory starting with ss.

它重命名任何certiss.

The sky is the limit.

天空才是极限。

Play around with regex and ALWAYS test this BEFORE with -n.

玩转正则表达式,并始终在使用-n.

WATCH OUT THIS WILL EVEN RENAME FOLDER NAMES THAT MATCH. Better cdinto the directory and do it there. USE AT OWN RISK.

小心这甚至会重命名匹配的文件夹名称。最好cd进入目录并在那里进行。使用风险自负。