简体   繁体   中英

Changing all file's extensions in a folder using CLI in Linux

如何在 Linux 的 CLI 上使用一个命令更改文件夹中所有文件的扩展名?

使用rename

rename 's/.old$/.new/' *.old

If you have the perl rename installed (there are different rename implementations) you can do something like this:

$ ls -1
test1.foo
test2.foo
test3.foo

$ rename 's/\.foo$/.bar/' *.foo

$ ls -1
test1.bar
test2.bar
test3.bar

You could use a for-loop on the command line:

for foo in *.old; do mv $foo `basename $foo .old`.new; done

this will take all files with extension .old and rename them to .new

This should works on current directory AND sub-directories. It will rename all .oldExtension files under directory structure with a new extension.

for f in `find . -iname '*.oldExtension' -type f -print`;do mv "$f" ${f%.oldExtension}.newExtension; done

Source : recursively add file extension to all files (Not my answer.)

find . -type f -exec mv '{}' '{}'.jpg \\;

Explanation: this recursively finds all files (-type f) starting from the current directory (.) and applies the move command (mv) to each of them. Note also the quotes around {}, so that filenames with spaces (and even newlines...) are properly handled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM