Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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