I've tried to make a bash script that will help me move specific subfolders from my folder.
The issue is,in the main folder photos I have folders named like this: 1,2,3,4,5,6,7,8,9,0 and inside them the folders with exact name repeat,and then again the same folder structure repeats. The subfolders I want to move are in the last folder with one character in their name.The subfolders I want to move have 2 or more characters in their name. Folder structure is 0-9/0-9/0-9/subdir The destination folder isn't specified,so let's say it is desktop/photos1.

Example: photos/0/0/0/23345 photos/1/9/0/12578

If two subdirectories have the same name e.g. photos/0/0/0/23345 and photos/1/1/1/23345 I would like to rename one of them. I'm on OSX

  • I would like to rename one of them
    – OneStyle07
    Mar 17, 2016 at 14:37
  • Yes the depth is always,photos/name/name/name/somedir in my case the "name" has one character 0-9 and subdir always has more than 2 characters in his name
    – OneStyle07
    Mar 17, 2016 at 15:03

1 Answer 1

  • On systems with GNU coreutils (so not a default installation of OSX), you could use

    mv -f --backup=numbered -t /path/to/target photos/*/*/*/*/

    This will move all subdirs at depth 4 under photos/, whatever the names of the dirs at depths 1, 2, and 3.

    The advantage of this method: renaming will happen only as needed (if you have only one 12578 subfolder, e.g. in photos/1/9/0/, it will not be renamed).

  • Without GNU coreutils, you can use this (somewhat convoluted) script, which will rename all subfolders. E.g. photos/1/9/0/12578 will be renamed 12578-190:

    for i in {0..9}; do
      for j in {0..9}; do
        for k in {0..9}; do
          for dir in "photos/$i/$j/$k/*/"; do
            mv -- "$dir" "$target/$(basename $dir)-$i$j$k"
  • I'm getting this error mv: rename 1027999 to /photos1/photos 1027999-9: No such file or directory
    – OneStyle07
    Mar 17, 2016 at 19:58
  • In target you must put the full path of your dir, something like /home/<username>/desktop/photos1, and this dir must exist. Now, I really don't see how that /space/ could appear: "photos 1027999-9". Please, double-check your typing.
    – L. Levrel
    Mar 17, 2016 at 20:20
  • now only thing that was moved are folders from 0/0/0/ the rest isn't moved,other like example : 1/2/3/ are not moved
    – OneStyle07
    Mar 17, 2016 at 20:25
  • This is weird. Are you sure your shell is bash? Type echo {0..9}, the output should be 0 1 2 3 4 5 6 7 8 9.
    – L. Levrel
    Mar 17, 2016 at 20:39
  • It seems i'v made a mistake explaining. Folders that were moved are only from 0/0/0 1/1/1 2/2/2 3/3/3 to 9/9/9 but all the folders like 1/2/3 2/1/9 aren't moved. Yes my shell is bash
    – OneStyle07
    Mar 17, 2016 at 20:46

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.