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
Linux rename files to uppercase
提问by Jay
I have large number of files in the format x00000.jpg
, X00000.jpg
and xx00000.jpg
.
我有大量的文件格式x00000.jpg
,X00000.jpg
和xx00000.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, ls
and mv
are 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 *.jpg
expands to a too long argument list (error: Argument list too long).
如果你有很多文件,上面的解决方案不起作用,因为*.jpg
扩展到一个太长的参数列表(错误:参数列表太长)。
If tr
and mv
are allowed, then see damienfrancois' answer.
如果tr
和mv
被允许,那么请参阅 damienfrancois 的回答。
If mv
is allowed:
如果mv
允许:
ls *.txt | tr '[a-z]' '[A-Z]' | sort | uniq -c | sort -n
Please note that these rename .jpg
to .JPG
at the end, but you can modify them to avoid that.
请注意,这些在最后重命名.jpg
为.JPG
,但您可以修改它们以避免这种情况。
回答by Keith Thompson
The bash
shell 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 bash
implements it. To avoid mistakes, try it once replacing mv
by echo mv
, just to make sure it's going to do what you want.
我认为这可能是一个相当新的功能,因此首先验证您的版本是否bash
实现了它。为避免错误,尝试一次替换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. 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:
解释:
-rs
: replace string././\C/g
replaces all match of.
(regex) to it's uppercase.-fo
: file only mode-dp
: depth of directory (-1 means unlimited).
-rs
: 替换字符串。/./\C/g
将.
(regex) 的所有匹配项替换为大写。-fo
: 仅文件模式-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
:
如果文件也在子目录中,您可以使用 globstar或find
: