Linux:对于多个文件,删除文件扩展名 [英] Linux: remove file extensions for multiple files
问题描述
我有一个扩展名为.txt多个文件。
如何在Linux中删除扩展名为.txt多个文件?
I have many files with .txt extension. How to remove .txt extension for multiple files in linux?
我发现
rename .old .new *.old
替代 .old的
扩展到。新
此外,我想为子文件夹中的文件做到这一点。
Also I want to do this for files in sub-folders.
推荐答案
改名
稍微危险的,根据其手册页,因为:
rename
is slightly dangerous, since according to its manual page:
重命名将通过更换指定的文件重命名第一的发生...
rename will rename the specified files by replacing the first occurrence of...
这会很乐意做文件名错误的事情如 c.txt.parser.y
。
It will happily do the wrong thing with filenames like c.txt.parser.y
.
下面是一个使用的解决方案找到
和庆典
:
Here's a solution using find
and bash
:
find -type f -name '*.txt' | while read f; do mv "$f" "${f%.txt}"; done
请记住,这将打破,如果文件名包含一个换行符(罕见,但不是不可能的)。
Keep in mind that this will break if a filename contains a newline (rare, but not impossible).
如果你有GNU发现,这是一个更坚实的解决方案:
If you have GNU find, this is a more solid solution:
find -type f -name '*.txt' -print0 | while read -d $'\0' f; do mv "$f" "${f%.txt}"; done
这篇关于Linux:对于多个文件,删除文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!