Is there a --parents option for mv like for cp?

6,704

Solution 1

Not directly, at least not for GNU mv according to its man page. A possible workaround would be adding a function like this:

pmv(){
    source=$1
    target=${2%/}
    mkdir -p -- "$target/$(dirname -- "$source")" &&
      mv -- "$source" "$target/$(dirname -- "$source")/"
}

Add that function to your shell's configuration file (~/.bashrc if you are using bash) and run it like this:

pmv source/directory/from target/

Solution 2

Is there a way to move a file in a similar fashion?

None. mv is rather simple utility which uses system call rename (atomic FS operation) if possible — when source/destination are on the same block device, otherwise it falls back to cp behavior. Actually rename would fail even if you src/dst are the same device mounted to different mount points which Linux does allow.

So, when you need to move files like:

cp -v --parents source/directory/from target/

you can just think of:

mv source target/

but keep in mind that it would move the whole content all the parents dir could have inside along the way, so you'd need to deal with that later — I mean unlinking all those extra-files if you need to have the same result as cp --parents would provide.

Sooner or later you'd realize, that what you're doing could be easily accomplished with cp/rm approach instead. And cp's -l can be used to make it working fast, meanwhile allowing to use beloved --parents. That's the reason mv wouldn't have anything such fancy as cp's --parents.

Share:
6,704

Related videos on Youtube

Flimm
Author by

Flimm

Updated on September 18, 2022

Comments

  • Flimm
    Flimm almost 2 years

    cp has a useful feature --parents that allows a file's directory structure to be copied to another directory:

    $ cp -v --parents source/directory/from target/
    ‘source/directory/from’ -> ‘target/source/directory/from’
    

    How do I similarly move a file?

  • terdon
    terdon over 10 years
    @poige what does "Typical somewhat different" mean? As for modes, the OP did not mention that they were supposed to be kept. cp --parents which is what the OP was using does not keep the original modes so why should I provide an answer that does if that has not been requested?
  • poige
    poige over 10 years
    You're mistaken, cp --parents does keep at least modes, ownership, I deem, would depend on whether it's run with sufficient privileges…
  • terdon
    terdon over 10 years
    @poige on my system (debian using cp (GNU coreutils) 8.21), cp --parents changes the ownership of the file to the user running the cp command (how are privileges relevant? Anyway, same happens when run as root) and also changes the modes to match the umask of the directory being copied to. Is that different on your system? Did you check or just "deem"? Are you perhaps thinking of cp -p or cp --preserve=all?
  • poige
    poige over 10 years
    -p or -a (which I do prefer) would for sure preserve it. But even w/o if directory doesn't have "x" flag set, it remains unset with cp --parents
  • terdon
    terdon over 10 years
    @poige I still can't see what your issue is. mv preserves attributes, cp does not unless it has been told to and --parents does not tell it to. Since the OP asked for a solution involving mv, I gave one. Since it uses mv, the attributes are kept. In any case, the OP did not specify anything about the file attributes so what exactly is it you don't like?
  • Flimm
    Flimm over 10 years
    I'm not sure I understand this sentence: "When you need to move files...deal with that later". Also, cp then rm is too slow for my needs.
  • poige
    poige over 10 years
    There's could be more than one file in those sub-dirs, actually. So if you move the parent dir and you don't want all those extra-files to be moved, you have to fix it by hands, one way or another. That's it.
  • poige
    poige over 10 years
    And regarding your "too slow" concern — as I pointed out in my answer mv is fast only when move files around the same block device. You can have cp working fast using its -l which would hardlink files instead of producing theirs copy.
  • poige
    poige over 10 years
    ok, back to the issue. Your alias would move all the content inside parent dirs, cp --parents wouldn't do that. So it's simply wrong (or non-equivalent) approach, as I pointed in mine answer to this question.
  • terdon
    terdon over 10 years
    @poige wow, you're really persistent. 1) The OP has accepted this answer (which uses functions, not aliases) so he clearly disagrees with you. 2) My answer does not move the content of the parent dirs (yours does by the way), it just replicates the directory structure and copies the target only.
  • poige
    poige over 10 years
    Somebody "+1" mine answer, so here it is — the new spin. It's not me. :-P 1) How does it justify — right, in no way, OPs can be wrong as we all can be. :) 2.1) Well, permissions ;-P 2.2) My bad, I beg ur pardon, yeah, your doesn't, I'm mistaken. :)