【发布时间】:2012-08-10 07:16:30
【问题描述】:
可能重复:
Extract filename and extension in bash
Linux: remove file extensions for multiple files
For example, A.txt B.txt, I want to rename then to A and B .
我如何使用 shell 脚本来做到这一点?还是其他方法?谢谢。
【问题讨论】:
可能重复:
Extract filename and extension in bash
Linux: remove file extensions for multiple files
For example, A.txt B.txt, I want to rename then to A and B .
我如何使用 shell 脚本来做到这一点?还是其他方法?谢谢。
【问题讨论】:
我会使用类似的东西:
#!/bin/bash
for file in *.txt
do
echo "$file" "$( echo $file | sed -e 's/\.txt//' )"
done
当然,将“.txt”的上述两个引用替换为您要删除的任何文件扩展名,或者最好只使用 $1(第一个传递给脚本的参数)。
迈克尔 G.
【讨论】:
$(ls *.txt) 应该只是*.txt,$file 应该被引用为"$file"(以及"$( echo ... )"),正则表达式应该是's/\.txt$//'(@987654328 @flag 的意思是“做多个替换”,这不是你想要的,你只想要一个)。
for i in *.txt; do mv "$i" "${i%.txt}"; done
【讨论】:
for FILE in *.txt ; do mv -i "$FILE" "$(basename "$FILE" .txt)" ; done
【讨论】: