3 ms·
Your version above has at least two bugs I see (will fall over with various forms of white space in the filenames). And it would be nice to have interactivity
by rogerbinns 10y ago
Your version above has at least two bugs I see (will fall over with various forms of white space in the filenames). And it would be nice to have interactivity and verboseness (admittedly just flags to mv). It would also be better to work with plain portable shell, rather than requiring bash, so you'll need to include some basename calls. Also should it stop on the first rename failure, or just keep ploughing on no matter what?
Before you know it, you have a script you've debugged and tested.
- AdieuToLogic 10y agoThe command I whipped up is, by definition, a one-liner meant for situations where the file movement is known by the person wishing to perform it and done interactively. As to the portability of it, yes this is not portable as the syntax relies on substitution syntax supported by a limited number of Unix shells (bash and ksh spring to mind, others may support it too though). Since the OP specifically mentioned: mv foo.bar foo.baz And you pointed out CMD's 'ren' command, the incantation I put forth seemed apropos. YMMV.
- rogerbinns 10y agoDealing with one file is no problem. A regular rename on the command line or gui file manager of choice works well. The difficulties come when trying to work with multiple files, and why you'd resort to the command line. Amusingly, the rename on Windows just works. UNIX makes it difficult because of the wildcard expansion. You can start writing some script, but there are gotchas galore such as correct escaping, IFS, shell portability, dry runs, interactivity, error handling and the list goes on. Having something that works as well as the simple Windows command is hard work, and before you know it end up publishing a github repository!
- AdieuToLogic 10y agoDealing with one file is no problem. ... The difficulties come when trying to work with multiple files, and why you'd resort to the command line. Maybe we just have a different perception regarding these workflows. Not that either is superior or anything, just different. My background is one where the command line is not something I turn to, but instead one where I begin. UNIX makes it difficult because of the wildcard expansion. In this specific situation, your point is spot on. I submit that in many other situations, the wildcard expansion employed by many Unix shells is very helpful. Not every tool is applicable for every situation and the specific case of renaming multiple files to those with different extensions highlights a shortcoming of many Unix shells. Perhaps an explanation is due to file extensions being of little importance to the vast majority of Unix programs. Having something that works as well as the simple Windows command is hard work ... Here, I will respectfully disagree and step away from this thread :-).