I have many files with .txt extension. How to remove .txt extension for multiple files in linux?
I found that
rename .old .new *.old
substitutes .old
extension to the .new
Also I want to do this for files in sub-folders.
In Unix-like operating systems such as Linux, you can use the mv command to rename a single file or directory. To rename multiple files, you can use the rename utility. To rename files recursively across subdirectories, you can use the find and rename commands together.
Using rm Command The 'rm' command is a basic command-line utility in Linux to remove sockets, pipes, device nodes, symbolic links, directories, system files, etc. To remove a file with a particular extension, use the command 'rm'. This command is very easy to use, and its syntax is something like this.
rename
is slightly dangerous, since according to its manual page:
rename will rename the specified files by replacing the first occurrence of...
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).
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
I use this:
find ./ -name "*.old" -exec sh -c 'mv $0 `basename "$0" .old`.new' '{}' \;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With