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
|-Subfolder 3-1000

Main Folder2
|-Subfolder 2 HasAReallyLongName
  • 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!

  • Try, mv /Main/Sub2 /Main2/. See if it helps. Note the subtle difference in our commands. – amanthethy Jul 25 '14 at 20:38
  • @amanthethy I got the error "folder not empty" and also tried it with the -f switch. Still nothing. – mktoaster Jul 25 '14 at 20:45
  • Sorry try with the -r flag. Or even -rf. – amanthethy Jul 25 '14 at 20:46
  • -r is an invalid option... cp has a -r for recursive but not mv – mktoaster Jul 25 '14 at 20:56

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

  • 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 !!
  • 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 0:41
  • What does not work? What are you doing and what error are you getting? @EugeneMala – Zanna Aug 31 '18 at 5:25
  • In my case mv always copied files because I tried to move file from password encrypted home folder – Eugene Mala Sep 1 '18 at 22:43

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

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

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.