我想用awk/sed/bash将小写文件名更改为大写
你的帮助将不胜感激
aaaa.txt
vvjv.txt
acfg.txt
Run Code Online (Sandbox Code Playgroud)
期望的输出
AAAA.txt
VVJV.txt
ACFG.txt
Run Code Online (Sandbox Code Playgroud)
Ste*_*eve 18
前言:
如果您不关心扩展的情况,只需在shell循环中使用'tr'实用程序:
for i in *.txt; do mv "$i" "$(echo "$i" | tr '[a-z]' '[A-Z]')"; done
Run Code Online (Sandbox Code Playgroud)
如果您确实关心扩展的情况,那么您应该知道有多种方法(TIMTOWTDI).就个人而言,我相信这里列出的Perl解决方案可能是Linux下最简单,最灵活的解决方案.如果您有多个文件扩展名,只需指定您希望保持不变的数字.该BASH4解决方案也是一个非常好的一个,但你必须愿意写出来的扩展几次,或者使用其他变量来存储它.但是如果您需要严格的可移植性,那么我建议使用octals的答案中的最后一个解决方案.有些版本的Linux还附带了一个名为rename的工具,也可能值得一试.它的用法因发行版和发行版而异,因此请输入man rename更多信息.
解决方案:
使用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
Run Code Online (Sandbox Code Playgroud)
使用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
Run Code Online (Sandbox Code Playgroud)
使用GNU awk:
for i in *.txt; do
mv "$i" $(echo "$i" | awk '{ sub(/.txt$/,""); print toupper($0) ".txt" }');
done
Run Code Online (Sandbox Code Playgroud)
使用GNU sed:
for i in *.txt; do
mv "$i" $(echo "$i" | sed -r -e 's/.*/\U&/' -e 's/\.TXT$/\u.txt/');
done
Run Code Online (Sandbox Code Playgroud)
使用BASH3.2:
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
Run Code Online (Sandbox Code Playgroud)
通常,对于小写/大写修改,经常使用"tr"(翻译字符)实用程序,它来自用于字符替换的命令行实用程序集.
dtpwmbp:~ pwadas$ echo "xxx" | tr '[a-z]' '[A-Z]'
XXX
dtpwmbp:~ pwadas$
Run Code Online (Sandbox Code Playgroud)
此外,对于重命名文件,还有"重命名"实用程序,与perl(man rename)一起提供.
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/' *
Run Code Online (Sandbox Code Playgroud)
我建议使用rename,如果您只想大写文件名而不是扩展名,请使用如下所示:
rename -n 's/^([^.]*)\.(.*)$/\U$1\E.$2/' *
Run Code Online (Sandbox Code Playgroud)
\U将所有内容都大写,直到\E参阅perlreref(1)。-n当您对输出满意时,将其删除。