简体   繁体   English

Linux 将文件重命名为大写

[英]Linux rename files to uppercase

I have large number of files in the format x00000.jpg , X00000.jpg and xx00000.jpg .我有大量格式x00000.jpgX00000.jpgxx00000.jpg的文件。

How can I rename these files so they are all uppercase, ignoring the numeric part of the name?如何重命名这些文件,使它们全部大写,忽略名称的数字部分?

for f in * ; do mv -- "$f" "$(tr [:lower:] [:upper:] <<< "$f")" ; done

You can't rename files from Bash only, because Bash doesn't have any built-in command for renaming files.您不能仅从 Bash 重命名文件,因为 Bash 没有任何用于重命名文件的内置命令。 You have to use at least one external command for that.为此,您必须至少使用一个外部命令。

If Perl is allowed:如果允许 Perl:

perl -e 'for(@ARGV){rename$_,uc}' *.jpg

If Python is allowed:如果允许使用 Python:

python -c 'import os, sys; [os.rename(a, a.upper()) for a in sys.argv[1:]]' *.jpg

If you have thousands or more files, the solutions above are fast, and the solutions below are noticably slower.如果您有数千个或更多文件,则上述解决方案速度很快,而以下解决方案明显较慢。

If AWK, ls and mv are allowed:如果允许 AWK、 lsmv

# Insecure if the filenames contain an apostrophe or newline!
eval "$(ls -- *.jpg | awk '{print"mv -- \x27"$0"\x27 \x27"toupper($0)"\x27"}')"

If you have a lots of file, the solutions above don't work, because *.jpg expands to a too long argument list (error: Argument list too long ).如果您有很多文件,则上述解决方案不起作用,因为*.jpg扩展为过长的参数列表(错误:参数列表太长)。

If tr and mv are allowed, then see damienfrancois' answer.如果允许trmv ,请参阅 damienfrancois 的回答。

If mv is allowed:如果允许mv

for file in *; do mv -- "$file" "${file^^}"; done

Please note that these rename .jpg to .JPG at the end, but you can modify them to avoid that.请注意,这些文件最后将.jpg重命名为.JPG ,但您可以修改它们以避免这种情况。

The bash shell has a syntax for translating a variable name to all-caps. bash shell 具有将变量名转换为全大写的语法。

for file in * ; do      # or *.jpg, or x*.jpg, or whatever
    mv "$file" "${file^^}"
done

This feature was introduced in bash version 4.0, so first verify that your version of bash implements it.这个特性是在 bash 版本 4.0 中引入的,所以首先验证你的bash版本是否实现了它。 To avoid mistakes, try it once replacing mv by echo mv , just to make sure it's going to do what you want.为避免错误,请尝试一次将mv替换为echo mv ,以确保它会执行您想要的操作。

The documentation for this feature is here , or type info bash and search for "upper".此功能的文档在此处,或键入info bash并搜索“upper”。

You should probably decide what to do if the target file already exists (say, if both x00000.jpg and X00000.JPG already exists), unless you're certain it's not an issue.如果目标文件已经存在(例如,如果x00000.jpgX00000.JPG都已经存在),您可能应该决定该怎么做,除非您确定这不是问题。 To detect such name collisions, you can try:要检测此类名称冲突,您可以尝试:

ls *.txt | tr '[a-z]' '[A-Z]' | sort | uniq -c | sort -n

and look for any lines not starting with 1 .并查找任何不以1开头的行。

rename

Probably the easiest way for renaming multiple files is using Perl's rename .重命名多个文件的最简单方法可能是使用 Perl 的rename To translate lowercase names to upper, you'd:要将小写名称转换为大写名称,您需要:

rename 'y/a-z/A-Z/' *

If the files are also in subdirs you can use globstar or find :如果文件也在子目录中,您可以使用 globstarfind

find . -maxdepth 1 -type f -iname "*.jpg" -execdir rename "y/a-z/A-Z/" {} +

References参考

Combining previous answers could yield:结合以前的答案可能会产生:

for file in * ; do            # or *.jpg, or x*.jpg, or whatever
   basename=$(tr '[:lower:]' '[:upper:]' <<< "${file%.*}")
   newname="$basename.${file#*.}"
   mv "$file" "$newname"
done

Using tr:使用 tr:

f="x00000.jpg"
n="${f%.*}"
n=$(tr '[:lower:]' '[:upper:]' <<< "$n")
f="$n.${f#*.}"
echo "$f"

OUTPUT:输出:

X00000.jpg

If only renaming files/dirs is all you want, then you can use rnm :如果您只想重命名文件/目录,那么您可以使用rnm

rnm -rs '/./\C/g' -fo -dp -1 *

Explanation:解释:

  1. -rs : replace string. -rs :替换字符串。 /./\C/g replaces all match of . /./\C/g替换. (regex) to it's uppercase. (正则表达式)为大写。
  2. -fo : file only mode -fo :仅文件模式
  3. -dp : depth of directory (-1 means unlimited). -dp :目录深度(-1 表示无限制)。

More examples can be found here .更多示例可以在这里找到。

如果您像我一样使用 zsh:

for f in * ; do mv -- "$f" "${f:u}" ; done

Rename all source files to uppercase and stage to git将所有源文件重命名为大写并暂存为 git

#!/bin/bash

SOURCE_DIRS=('./src' './Public' './Private')
FILE_EXTENSION="*.php"

for dir in $SOURCE_DIRS; do
    files="$(find "$dir" -name "${FILE_EXTENSION[@]}";)"
    for file in $files; do
        base_name="$(basename $file)";
        new_name=$(sed "s/$base_name/${base_name^}/g" <<< $file)
        git mv $file ${new_name} # or
        # mv $file ${new_name}
    done
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM