Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.