使用 awk 、sed 或 bash 将小写文件名更改为大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12468173/
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
change lowercase file names to uppercase with awk ,sed or bash
提问by rebca
I would like to change lowercase filenames to uppercase with awk/sed/bash
我想使用 awk/sed/bash 将小写文件名更改为大写
your help would be appreciated
您的帮助将不胜感激
aaaa.txt
vvjv.txt
acfg.txt
desired output
期望的输出
AAAA.txt
VVJV.txt
ACFG.txt
回答by Steve
PREFACE:
前言:
If you don't care about the case of your extensions, simply use the 'tr' utility in a shell loop:
如果您不关心扩展的情况,只需在 shell 循环中使用 'tr' 实用程序:
for i in *.txt; do mv "$i" "$(echo "$i" | tr '[a-z]' '[A-Z]')"; done
If you do care about the case of the extensions, then you should be aware that there is more than one way to do it (TIMTOWTDI). Personally, I believe the Perlsolution, listed here, is probably the simplest and most flexible solution under Linux. If you have multiple file extensions, simply specify the number you wish to keep unchanged. The BASH4solution is also a very good one, but you must be willing to write out the extension a few times, or alternatively, use another variable to store it. But if you need serious portability then I recommend the last solution in this answer which uses octals. Some flavours of Linux also ship with a tool called renamethat may also be worth checking out. It's usage will vary from distro to distro, so type man renamefor more info.
如果您确实关心扩展的情况,那么您应该意识到有不止一种方法可以做到(TIMTOWTDI)。我个人认为,这里列出的Perl解决方案可能是 Linux 下最简单、最灵活的解决方案。如果您有多个文件扩展名,只需指定您希望保持不变的数字。该BASH4解决方案也是一个非常好的一个,但你必须愿意写出来的扩展几次,或者使用其他变量来存储它。但是如果你需要真正的便携性,那么我推荐这个答案中使用八进制的最后一个解决方案。某些版本的 Linux 还附带一个名为rename的工具这也可能值得一试。它的用法会因发行版而异,因此请输入man rename以获取更多信息。
SOLUTIONS:
解决方案:
Using Perl:
使用Perl:
# single extension
perl -e 's/\.[^\.]*$/rename $_, uc($`) . $&/e for @ARGV' *.txt
# multiple extensions
perl -e 's/(?:\.[^\.]*){2}$/rename $_, uc($`) . $&/e for @ARGV' *.tar.gz
Using BASH4:
使用BASH4:
# single extension
for i in *.txt; do j="${i%.txt}"; mv "$i" "${j^^}.txt"; done
# multiple extensions
for i in *.tar.gz; do j="${i%.tar.gz}"; mv "$i" "${j^^}.tar.gz"; done
# using a var to store the extension:
e='.tar.gz'; for i in *${e}; do j="${i%${e}}"; mv "$i" "${j^^}${e}"; done
Using GNU awk:
使用GNU awk:
for i in *.txt; do
mv "$i" $(echo "$i" | awk '{ sub(/.txt$/,""); print toupper(for i in *.txt; do
mv "$i" $(echo "$i" | sed -r -e 's/.*/\U&/' -e 's/\.TXT$/\u.txt/');
done
) ".txt" }');
done
Using GNU sed:
使用GNU sed:
for i in *.txt; do
stem="${i%.txt}";
for ((j=0; j<"${#stem}"; j++)); do
chr="${stem:$j:1}"
if [[ "$chr" == [a-z] ]]; then
chr=$(printf "%o" "'$chr")
chr=$((chr - 40))
chr=$(printf '\'"$chr")
fi
out+="$chr"
done
mv "$i" "$out.txt"
out=
done
Using BASH3.2:
使用BASH3.2:
dtpwmbp:~ pwadas$ echo "xxx" | tr '[a-z]' '[A-Z]'
XXX
dtpwmbp:~ pwadas$
回答by Piotr Wadas
In general for lowercase/upper case modifications "tr" ( translate characters ) utility is often used, it's from the set of command line utilities used for character replacement.
通常,对于小写/大写修改,经常使用“tr”(翻译字符)实用程序,它来自用于字符替换的命令行实用程序集。
SYNOPSIS
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
DESCRIPTION
"rename" renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected to modify the $_ string in
Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line,
filenames will be read via standard input.
For example, to rename all files matching "*.bak" to strip the extension, you might say
rename 's/\.bak$//' *.bak
To translate uppercase names to lower, you'd use
rename 'y/A-Z/a-z/' *
Also, for renaming files there's "rename" utility, delivered with perl ( man rename ).
此外,对于重命名文件,还有 perl 提供的“重命名”实用程序( man rename )。
rename -n 's/^([^.]*)\.(.*)$/\U\E./' *
回答by Thor
I would suggest using rename, if you only want to uppercase the filename and not the extension, use something like this:
我建议使用rename,如果您只想大写文件名而不是扩展名,请使用以下内容:
for i in *.txt
do
fname=$(echo $i | cut -d"." -f1 | tr [a-z] [A-Z])
ext=$(echo $i | cut -d"." -f2)
mv $i $fname.$ext
done
\Uuppercases everything until \E, see perlreref(1). Remove the -nwhen your happy with the output.
\U大写所有内容,直到\E,请参阅perlreref(1)。-n当您对输出感到满意时删除。
回答by Ashish Kumar
An easier, lightweight and portable approach would be:
一种更简单、轻便和便携的方法是:
for i in *.txt; do mv $i `echo ${i%.*} | tr [:lower:] [:upper:]`.txt; done;
This would work on almost every version of BASH since we are using most common external utilities (cut, tr) found on every Unix flavour.
这几乎适用于 BASH 的每个版本,因为我们使用的是在每个 Unix 版本上都能找到的最常见的外部实用程序(cut、tr)。
回答by Vasu
Simply use (on terminal):
只需使用(在终端上):
for f in *.txt; do
mv "$f" "`tr [:lower:] [:upper:] <<< "${f%.*}"`.txt"
done
回答by Ansgar Wiechers
for i in *.txt; do
i="${i%.txt}"
mv "$i.txt" "${i^^?}.txt"
done
回答by chepner
Bash 4 parameter expansion can perform case changes:
Bash 4 参数扩展可以进行大小写更改:
for f in *.txt; do
no_ext=${f%.txt}
mv "$f" "${no_ext^^}.txt"
done
回答by glenn Hymanman
bash:
重击:
printf "%s\n" *.txt | sed 'h;s/[^.]*/\U&/;H;g;s/\(.*\)\n/mv -v /' | sh
回答by potong
This might work for you (GNU sed):
这可能对你有用(GNU sed):
printf "%s\n" *.txt | sed 'h;s/[^.]*/\U&/;H;g;s/\(.*\)\n/mv -v /e'
or more simply:
或更简单地说:
for i in *.jar; do mv $i `echo ${i%} | tr [:upper:] [:lower:]`; done;
回答by NOZUONOHIGH
this works for me.
这对我有用。

