move multiple directories in one directory - recursively

9,027

Solution 1

Assuming that "Main Directory"/Test exists:

mv "Main Directory"/Sub[1-3] "Main Directory"/Test

The only thing happening here is that you move the directories into the Test directory. The files in Sub1, Sub2 and Sub3 will still be available in those same directories, but now under the new path "Main Directory"/Test/Sub1 etc.


With updated information in the comments below, assuming bash is used as the shell:

mkdir -p "Main Directory"/Test
mv "Main Directory"/episode_{0000..0049} "Main Directory"/Test

The brace expansion "Main Directory"/episode_{0000..0049} would expand to Main Directory/episode_0000 Main Directory/episode_0001 ... Main Directory/episode_0049.

Solution 2

You can also do this using -t flag with the mv command.

The syntax would look like the following.

mv -t <destination> <src1> <src2> .... <srnN>

You can do that in the reverse fashion as well:

mv file1 file2 file3 -t DESTINATION

In your case will be:

mv -t Test/ Sub1/ Sub2/ Sub3/

Source: DevopsCube / AskUbuntu

Solution 3

you can specify using curly braces,

mv {Sub1, Sub2, Sub3} Test/
Share:
9,027

Related videos on Youtube

Mostafa Hussein
Author by

Mostafa Hussein

Updated on September 18, 2022

Comments

  • Mostafa Hussein
    Mostafa Hussein almost 2 years

    I have a directory which contains multiple directories including subdirectories too. I want to move some of them to a single one at the same time (with one command)

    Example

    Main Directory
         Sub1
            Subsub1
            Subsub2
         Sub2
            Subsub1
            Subsub2
         Sub3
            Subsub1
            Subsub2
         Sub4
            Subsub1
            Subsub2
         Sub5
            Subsub1
            Subsub2
         Test
     -----------------------
    

    I want to move Sub1, Sub2, Sub3 including their subdirectories, into Test folder, so finally I will have something like this

    Main Directory
         Sub4
            Subsub1
            Subsub2
         Sub5
            Subsub1
            Subsub2
         Test
             Sub1
                 Subsub1
                 Subsub2
             Sub2
                 Subsub1
                 Subsub2
             Sub3
                 Subsub1
                 Subsub2
     -----------------------
    
  • Mostafa Hussein
    Mostafa Hussein almost 6 years
    when doing it in one line as Sub[1-3] , it gives me No such file or directory
  • Kusalananda
    Kusalananda almost 6 years
    @MostafaHussein Then you are either not executing the command from the directory containing "Main Directory", or your subdirectories have different names not matching the patternSub[1-3].
  • Mostafa Hussein
    Mostafa Hussein almost 6 years
    my subdirectories are like that: episode_0000 till episode_0049 I want to move episode_0000 till episode_0039 to another directory
  • Kusalananda
    Kusalananda almost 6 years
    @MostafaHussein It would have been helpful if you had stated that in the question from the start.
  • Mostafa Hussein
    Mostafa Hussein almost 6 years
    I am sorry, I didn't have the folders ready, I was just getting the idea, then I created these folders
  • Kusalananda
    Kusalananda almost 6 years
    @MostafaHussein See updated answer.
  • Mostafa Hussein
    Mostafa Hussein almost 6 years
    This worked, thanks a lot @Kusalananda and sorry for the conflict
  • Kusalananda
    Kusalananda about 5 years
    Did you test this? The result will probably surprise you.
  • InsOp
    InsOp almost 4 years
    @Kusalananda I did test it it works - but to make sure: could you elaborate?
  • Kusalananda
    Kusalananda almost 4 years
    @InsOp It would move the files {Sub1,, Sub2,, and Sub3} to the directory Test. It's not actually a brace expansion.