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

时间:2021-03-11 00:35:14

Rename all the files within a folder with prefix “Unix_” i.e. suppose a folder has two files a.txt and b.pdf, then they both should be renamed from a single command to Unix_a.txt and Unix_b.pdf

将文件夹中的所有文件重命名为“Unix_”,即假设一个文件夹有两个文件a。txt和b。然后,应该将它们从单个命令重命名为Unix_a。txt和Unix_b.pdf

8 个解决方案

#1


73  

If your filenames contain no whitepace and you don't have any subdirectories, you can use a simple for loop:

如果你的文件名不包含白名单,你没有任何子目录,你可以使用一个简单的for循环:

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

Otherwise use the convenient rename command (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 -printf option, unfortunately. But GNU findutils should be installable (on e.g. Mac OS X with brew install findutils).

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

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

#2


53  

Try the rename command in the folder with the files:

尝试在文件夹中使用文件重命名命令:

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.

重命名(sed命令)的争论表明与Unix_取代regex ^。插入符号(^)是一个特殊的字符,这意味着开始。

#3


22  

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

我想这正是你要找的:

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

Yes, it is simple, elegant and powerful, and alos oneline-comand of courese. You can get more detailed intro from me on the page:Rename Files and Directories (Add Prefix)

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

#4


6  

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,苹果将批量重命名功能直接集成到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.

      格式:为文件选择名称格式,然后选择将索引、计数器或日期放在名称之前或之后。在Custom Format字段中输入一个名称,然后输入您想要开始的数字。

  4. Click Rename.

    点击重命名。

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

如果您的文件中有一个常见的模式,您可以使用替换文本,否则添加文本也可以这样做。

#5


4  

You can just use -i instead of -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 -i option
  • xargs -根据-i选项逐行接受所有文件
  • {} is the placeholder for all files, necessary if xargs gets more than two arguments as input
  • {}是所有文件的占位符,如果xargs获得两个以上的参数作为输入,则必须使用{}

Using awk:

使用awk:

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

#6


0  

Also works for items with spaces and ignores directories

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

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

#7


0  

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.

P。我是这个工具的作者。

#8


0  

Situation:

情境:

We have certificate.key certificate.crt inside /user/ssl/

我们有证书。密钥证书。crt / user / ssl /内部

We want to rename anything that starts with certificate to certificate_OLD

我们希望将以证书开头的任何内容重命名为certificate_OLD

We are now located inside /user

我们现在位于/用户内部。

First, you do a dry run with -n:

首先,你用-n进行试车:

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

重命名- n s /证书/ certificate_old /。/ ssl / *

Which returns:

返回:

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

重命名(. / ssl /证书。crt,。/ ssl / certificate_OLD.crt)重命名(. / ssl /证书。关键,。/ 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/*

重命名“s /证书/ certificate_OLD”。/ ssl / *

What it means:

它的意思是:

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

'重命名' s/ SOMETHING / SOMETING_ELSE '路径/文件

Tip:

提示:

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

如果你已经在路径上运行它:

rename "s/certificate/certificate_OLD/" *

重命名" s /证书/ certificate_OLD / " *

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

或者,如果你想在任何子目录中这样做,从ss do开始:

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

重命名- n s /证书/ certificate_old /。/ ss * / *

You can also do:

你也可以做的事:

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

重命名- n / certi * / certificate_old /”。/ ss * / *

Which renames anything starting with certi in any sub-directory starting with ss.

在任何以ss开头的子目录中重命名任何以证言开头的内容。

The sky is the limit.

天空是极限。

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

使用regex,并始终使用-n进行测试。

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

注意,这甚至会重命名匹配的文件夹名。更好的cd进入目录并在那里执行。使用在自己的风险。

#1


73  

If your filenames contain no whitepace and you don't have any subdirectories, you can use a simple for loop:

如果你的文件名不包含白名单,你没有任何子目录,你可以使用一个简单的for循环:

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

Otherwise use the convenient rename command (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 -printf option, unfortunately. But GNU findutils should be installable (on e.g. Mac OS X with brew install findutils).

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

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

#2


53  

Try the rename command in the folder with the files:

尝试在文件夹中使用文件重命名命令:

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.

重命名(sed命令)的争论表明与Unix_取代regex ^。插入符号(^)是一个特殊的字符,这意味着开始。

#3


22  

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

我想这正是你要找的:

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

Yes, it is simple, elegant and powerful, and alos oneline-comand of courese. You can get more detailed intro from me on the page:Rename Files and Directories (Add Prefix)

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

#4


6  

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,苹果将批量重命名功能直接集成到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.

      格式:为文件选择名称格式,然后选择将索引、计数器或日期放在名称之前或之后。在Custom Format字段中输入一个名称,然后输入您想要开始的数字。

  4. Click Rename.

    点击重命名。

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

如果您的文件中有一个常见的模式,您可以使用替换文本,否则添加文本也可以这样做。

#5


4  

You can just use -i instead of -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 -i option
  • xargs -根据-i选项逐行接受所有文件
  • {} is the placeholder for all files, necessary if xargs gets more than two arguments as input
  • {}是所有文件的占位符,如果xargs获得两个以上的参数作为输入,则必须使用{}

Using awk:

使用awk:

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

#6


0  

Also works for items with spaces and ignores directories

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

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

#7


0  

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.

P。我是这个工具的作者。

#8


0  

Situation:

情境:

We have certificate.key certificate.crt inside /user/ssl/

我们有证书。密钥证书。crt / user / ssl /内部

We want to rename anything that starts with certificate to certificate_OLD

我们希望将以证书开头的任何内容重命名为certificate_OLD

We are now located inside /user

我们现在位于/用户内部。

First, you do a dry run with -n:

首先,你用-n进行试车:

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

重命名- n s /证书/ certificate_old /。/ ssl / *

Which returns:

返回:

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

重命名(. / ssl /证书。crt,。/ ssl / certificate_OLD.crt)重命名(. / ssl /证书。关键,。/ 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/*

重命名“s /证书/ certificate_OLD”。/ ssl / *

What it means:

它的意思是:

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

'重命名' s/ SOMETHING / SOMETING_ELSE '路径/文件

Tip:

提示:

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

如果你已经在路径上运行它:

rename "s/certificate/certificate_OLD/" *

重命名" s /证书/ certificate_OLD / " *

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

或者,如果你想在任何子目录中这样做,从ss do开始:

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

重命名- n s /证书/ certificate_old /。/ ss * / *

You can also do:

你也可以做的事:

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

重命名- n / certi * / certificate_old /”。/ ss * / *

Which renames anything starting with certi in any sub-directory starting with ss.

在任何以ss开头的子目录中重命名任何以证言开头的内容。

The sky is the limit.

天空是极限。

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

使用regex,并始终使用-n进行测试。

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

注意,这甚至会重命名匹配的文件夹名。更好的cd进入目录并在那里执行。使用在自己的风险。