4

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!

Zanna
  • 70,465
mktoaster
  • 251

2 Answers2

3

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 !!
    
Zanna
  • 70,465
  • This does not work, still copying occur. I am trying to move 90 Gb file from home folder to root – Eugene Mala Aug 31 '18 at 00:41
  • What does not work? What are you doing and what error are you getting? @EugeneMala – Zanna Aug 31 '18 at 05:25
  • In my case mv always copied files because I tried to move file from password encrypted home folder – Eugene Mala Sep 01 '18 at 22:43
  • 1
    Thanks for pointing out that the directory already existing at the destination causes that error. I was dumbfounded because I thought it was supposed to be a simple command but I wrongly assumed that an earlier command had failed to create a directory. – GuiRitter Jul 09 '21 at 11:17
-1

Got it: Using sudo mv /Main1/Sub2 /Main2 did it for me. The main difference is being root I suppose.

Zanna
  • 70,465
mktoaster
  • 251
  • Actually this only works sometimes for whatever reason... Sometimes when I run the sudo mv command, it'll still show "Directory not empty" error... – mktoaster Jul 25 '14 at 21:01
  • 1
    There's an article about that ERROR message here: http://askubuntu.com/questions/269775/mv-directory-not-empty

    Seems to be caused by the FOlder not being empty. So the first time would work with mv, but it'll fail after that.

    – amanthethy Jul 25 '14 at 21:18
  • That is incorrect, it is not a permissions issue. The problem is that the directory you are trying to move that one to contains a directory with the same name. You can achieve what you want by first removing or renaming the directory /Main2/Sub2. After that, mv /Main1/Sub2 /Main2 will work as expected – Zanna Nov 21 '16 at 13:56