Score:0

Rename files from different directories with their paths

cn flag

I don't know if I'm making sense but, this is roughly what I'm dealing with, and just in case I'll include that some folders have .jpg in the name:

Photos$ tree
.
├── 1
│   ├── 1.jpg
│   │   └── X
│   │       └── Y
│   │           ├── A
│   │           │   └── image.jpg
│   │           └── B
│   │               └── image.jpg
│   └──2.jpg
│      └── X
│          └── Y
│              ├── A
│              │   └── image.jpg
│              └── B
│                  └── image.jpg
│
└── 2
    ├── 1.jpg
    │   └── X
    │       └── Y
    │           ├── A
    │           │   └── image.jpg
    │           └── B
    │               └── image.jpg
    ├── 2.jpg
    │   └── X
    │       └── Y
    │           ├── A
    │           │   └── image.jpg
    │           ├── B
    │           │   └── image.jpg
 

Ideally, through the directories names, I would like it to recognize the correct order and rename the images accordingly 1.jpg ... 8.jpg.

If that's too complicated then rename them with their path:

  • 1_1.jpg_X_Y_A_image.jpg
  • 2_2.jpg_X_Y_B_image.jpg
  • etc.

... and maybe a way to extract all the actual jpg at once.

I tried (this answer](Rename files with folders' names and move them to one big folder), and I'm guessing I should adapt it better to how many subfolders I have, but I don't know how.

Here's a way I tried to fit in my directory:

user:~/home/test$ mkdir folder
user:~/home/test$ for file in Photos*/*/*.jpg/X*/Y*/*/*.jpg; do
> mv "$file" "folder/${file/\//_}"
> done

the result is always

mv: cannot move 'Photos/1/1.jpg/X/Y/A/image.jpg' to 'folder/Photos/1/1.jpg/X/Y/A/image.jpg': No such file or directory
mv: cannot move 'Photos/2/2.jpg/X/Y/B/image.jpg' to 'folder/Photos/2/2.jpg/X/Y/B/image.jpg': No such file or directory

What am I doing wrong? There are more than a 1000 images, so I can't do this manually!

pLumo avatar
in flag
Please make sure that your code matches your `tree`, so we can get a better idea of your problem. I don't see any "Natur_Set_*" directory. Also please post the full error message.
killmenowplease avatar
cn flag
edited it, sorry it was a mess, I had forgotten to include an additional layer of subfolders
pLumo avatar
in flag
You are lucky that it didn't work, or you would have overwritten the files, because you don't rename them. Q: Is the order for the ne naming important? What is the rule for it? Sorted lexically or what ?
killmenowplease avatar
cn flag
yes the correct order is fundamental, it needs to be alphabetical, but the path is ok too, I can sort it out
Arnaud Valmary avatar
my flag
I understand, you may be omitted a `/` in `${file/\//_}` ==> `${file//\//_}`
killmenowplease avatar
cn flag
thank you, this worked perfectly, you saved me hours of work
ar flag
Please don't put SOLVED in the question title. @ArnaudValmary consider turning your comments to an answer so that OP can accept it.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.