This question is similar to one I asked recently, but the answers to that question, for the most part, do not extend to this one, AFAICT. The main difference between the two questions is that the earlier one was about copying while this one is about moving. Also, that earlier question was unnecessarily specific by referring to the relative path to a file. In this question I've removed this overspecificity.

I find myself doing this often enough that I wonder if there's a standard Unix way to do it:

% mkdir -p /TARGETDIR/relative/path/to
% mv ./relative/path/to/x /TARGETDIR/relative/path/to

In other words, I don't want to just move somefile to /TARGETDIR, but actually I want to "move" (so-to-speak) its entire relative path.

I know that I can write a shell function or a script to achieve the same effect, but I'd like to know if there already exists a way to do this with a single command?

1 Answer 1


Rsync could do it:

rsync -R --remove-source-files a/b/c/d /TARGETDIR/

But strictly speaking it's more a copy than a rename.

a function is simple:

mkmv() {
  mkdir -p -- "$2/$(dirname -- $1)" && mv -- "$1" "$2/$1"

and probably more efficiently (though rsync has a few more options which allow you to copy permissions or other attributes of the files or directories).

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.