Ubuntu – How to move a subfolder with contents to a different source folder without copying

command linemv

I know it's been asked a million times, but I can't find the answer to what I'm trying to do specifically.

Here's the directory structure:

Main Folder1
|
|-Subfolder 1
|-Subfolder 2 HasAReallyLongName
|---files
|-Subfolder 3-1000

Main Folder2
|
|-Subfolder 2 HasAReallyLongName
|---files
  • I want to try to achieve moving Subfolder2 with its files to a
    different directory.
  • I don't want to copy and then remove anything because I don't have enough space.
  • I don't want to mv /Main/Sub2/* /Main2/ because that puts everything in the /Main2 folder and not in a subdirectory.
  • I don't want to mkdir /Main2/... because it's a really long name (timestamps mostly) and I don't want to manually type it (and probably mess it up)
  • As far as I know, rsync copies the files too, so I'm not sure that it
    would work.

Any suggestions or other tools would be much appreciated!

Best Answer

This is completely straightforward.

mv /Main1/Sub2 /Main2

This creates the directory /Main2/Sub2 with all the contents of the original, and deletes the directory /Main1/Sub2. If you don't want to type the full name, you can use a glob; just stop typing the name and end with * when you have entered enough characters to distinguish the directory name from everything else in the directory (be careful!):

mv /Main/Sub* /Main2

Possible problems that could be causing this not to work as expected:

  • There is already a directory inside /Main2 with the same name as /Main1/Sub2. This causes the error directory not empty, as mv is refusing to overwrite the existing directory. If this is the case, rename (or remove) the existing directory in /Main2 first:

    mv /Main2/Sub2 /Main2/SomethingElse
    
  • Using an incorrect path. Remember that everything is case sensitive and be aware of relative paths and ~ expansion. These are all different depending on the current working directory

    /Main1/Sub2
    Main1/Sub2
    ~/Main1/Sub2
    
  • You do not have permission to write to the source and/or destination directory. This will be very obvious from the error message: mv: cannot move 'thing' to '/place': Permission denied. In which case you can run my favourite command to overcome the problem

    sudo !!
    
Related Question