4 ms·
How is it more convenient than `mv foo.bar foo.baz`?
by prashnts 10y ago
How is it more convenient than `mv foo.bar foo.baz`?
- sdoering 10y agoExactly what I was wondering.
- grenoire 10y agoLiterally the only part that does anything is: mv $vflag $iflag $1 ${1%\.*}.$ext
- antognini 10y agoYeah, it doesn't do much. But every so often I find myself needing to change the extension of a bunch of files somewhere and needing to write something like "for f in $(ls *.txt); do ... " and then I have to remember how to strip out the prefix (do I use # or % or ##?), and then I have to try it out on a set of test files to make sure I'm not accidentally deleting them all. I figured I would write something in the Unix philosophy to spare me (and maybe others) the trouble. It does exactly one thing, and it (hopefully) does it well. It was also fun to learn how to write a man page and do unit tests for a shell script.
- rogerbinns 10y agoNow try with more than one file like this: mv *.bar *.baz This is one place where Windows CMD works: ren *.bar *.baz It can't work on Unix shells because wildcard expansion is done before the program (mv) is invoked.
- AdieuToLogic 10y agoWhile not quite as convenient, it's not that tough in bash: ls *.bar | while read f; do mv $f ${f%.bar}.baz; done
- boobsbr 10y agoalso as for f in *.bar; do mv $f ${f%txt}baz; done
- seanp2k2 10y agoThis is called brace expansion: http://wiki.bash-hackers.org/syntax/expansion/brace http://wiki.bash-hackers.org/syntax/expansion/brace Very useful stuff. E.g. mv nginx.conf{,.old}
- rogerbinns 10y agoYour 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 ago