在单个命令中使用前缀重命名文件夹中的所有文件 [英] Rename all files in a folder with a prefix in a single command

查看:22
本文介绍了在单个命令中使用前缀重命名文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重命名文件夹内的所有文件,前缀为"Unix_"

Rename all the files within a folder with prefix "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

推荐答案

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

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

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

否则使用方便的 rename 命令(它是一个 perl 脚本) - 尽管它可能不是在每个 Unix 上开箱即用的(例如,OS X 不带有 rename).

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).

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

A short overview at debian-administration.org:

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

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

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

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

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).

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

这篇关于在单个命令中使用前缀重命名文件夹中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆