Score:1

How to find and rename files with different extensions

cn flag

I have some files inside a folder like this:

DUMAK011.618
DUMAK011.619
DUMAK011.620

I would like to rename them to:

DUMAI011.618
DUMAI011.619
DUMAI011.620
Score:3
cn flag

This can be achieved in Bash with a one-liner:

for i in DUMAK011.6*; do mv "$i" "DUMAI${i##DUMAK}" ;done
fuzzyTew avatar
mg flag
If you use bash, this is a good tool to have in your toolbelt. It works for many other tasKs and doesn't need anything new installed on a system.
Score:3
in flag

You can do this with mmv (Multi-Move), so long as you install it first:

sudo apt install mmv

Once installed, you can do something like this:

mmv DUMAK\* DUMAI\#1

That will rename all files starting with DUMAK, leaving everything afterwards and the extension untouched. If you are proficient with shell glob patterns, you can save a remarkable amount of time with this tool.

If you'd like, you can learn more about the power of mmv from the documentation.

Janchi Garcia avatar
cn flag
Thank you for this but since the file were recurrence/ downloaded everyday on the said folder, is it possible to have a bash script for this so the new files were automatically renamed once detected? Or this only possible by manual execution? Thank you so much in advance.
in flag
You can certainly write a script that will automatically rename files if that's what you would like to do. The script could be executed at regular intervals via a cron job, as that would require fewer resources than having a file system watcher.
Janchi Garcia avatar
cn flag
On the given command line, it will only rename files starting with DUMAK. Forgot to mentioned but what if there is an other file that need to be rename as well, e.g HUMAK and SUMAK, can I rename those files in one execution (DUMAK, SUMAK and HUMAK)?
in flag
So long as there is a predicable pattern, yes. Take a look at shell glob patterns and how they're expanded. That'll let you rename items accordingly.
hr flag
... for example `mmv -n '?UMAK*' '#1UMAI#2'` or `mmv -n '*MAK*' '#1MAI#2'`
fuzzyTew avatar
mg flag
You can use `inotifywait` to wait for new files in a folder. That wasn't included in the question, though.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.