0

I would like to know if it's possible to move all subdirectories and their contents within my working directory into the working directory, so that the subdirectories become primary directories in the tree.

for example:

I'm in workingdir, inside we have folder1, folder2, folder3, etc

now folder 1, 2, and 3 are already primary directories within the working directory, but when we get to folder4 there is a subdirectory within which has the same name folder4 then contents and this continues folder5, folder6 onto folder100andsomething.

What I would like is to run a command to move the subdirectories folder4 all the way to folder100andsomething along with their contents out of the parent directories, and into the working directory, so in the end I would have folder1 - folder100andsomething as primary directories and a bunch of erroneous empty parents. To clean up I can run a command to remove all the empty parent directories.

  • Could you not just mv folder4/* $(pwd)? – Stephen M. Webb Mar 4 '17 at 22:24
  • would that move all of the subdirectory contents as well? what about folder5-100? also in reality the names of the directories are not as simple as in the example – Johnny Mar 4 '17 at 22:27
  • basically I don't want to drag and drop each subdirectory one by one, there has to be a way to do a super drag and drop on all subdirs via command line. no? – Johnny Mar 4 '17 at 22:30
  • I other words in folder4, we have other folders from 1 - 100 with content in them. – George Udosen Mar 4 '17 at 23:00
  • NO sorry I may have been unclear. Folder4 only has subdirectory folder4 in it and this subdirectory has content. Then we have folder5 in the same working directory as folder4. Again Folder5 has subdir folder5 which has more content, then we have folder6 with subdir folder6+content, etc etc etc – Johnny Mar 4 '17 at 23:08
0

With find and rename:

$ find . -mindepth 3 | rename -n 's:([^/]+)/\1:$1:'  
./folder10/folder10/blah10 -> ./folder10/blah10
./folder9/folder9/blah9 -> ./folder9/blah9
./folder8/folder8/blah8 -> ./folder8/blah8
./folder7/folder7/blah7 -> ./folder7/blah7
./folder6/folder6/blah6 -> ./folder6/blah6
./folder5/folder5/blah5 -> ./folder5/blah5
./folder4/folder4/blah4 -> ./folder4/blah4

The regex ([^/]+)/\1 matches text which is repeated, and has a / in between (so, folder4/folder4, etc.). The replacement is just the part which was repeated (so, just folder4).

The -n option only shows what changes will be made. If the changes look OK, then run without it to actually perform the changes.

  • Ok thanks muru for the answer, I ran a quick test on a small directory and for the most part it worked. The only problem is that some of the subdirectories have a slightly different name to their parent directories. In this case the regex didn't work. – Johnny Mar 5 '17 at 14:51
  • @Johnny without some way to distinguish between the subfolders you want moved and other subfolders, it's going to be risky. Do you want to move all the subfolders up one level, for example, what do you do with ./folderX/folderY/fileA? – muru Mar 5 '17 at 14:53
  • Yeah basically I want to move all subdirectories up one level. with ./folderX/folderY/fileA I would like folderY to move up a level, keeping it's contents, so that it's next to folderX. folderX would then be empty and I could run find . type d -empty -delete to remove the empty directory – Johnny Mar 5 '17 at 19:41
  • is this possible? I can't find a command to do this – Johnny Mar 7 '17 at 13:16
  • Try with a pattern like 's:([^/]+)/([^/]+):$2:' – muru Mar 7 '17 at 13:26

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.