0

I want to move files and folders to a folder within the same folder. The following model is an example

|-folderN
|-- folder1
|-- folder2
|-- file1

So the question asked is how can I move everything at once from folderN (/var/www) to folder2? Moving every file manually takes too long and it is not feasible in to long run.

Tried with

mv -v folderN/* folder2

and

mv folderN/* folder2 
5
  • Please explain what's wrong in what you're doing. Mar 6, 2018 at 15:06
  • what is the error you get with mv folder1/* folder2 ? Please update your post to include this information.
    – kemotep
    Mar 6, 2018 at 15:22
  • There is no error, but it is simply not feasible to take them one by one. which is what I would be doing if I did with mv folder1/* folder2. Mar 6, 2018 at 17:36
  • Are you trying to move all of folderN/* to folderN/folder2/, or all of folderN/folder1/* to folderN/folder2, or something else? Your question is ambiguous.
    – Useless
    Mar 6, 2018 at 19:02
  • folderN/* to folderN/folder2/, sorry, I edited the question to rectify. @Useless Mar 6, 2018 at 20:03

4 Answers 4

2

Another way using ls and grep -v. The -v option inverts the matching so only non matching lines are listed.

mv `ls | grep -v folder2` folder2

Note that backticks are used and not single quotes in the command.

2
  • This worked, it gave the error mv: cannot move 'folder2 to a subdirectory of itself, folder2/folder2 but moved everything inside the folder that was needed. Mar 7, 2018 at 8:41
  • @Schneejäger Hmm, that's odd. The grep -v folder2 should have excluded folder2 from trying to be moved and eliminated the warning.
    – Paul H.
    Mar 8, 2018 at 19:11
2

Move the second folder one step up, move the contents, then move the second folder back. Like so:

cd folder1
mv folder2 ..
mv * ../folder2/
mv ../folder2 .
4
  • I cannot move the main folder one up because it is in /var/ and I don't have sudo/root privileges. It would be /var/www but if I want to move it one up it would be /var. Mar 6, 2018 at 17:37
  • Move it to some other place where you do have write access. It's best if it's on the same file system, if possible, since that will be a lot quicker.
    – Jenny D
    Mar 7, 2018 at 9:11
  • I thought of that but I am not allowed access everywhere and have to make with what I've got, but it was solved. Mar 7, 2018 at 14:44
  • 1
    Yes, I saw and upvoted the accepted solution. This is Unix - there's almost always more than one way to do a thing, and what way you choose must depend on the particular circumstances. I'm glad you got it sorted!
    – Jenny D
    Mar 7, 2018 at 15:04
0

find + mv solution:

find folderN/ -mindepth 1 ! -wholename '*/folder2' -exec mv "{}" "folderN/folder2" \;
0

You are reporting that you are unable to move the contents of one folder to another? Is this the sequence of events you have tried?

$ ls folderN
file1 file2 folder1 folder2
$mv folderN/* folder2
mv: cannot move 'folder2 to a subdirectory of itself, `folder2/folder2`
$ls folderN/folder2
file1 file2 folder1

mv folderN/* folderN/folder2 should be the only command you need.

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.