0

I want to move multiple directories to the directories if the second directory exist.

Initial:

dmv/toto/toto0
dmv/toto1/toto1
dmv/toto2/toto2

dmv/toto0/toto00
dmv/toto2/toto02

Final:

dmv/toto/toto0
dmv/toto1/toto1
dmv/toto2/toto2

dmv2/toto0/toto00
dmv2/toto0/toto0
dmv2/toto2/toto02
dmv2/toto2/toto2

I try this, but I don't check if a directory exist:

~/dmv/$ for i in $(ls); do mv "${i}"/* ../dmv2/"${i}"; done
  • Welcome to UNIX&Linux SE. Can you please edit your question to include an actual question or what your problem is, any errors you have received, etc? – 0xSheepdog Apr 1 at 16:40
  • What directories, if any, existed under dvm2/ at the initial stage? – doneal24 Apr 1 at 16:43
0

Your initial and final directory listing stages are somewhat cryptic, and so I am not sure about what you ask for exactly. Perhaps this will help you a bit. A script tends to be more readable; call it mvDir.sh for instance:

#!/usr/bin/bash
for file in ./*; do
    [ -d "${file}" ] && [ mkdir -p ../dmv2/"${file}" ] \
    && mv "${file}"/* ../dmv2/"${file}" 
done

Make sure that:

  • the place where your script's path is included in your PATH environment variable. On cli, do: $ echo "$PATH") to check it.
  • the script is executable. On cli, do: $ chmod u+x mvDir.sh

To run it just go to the directory that contains the script and the directories whose names you want to parse, and on cli, do: $ ./mvDir.sh

If you insist on an awkwardly long one-liner, do in cli:

$ for file in ./*; do [ -d "${file}" ] && [ mkdir -p ../dmv2/"${file}" ] && mv "${file}"/* ../dmv2/"${file}"; done

There are other, more advanced ways of doing this, e.g. involving find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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