0

I am trying to figure out how multiple mv commnads in a script work. I have a folder which has log, result and tmp folders. This is also where I wrote a shell script(make_move.csh) with the following commands

#!/bin/csh -f
set DN=$1
#mkdir FOLDER1/$DN
mv ./log ./FOLDER1/$DN
mv ./result ./FOLDER1/$DN
mv ./tmp ./FOLDER1/$DN

When I execute this script

csh make_move.csh 50_1000error

I see the contents of "log" get copied to FOLDER1/50_1000error, and the entire folder of result and tmp get copied over. The intention is to have FOLDER1/50_1000error have the three folders instead of 2 folders and content on 1.

I am not sure what I am missing, or why the firts mv command works differently then the second and third.

1 Answers1

-1

You have the chance to explicitly tell mv to move stuff into the target directory to avoid ambiguity in the case of empty dirs. Also make sure to recursively create all necessary dirs:

#!/bin/bash
mkdir -p FOLDER1/$1
mv -t ./FOLDER1/$1/ ./a
mv -t ./FOLDER1/$1/ ./b
mv -t ./FOLDER1/$1/ ./c

it correctly transforms

1& [planetmaker:~/test] $ ls
a  b  c  script.sh

to

1& [planetmaker:~/test] $ ./scritp.sh blubber
1& [planetmaker:~/test] $ ls
FOLDER1  script.sh
1& [planetmaker:~/test] $ ls FOLDER1/blubber
a  b  c

(a,b,c are the directories here)

  • 1
    That's no different to what the OP wrote, except that you've reversed the reading order of source and destination – Chris Davies Jul 01 '20 at 23:06
  • 1
    That's not so. mv src dest has ambiguity, as the dest may be interpreted as a file or a directory. The -t option removes the ambiguity, as it explicitly references a directory only. – Paul_Pedant Jul 02 '20 at 07:30