I have thousands of files in different folders that I would like to rename with their directories and, but less importantly, edit a few details after.
As a structure example:
FOLDER A
---> folder 1
- 1.jpg
- 2.jpg
---> folder 2
- 1.jpg
- 2.jpg
FOLDER B
---> folder 1
- 1.jpg
- 2.jpg
...
The end name should be: FOLDER A_folder 1_1.jpg
…etc
and the less important edit: FOLDER A - 1_1.jpg
…etc
I don’t know if it’s relevant, but I have Ubuntu and from the terminal I’ve test tried this:
for file in Folders*/*/*/*.jpg; do
mv "$file" "${file////_}";
done
It correctly renames the files with their paths but simultaneously extracts them and put them in the main folder, how do I avoid that?
2
Answers
This should do the trick:
You need to add the path to the "target" file (the second parameter of the mv command) since you replaced all
/
with_
practically removing the path and making it part of the new name:TEST
Directories and files before running the loop:
Renaming the files and the results:
As for your "less important edit," you can rename the files again, like this:
Note that you don’t need to add the path here since the
_folder
to-
substitution done to thefile
variable only affects the file name and not the path.Final result: