How can I easily replace a character in multiple file names?
Asked by
CuriousCat42
I have several files with names like '8.7blahblahblah', and I want to change the '.' to a '-' in all of them without having to rename each file one by one using 'mv'. Is there a way to do this in bulk?
Answers
Be sure you have the 'rename' command installed. If you don't have it yet, install it with 'sudo apt install rename'. Then you can run this in your terminal: `rename 's/./-/g' *.*` to rename your files.
Reply to this answer
You might want to try using 'mmv', which stands for multiple move. Not every Linux distro includes it in their repositories, but most do. It's pretty handy for bulk renaming tasks like this!
Reply to this answer
If you install Midnight Commander, you can easily select your files in the target directory and use the F6 key to rename them all at once. Just specify the source mask as '*.*' and the destination pattern like '1-2'. It's a simple way to change characters in the file names!
Reply to this answer
Before running any commands, I suggest testing them on some example files to make sure they perform as you expect. For instance, you can create a test directory and test files. Then, to replace the first '.' with a '-', run this command: `find . -type f -name '*.*' -exec rename 's/./-/g' {} +`. For replacing all occurrences, thereโs a more complex script that can handle it. Just try to double-check what each command does before applying!
Reply to this answer
You can also script the rename process using a bash script to handle the moving. This involves creating a script that generates new names for you. It's a bit complex, but a powerful method if you're comfortable with some coding!
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions