0

Is there a convenient way to rename a file (or dir) without redundantly repeating the path or directory-changing to it?

For example …

mv -some_flag db/migrations/abc_201911201243.php abc_20191101090000.php

… to rename the file without moving it out of that directory.

I looked into the man pages for mv, rename and rsync but didn't find anything, so I'm wondering if there is by chance a non-obvious trick to do this.

6

Use brace expansion:

mv -some_flag db/migrations/abc_201911{201243,01090000}.php
| improve this answer | |
  • That's so cool! I learned something useful today. And I've taken the liberty of removing the -some_flag since in my question it was assumed to be part of the solution; but for this solution, it isn't. – WoodrowShigeru Nov 20 '19 at 15:14
  • @WoodrowShigeru: Well, including it was kind of needed, as it demonstrates nothing can be placed between the file names. – choroba Nov 20 '19 at 15:20
0

I guess I already found a suitable solution: you change the directory and then return.

cd db/migrations/ && mv abc_201911201243.php abc_20191101090000.php && cd ~-
| improve this answer | |
  • 4
    or, if you find it simpler, a subshell: (cd db/migrations/ && mv abc_201911201243.php abc_20191101090000.php) – Jeff Schaller Nov 20 '19 at 13:07
  • 3
    note, if mv aborts, shell won't return you to original directory – rush Nov 20 '19 at 13:29
  • But after you type "cd db/migrations/ && mv abc_", and you press TAB, nothing happens. The brace expansion is a bit both - nice idea. For me, a cd, a tab completion and a corrected tab completion is as convenient as it can get. Depends on the length of the names also, and copy pasting them or not (hey it is allowed...sometimes ;) – user373503 Nov 20 '19 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.