cp or mv files to same directory they are already in when I am in another?
Solution 1
To minimize the amount of typing, it's probably easiest to do it in two steps. CD to the directory, then move.
If you only have one command, you can use a subshell:
$ (cd blob/a_long_directory_name/c/ && mv x.x y.y )
That means the cd
will only take effect in the subshell,
the mv
will only happen if the cd
succeeds,
and the working directory of your current shell won't change.
If you have more than one command, use the directory stack.
$ pushd blob/a_long_directory_name/c/
$ mv x.x y.y
$ mv z.z q.q
$ popd
Solution 2
With bash, you could use brace expansion
mv blob/a_long_directory_name/{c/x.x,evenmore/y.y}
Solution 3
You could use your shell's text substitution capabilities. In bash
, you could use a beast like:
mv blob/a_long_directory_name/c/xx !#:$:gs^c/xx^evenmore/yy
To break it down:
!#
is the current line typed so far
:$
asks to pick the last word (replace with a number n
to pick n
'th word) of the string under consideration (!#
in this case). The last word of the line typed so far would be the previous word.
:gs^xx^yy
does global (g
) textual substitution (s
) on the resulting word, replacing 'xx' with 'yy'. ^
is just a delimiter for the s
command to allow using /
in your patterns.
This method is slightly more general as it lets you replace any part of the path ('xx') with any other word ('yy').
Finally, like with any history manipulation, it's good to have appropriate shell options set (e.g. in ~/.bashrc
). In this case
shopt -s histverify
will display the edited line after you press return, so you can still edit it and check for errors.
Why you'd use a construct like that I'm not sure, but it works in this case.
Related videos on Youtube
Comments
-
Michael Durrant almost 2 years
If I am in
~/blob
and I have a file in~/blob/a_long_directory_name/c/x.x
I can type
mv blob/a_long_directory_name/c/x.x blob/a_long_directory_name/even_more/y.y
Is there any shortcut whereby I can type something shorter that uses the directory path in the first param (not my current directory though), e.g.
mv blob/a_long_directory_name/c/x.x $same_dir/y.y
where something like
$same_dir
would point to the dir of param 1 -
Useless almost 11 yearsThis doesn't address the
even_more
relative path ... -
mhars almost 11 yearsThat is correct. The $same_dir example seemed to ignore it as well.
-
Jeff Hewitt almost 11 yearsa) Too much line noise (compare to an elegant brace expansion) b) Does not solve the OP's problem: they want to replace
c/x.x
withevenmore/y.y
keeping the original path prefix. -
Wojtek almost 11 yearsYour second comment was a simple omission on my part, that required a simple edit to fix. I don't see how this answer deserved a downvote. With the edit (technicality), it does solve the problem and is more general as it allows swapping any part of the path (I often used something similar on
$PWD
). Sure, maybe it's not the most elegant but it points to the fact that shell does have textual substitutions and word designators, which some users might still find useful to research. -
Michael Durrant almost 11 years+1 I like it. Very easy to remember and do and that is key to me
-
Jeff Hewitt almost 11 yearsOK. I stand corrected.
-
Useless almost 11 yearsInteresting, since it doesn't do what you asked for. Maybe your question isn't exactly what you intended?
-
Michael Durrant almost 11 yearsUseless, I updated my title to clarify that it's about cp/mv files and keep them in that same directory, even though I am in another. btw your username is cute but it makes comments look silly when used in them :)