1

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

1 Answers1

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:

    target=/some/path
    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"
          done
        done
      done
    done
    
L. Levrel
  • 1,503
  • I'm getting this error mv: rename 1027999 to /photos1/photos 1027999-9: No such file or directory – OneStyle07 Mar 17 '16 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 '16 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 '16 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 '16 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 '16 at 20:46
  • @don_crissti: if I do for dir in photos/$i/$i/$i/*/ I will have to do mv $dir $target/$(basename $dir)-$i. I thought the other way would be easier to understand for a newbie. (Also, thanks for the */ thing, I over-interpreted the request as saying there were only subdirs in the one-digit dirs.) – L. Levrel Mar 17 '16 at 20:46
  • @OneStyle07: yes your question is more than ambiguous, you stated "inside them the folders with exact name repeat" and you give examples 0/0/0 and 1/1/1! don_crissti's answer assumes the same. – L. Levrel Mar 17 '16 at 20:48
  • Yes i'm on OSX. – OneStyle07 Mar 17 '16 at 20:51
  • I'v made a mistake explaining the folder structure,like you said with this script the 1/1/1/subdir will be moved. Folder structure is 0-9/0-9/0-9/subdir so every folder,0-9 has 0-9 inside,like for example 1/9/2/subdir – OneStyle07 Mar 17 '16 at 21:01