0

I know its a big classical but I didn't found the exact situation that concerns me

I need a mkdir+mv command that can be invoked like that :

mvdir /home/user/Documents/irs.pdf /mnt/work/45/223/insight/irs1970.pdf

Exactly like a normal mv command works, just with a creation of path instead of a no such file or directory

Considering that work/45/223/insight/ doesn't exist and need to be created

All other command that I've found can't be invoked like that, needs some more informations, need to distinguish the path and file ourself, or something

Attempt: mkdir -p /mnt/work/45/223/insight && mv /home/user/Documents/irs.pdf /mnt/work/45/223/insight/irs1970.pdf

aaa
  • 157
  • 1
  • 11
  • If you have a list of commands that would implement what you need you could write your own script – Chris Davies Nov 27 '22 at 10:43
  • Exactly. But all the script that I've found leads to a command that have not this form – aaa Nov 27 '22 at 10:44
  • Ignore scripts for a moment. How would you do this if you were typing it at the command line? – Chris Davies Nov 27 '22 at 10:51
  • mkdir -p /mnt/work/45/223/insight && mv /home/user/Documents/irs.pdf /mnt/work/45/223/insight/irs1970.pdf – aaa Nov 27 '22 at 10:56
  • You might find that /mnt/work/45 exists, in which case creating the whole name again will fail. Check man mkdir for the -p option. Also consider defining a function in your .profile when you have tested your commands: better than either an alias or a script. – Paul_Pedant Nov 27 '22 at 10:59
  • So it needs to a special mkdir command who can just complete the missing path, who not fail too. My command doesn't work as is because it's two commands. I need one that make the work, who distinguish himself the path and the filename, like mv, just with a creation of path instead of an error. Sure I will consider to write in hard the function when I will find one that fulfill the need – aaa Nov 27 '22 at 11:13
  • 1
    @aaa include your attempt at the script. Otherwise it is asking for code with no attempts to resolve your issue yourself. – James Risner Nov 27 '22 at 12:17

2 Answers2

0

It's not the right answer but I still couldn't find something that will do the job

So instead I did a workaround, in two time, but who permit me to use the normal capacity of mv. I recreated the path totally, then I moved the file

find /mnt -type d -print0 | while read -d $'\0' i; do mkdir -p /home/user/Documents$i; done
mv /home/user/Documents/irs.pdf /mnt/work/45/223/insight/irs1970.pdf

Not ideal, but, well

aaa
  • 157
  • 1
  • 11
0

Your attempt in the question is a fair stab. Here's how I would consider approaching it

#!/bin/sh
src=$1
dst=$2

if [ ! -d "$dst" ] then dir=${dst%/*} # dir path [ "$dir" = "$dst" ] && dir=. # but had no dir

mkdir -p "$dir"                 # create path

fi mv "$src" "$dst" # move file

Save this as your mvdir somewhere in your $PATH, and make it executable.

It would be possible to extend this script to handle multiple sources but I haven't done that here

Chris Davies
  • 116,213
  • 16
  • 160
  • 287