2

I'm looking for a good shell one liner to move or rename a file inside a directory, where the target and destination parent directories are the same, and different than the current working directory. For example, the thing I don't want to write:

$ mv /usr/share/nginx/html/app.xml /usr/share/nginx/html/index.html

How can I do this same thing without typing '/usr/share/nginx/html/' twice or using multiple commands (to switch directory, pushd, etc)?

ErlVolton
  • 6,714
  • 2
  • 15
  • 26
  • My favorite part is that I was accidentally moving the wrong file here (.xml to .html, wtf?) but nobody cared and they just answered appropriately :) – ErlVolton Oct 20 '14 at 18:22

2 Answers2

6

You can use braces expansion:

$ mv /usr/share/nginx/html/{app.xml,index.html}
fredtantini
  • 15,966
  • 8
  • 49
  • 55
  • Part of the `parameter expansion` is [POSIX](http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02), but this syntax doesn't seems to be POSIX – Édouard Lopez Oct 20 '14 at 14:42
  • [brace expansion is Bash only](http://mywiki.wooledge.org/BraceExpansion#portability) – Édouard Lopez Oct 20 '14 at 14:57
2

You can use a subshell:

(cd /usr/share/nginx/html; mv app.xml index.html)
Cyrus
  • 84,225
  • 14
  • 89
  • 153