0

I have a folder structure with varying depths similar to the following.

parent/location/businessname/sublocation/hotelname
parent/location/businessname/hotelname

All of the lower most folders are Hotel Names and I'm trying to source these files in a PHP function. That aside, what I'm trying to do is to grab all of the lowest most folders and their contents into a single parent folder as below.

parent/hotelname

I'm on a Ubuntu machine and I'm new to command line but not totally ignorant to it. Any help would be appreciated.

  • try creating a symbolic link ln -s parent/location/businessname/sublocation/hotelname parent/location/businessnamen/hotelname hotelshortcut – Francisco Tapia Aug 21 '15 at 17:15
  • Cant do that. Long story but symlink isn't possible, i need to ditch all other folders apart from the hotelname folder as the previous folders need to be exact names, but some of them aren't correct. Thanks anyway. – Alex Knopp Aug 21 '15 at 17:19
  • so bassicaly you want stretch the path? – Francisco Tapia Aug 21 '15 at 17:22
  • What about File Explorer? Does it have to be at the command line? Unless you don't have access to X11.. – Leptonator Aug 21 '15 at 17:22
  • Is the final directory always hotelname/? If so, you could use something like find parent/ -type d -name hotelname -exec ln "{}/*" "{TargetDir}/" \;. This would create hard links to each of the files in a single target directory without changing any of the files in the original structure. The target directory can be deleted or rebuilt at any time. – AFH Aug 22 '15 at 13:12
0

Assuming the leaf-most folders are either in businessname/ or businessname/sublocation, you could start with the nested folders first, then proceed to the hotels at the same level as sublocation/:

$ cd /tmp
$ rm -rf parent
$ mkdir -p parent/location/businessname/sublocation/hotelname1 \
           parent/location/businessname/hotelname2

$ for depth in 2 1; do
    find parent/location/businessname -mindepth $depth -type d -print0 \
      | while read -d '' dir; do
          echo "$dir"
          mv "$dir" parent/ \
            && rmdir --ignore-fail-on-non-empty $(dirname "$dir")
        done
  done

$ find parent/
parent/
parent/hotelname2
parent/hotelname1
parent/location

This is a bit hacky in that it tries to remove the sublocation/ directory repeatedly (for every hotel contained therein) until it is empty.

It also does not deal with name collisions -- it will move the first hotel with a given name and leave others in place.

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.