3

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
 -----------------------

3 Answers 3

10

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

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.

7
  • when doing it in one line as Sub[1-3] , it gives me No such file or directory Jul 10, 2018 at 11:32
  • @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].
    – Kusalananda
    Jul 10, 2018 at 11:43
  • my subdirectories are like that: episode_0000 till episode_0049 I want to move episode_0000 till episode_0039 to another directory Jul 10, 2018 at 11:45
  • @MostafaHussein It would have been helpful if you had stated that in the question from the start.
    – Kusalananda
    Jul 10, 2018 at 11:49
  • I am sorry, I didn't have the folders ready, I was just getting the idea, then I created these folders Jul 10, 2018 at 11:50
1

you can specify using curly braces,

mv {Sub1, Sub2, Sub3} Test/
3
  • Did you test this? The result will probably surprise you.
    – Kusalananda
    May 15, 2019 at 6:49
  • @Kusalananda I did test it it works - but to make sure: could you elaborate?
    – InsOp
    Aug 13, 2020 at 15:54
  • 1
    @InsOp It would move the files {Sub1,, Sub2,, and Sub3} to the directory Test. It's not actually a brace expansion.
    – Kusalananda
    Aug 13, 2020 at 16:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .