Linux 将文件重命名为大写

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

Linux rename files to uppercase

linuxbashubuntu

提问by Jay

I have large number of files in the format x00000.jpg, X00000.jpgand xx00000.jpg.

我有大量的文件格式x00000.jpgX00000.jpgxx00000.jpg.

How can I rename these files so they are all uppercase, ignoring the numeric part of the name?

如何重命名这些文件,使其全部为大写,而忽略名称的数字部分?

采纳答案by damienfrancois

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

回答by anubhava

Using tr:

使用 tr:

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

OUTPUT:

输出:

X00000.jpg

回答by pts

You can't rename files from Bash only, because Bash doesn't have any built-in command for renaming files. You have to use at least one external command for that.

您不能仅从 Bash 重命名文件,因为 Bash 没有任何用于重命名文件的内置命令。为此,您必须至少使用一个外部命令。

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, lsand mvare allowed:

如果 AWK,ls并且mv被允许:

# Insecure if the filenames contain an apostrophe or newline!
eval "$(ls -- *.jpg | awk '{print"mv -- \x27"
for file in *; do mv -- "$file" "${file^^}"; done
"\x27 \x27"toupper(
for file in * ; do      # or *.jpg, or x*.jpg, or whatever
    mv $file ${file^^}
done
)"\x27"}')"

If you have a lots of file, the solutions above don't work, because *.jpgexpands to a too long argument list (error: Argument list too long).

如果你有很多文件,上面的解决方案不起作用,因为*.jpg扩展到一个太长的参数列表(错误:参数列表太长)。

If trand mvare allowed, then see damienfrancois' answer.

如果trmv被允许,那么请参阅 damienfrancois 的回答。

If mvis allowed:

如果mv允许:

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

Please note that these rename .jpgto .JPGat the end, but you can modify them to avoid that.

请注意,这些在最后重命名.jpg.JPG,但您可以修改它们以避免这种情况。

回答by Keith Thompson

The bashshell has a syntax for translating a variable name to all-caps.

所述bash外壳具有用于翻译变量名全部大写的句法。

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

I think this may be a fairly new feature, so first verify that your version of bashimplements it. To avoid mistakes, try it once replacing mvby echo mv, just to make sure it's going to do what you want.

我认为这可能是一个相当新的功能,因此首先验证您的版本是否bash实现了它。为避免错误,尝试一次替换mvecho mv,只是为了确保它会做你想做的事。

The documentation for this feature is here, or type info bashand search for "upper".

此功能的文档在此处,或键入info bash并搜索“upper”。

You should probably decide what to do if the target file already exists (say, if both x00000.jpgand X00000.JPGalready exists), unless you're certain it's not an issue. To detect such name collisions, you can try:

如果目标文件已经存在(例如,如果两者都存在x00000.jpg并且X00000.JPG已经存在),您可能应该决定该怎么做,除非您确定这不是问题。要检测此类名称冲突,您可以尝试:

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

and look for any lines not starting with 1.

并查找任何不以1.

回答by Wiley

Combining previous answers could yield:

结合以前的答案可能会产生:

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

回答by Jahid

If only renaming files/dirs is all you want, then you can use rnm:

如果您只需要重命名文件/目录,那么您可以使用rnm

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

Explanation:

解释:

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

More examples can be found here.

可以在此处找到更多示例。

回答by Pablo A

rename

rename

Probably the easiest way for renaming multiple files is using rename. To translate lowercase names to upper, you'd:

重命名多个文件的最简单方法可能是使用rename. 要将小写名称转换为大写,您需要:

##代码##

If the files are also in subdirs you can use globstaror find:

如果文件也在子目录中,您可以使用 globstarfind

##代码##

References

参考