Categories
terminal

Batch renaming files in the terminal

Renaming a few files, one at a time should not be very time consuming. But if a task requires hundreds, or sometimes thousands of files, there is a simple way to do it as well. You can rename multiple files simultaneously (batch) with using just the bash, in a single line. First, do a ‘dry-run’ with ‘echo’ to make sure this is the result you want. Here are the steps to execute a terminal batch renaming with ease:

To replace the file extension from ‘.config’ to ‘.json’:

$ for f in *.json; do echo "$f" "${f%.json}.config"; done
apple-develop.json apple-develop.config
banana-develop.json banana-develop.config
jackfruit-develop.json jackfruit-develop.config
orange-develop.json orange-develop.config

Then, replace ‘echo’ with ‘mv –‘ to actually rename the files:

$ for f in *.json; do mv -- "$f" "${f%.json}.config"; done
$ ls
apple-develop.config   jackfruit-develop.config
banana-develop.config  orange-develop.config

To replace a prefix ‘develop’ with ‘test’:

$ for f in *.config; do mv -- "$f" "test${f#develop}"; done
$ ls
test-apple.config   test-jackfruit.config
test-banana.config  test-orange.config

To replace the suffix ‘develop’ with ‘test’ (this one actually works with any occurrence of ‘develop’ to ‘test’):

$ for f in *.config; do mv -- "$f" "${f//develop/test}"; done
$ ls
apple-test.config  banana-test.config  jackfruit-test.config  orange-test.config

Now you have successfully finished a terminal batch renaming.

https://stackoverflow.com/questions/5394112/how-can-i-batch-rename-files-using-the-terminal

Leave a Reply

Your email address will not be published. Required fields are marked *