1

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

  • when doing it in one line as Sub[1-3] , it gives me No such file or directory – Mostafa Hussein Jul 10 '18 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 '18 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 – Mostafa Hussein Jul 10 '18 at 11:45
  • @MostafaHussein It would have been helpful if you had stated that in the question from the start. – Kusalananda Jul 10 '18 at 11:49
  • I am sorry, I didn't have the folders ready, I was just getting the idea, then I created these folders – Mostafa Hussein Jul 10 '18 at 11:50
1

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

0

you can specify using curly braces,

mv {Sub1, Sub2, Sub3} Test/
  • Did you test this? The result will probably surprise you. – Kusalananda May 15 '19 at 6:49

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.